Deviling Master
Deviling Master

Reputation: 3113

build-helper-maven-plugin: unable to use replaced value with other plugins

I have a Google App Engine Standard Maven project, which I created with the appengine-standard-archetype archetype.

I want to use the ${project.version} variable as deploy version, but the value is not allowed for certain characters:

May only contain lowercase letters, digits, and hyphens. Must begin and end with a letter or digit. Must not exceed 63 characters.

The value 0.0.1-SNAPSHOT need to be modified. I then used the build-helper-maven-plugin to obtain the replacement

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>version-urlsafe</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>project.version.urlsafe</name>
                <value>${project.version}</value>
                <regex>\.</regex>
                <replacement>-</replacement>
                <toLowerCase>true</toLowerCase>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

And the maven-antrun-plugin to show the value

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>regex-replace-echo</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>******** Displaying value of property ********</echo>
                    <echo>${project.version.urlsafe}</echo>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

At the end I used the new property as version for deploy

<app.deploy.version>${project.version.urlsafe}-urlsafe</app.deploy.version>

Note that I add the -urlsafe at the end of the value only to understand why the value is not considered

Running the deploy with mvn appengine:deploy I'm getting this output

...

[INFO] Executing tasks

main:
     [echo] ******** Displaying value of property ********
     [echo] 0-0-1-snapshot

...

gcloud.cmd app deploy --version ${project.version.urlsafe}-urlsafe
[INFO] GCLOUD: ERROR: (gcloud.app.deploy) argument --version/-v: Bad value [${project.version.urlsafe}-urlsafe]

Even if the ant-run plugin properly echoed the new version, when the deploy command is built, the variable itself is missing.

I then tried forcing the regex-property goal before the deploy, like follows

mvn build-helper:regex-property appengine:deploy

but I'm getting a missing configuration error in this case:

[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:regex-property (default-cli) on project maventest: The parameters 'regex', 'name', 'value' for goal org.codehaus.mojo:build-helper-maven-plugin:3.0.0:regex-property are missing or invalid -> [Help 1]

A little digression: I decided to manually run the build-helper:regex-property as additional goal due to a previous experience with a similar a case: a plugin that inject a new variable, which is properly echoed but when it comes to use the value, is missing. Here is the reference: Unable to obtaing git.branch property

Cooperating with the plugin author we found that adding the plugin goal before the appengine one can solve the issue mvn git-commit-id:revision appengine:deploy. At the end the root cause of this problem was a Maven bug: https://issues.apache.org/jira/browse/MNG-6260

So even the workaround of directly calling the plugin is not suitable in this case due to a plugin configuration error.

How can issue can be solved? How can I obtain the ${project.version.urlsafe} variable properly created when executing the appengine deploy?

Upvotes: 1

Views: 2235

Answers (2)

James Winnchester
James Winnchester

Reputation: 54

my whole pom

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.my.example</groupId>
<artifactId>note</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>note</name>
<description>spring boot note project</description>
<properties>
    <java.version>17</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}-${project.custom.version}</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${build-helper-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>custom-version</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>project.custom.version</name>
                        <value>${project.version}</value>
                        <regex>\.</regex>
                        <replacement>-</replacement>
                        <toLowerCase>true</toLowerCase>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>${maven-antrun-plugin.version}</version>
            <executions>
                <execution>
                    <id>regex-replace-echo</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <echo>******** Displaying value of property ********</echo>
                            <echo>${project.custom.version}</echo>
                            <echo>${project.name}</echo>
                            <echo>${project.artifactId}</echo>
                            <echo>${project.version}</echo>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

enter image description here

so the requirement is the project jar name must have version, but couldn't contain dots in version, must replace dot with dash character.

<version>${build-helper-maven-plugin.version}</version>
<version>${maven-antrun-plugin.version}</version>

click these two will go to spring-boot-dependencies-3.2.3.pom:

<build-helper-maven-plugin.version>3.4.0</build-helper-maven-plugin.version>
<maven-antrun-plugin.version>3.1.0</maven-antrun-plugin.version>

then you can

mvn clean package

will generate the jar as: note-0-0-1-snapshot.jar

but my intellij still shows the error in my pom: Cannot resolve symbol 'project.custom.version' it's red, no idea why IDE doesn't recognize it

Upvotes: 0

fboulay
fboulay

Reputation: 401

I had the same problem using the appengine-maven-plugin. And you're right, you need to first call the goal build-helper:regex-property before deploying on app engine. But to make this work, you have to move the configuration part outside the executions tag. Here is the complete configuration I am currently using:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <name>project.version.urlsafe</name>
    <value>${project.version}</value>
    <regex>\.</regex>
    <replacement>-</replacement>
    <toLowerCase>true</toLowerCase>
    <failIfNoMatch>false</failIfNoMatch>
    <fileSet/>
    <source/>
  </configuration>
</plugin>

Then when calling mvn build-helper:regex-property appengine:deploy, everything should work as expected.

Upvotes: 2

Related Questions