Can I set the project version with a buildnumber-maven-plugin?

我怕爱的太早我们不能终老 提交于 2019-11-28 20:46:33
Rich Seller

The problem has two parts:

  1. You're trying to set the buildNumber into the version before it is resolved so it will always be ${buildNumber} rather than the resolved value.

    Instead of trying to dynamically change the version, you should set the buildNumber into the finalName element in the build. This will create the artifacts with the intended name in the local repository.

  2. The install plugin will ignore the finalName and deploy it as 1.0.0-SNAPSHOT regardless, I don't know of a way to address that. The buildNumber is added to the Manifest if you configure the plugin as below.

    So your configuration would be something like:

    <version>1.0.0-${release.identifier}</version>
    ...
    <build>
      <finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
      ...
    </build>
    

I would avoid using build numbers on SNAPSHOT projects.

Maven provides the SNAPSHOT keyword to signify a volatile project in active development. So if you reference a project with a SNAPSHOT dependency version, Maven will automatically check for updates and keep your dependencies in sync.

If you then add a build number to the end of that version, you will have to manually update the dependencies, so you lose any benefit of having the SNAPSHOT suffix.

I personally avoid using build numbers where possible anyway. If I have to update a project, I just bump the version number, or use a suffix like beta-2 or RC2. If you need to track the revision in the SNAPSHOT, I'd recommend adding it to the Manifest so you can check where the build originated, but use the standard SNAPSHOT suffix to allow Maven to resolve the versions normally. The configuration below shows how to add the revision to the Manifest.

As far as your configuration is concerned, it looks OK to me assuming your SCM url is set up correctly. If you have no SCM configuration in your POM that may be the problem.

Can you run with -X and check for any output from the plugin indicating why it isn't setting the property?

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>buildnumber-maven-plugin</artifactId>
  <version>0.9.4</version>
  <executions>
    <execution>
      <id>useLastCommittedRevision</id>
      <phase>validate</phase>
      <goals>
        <goal>create</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
      </manifest>
      <manifestEntries>
        <Implementation-Build>${buildNumber}</Implementation-Build>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Add this after the buildnumber-maven-plugin:

<plugin>
    <groupId>io.github.michaldo</groupId>
    <artifactId>nashorn-maven-plugin</artifactId>
    <version>0.0.1</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>eval</goal>
            </goals>
            <configuration>
                <script>
                    $project.artifact.version = "${buildNumber}";
                </script>
            </configuration>
        </execution>
    </executions>
</plugin>

And the buildNumber will be recognized by packaging and deploy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!