build helper plugin not parsing project version

2019-08-06 00:02发布

I have a project where I have a need to break the version number down and access it's component pieces when building a manifest file. After doing some searching around I found the build-helper-maven-plugin and figured my problem was solved. I added the plugin to the master POM, it's shown below.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>validate</phase>
        <id>parse-version</id>
        <goals>
          <goal>parse-version</goal>
        </goals>
        <configuration>
            <propertyPrefix>parsedVersion</propertyPrefix>
        </configuration>
      </execution>
    </executions>
  </plugin>

The project version at this point in time is 3.0.0-SNAPSHOT. I wanted to see all the pieces (although in the final version I may not use them all) so I added these lines to my manifest file.

<value name="majorVersion">${parsedVersion.majorVersion}</value>

<value name="minorVersion">{$parsedVersion.minorVersion}</value>

<value name="incrementalVersion">${parsedVersion.incrementalVersion}</value>

<value name="versionQualifier">${parsedVersion.qualifier}</value>

<value name="parsedBuildNumber">${parsedVersion.buildNumber}</value>

After building I get this.

<value name="majorVersion">0</value>

<value name="minorVersion">{$parsedVersion.minorVersion}</value>

<value name="incrementalVersion">0</value>

<value name="versionQualifier">3.00.0-SNAPSHOT</value>

<value name="parsedBuildNumber">0</value>

The value tag is actually an XML tag and there is a closing value tag in the manifest file, I had to remove them since they were messing up the display.

So the incremental version appears to be correct, although I'm not all that confident given that the major version isn't correct, it found no minor version, and the qualifier comes back as the entire version number, rather than just the SNAPSHOT piece i had expected. I can see where a build number of zero would be correct since we don't have what Maven considers a build number.

Any ideas on why the version number doesn't seem to be parsing? Have I implemented this plugin incorrectly?

thanks steve

1条回答
淡お忘
2楼-- · 2019-08-06 00:29

Change {$parsedVersion.minorVersion} to ${parsedVersion.minorVersion}


When are you inspecting the property values? Ensure you're doing it after the validate phase in your example, or if you want it on the same phase ensure the build-helper-maven-plugin occurs before the plugin you're using to inspect the property values. If I have this:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>Major: ${parsedVersion.majorVersion}</echo>
                            <echo>Minor: ${parsedVersion.minorVersion}</echo>
                            <echo>Incremental: ${parsedVersion.incrementalVersion}</echo>
                            <echo>Qualifier: ${parsedVersion.qualifier}</echo>
                            <echo>BuildNumber: ${parsedVersion.buildNumber}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

after the build-helper-maven-plugin, I see this in the output of mvn validate:

[INFO] --- build-helper-maven-plugin:1.7:parse-version (parse-version) @ so-example ---
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ so-example ---
[INFO] Executing tasks
     [echo] Major: 1
     [echo] Minor: 0
     [echo] Incremental: 0
     [echo] Qualifier: SNAPSHOT
     [echo] BuildNumber: 0

anew@buddha:~/dev/so-example$ grep "<version>" pom.xml | head -1
    <version>1.0.0-SNAPSHOT</version>
查看更多
登录 后发表回答