build-helper-maven-plugin: unable to use replaced

2019-09-16 14:13发布

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?

1条回答
该账号已被封号
2楼-- · 2019-09-16 15:02

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.

查看更多
登录 后发表回答