我可以使用 buildnumber-maven-plugin 设置项目版本吗?
我正在尝试将 svn.revision 添加到项目版本作为内部版本号,但似乎无法这样做。 我的 jar 在打包期间具有正确的名称,但它安装在我的本地存储库中,就好像在设置版本时 ${buildNumber} 未定义。
我得到 foo-1.0.0-SNAPSHOT-${buildNumber} 而不是 foo-1.0.0-SNAPSHOT-304
知道我做错了什么或添加了什么修改项目版本是个坏主意吗? 谢谢您的帮助。
<project>
...
<version>1.0.0-${release.identifier}-${buildNumber}</version>
<properties>
<release.identifier>SNAPSHOT</release.identifier>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<useLastCommittedRevision>true</useLastCommittedRevision>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</project>
I'm trying to add the svn.revision to project version as a build number and can't seem to do so. My jar has the correct name durin packaging, but its installed in the my local repository it is as if ${buildNumber} is/was undefined when the version was set.
I get foo-1.0.0-SNAPSHOT-${buildNumber} instead of foo-1.0.0-SNAPSHOT-304
Any idea what I'm doing wrong or is adding a revision to the project version a bad idea? Thanks for the help.
<project>
...
<version>1.0.0-${release.identifier}-${buildNumber}</version>
<properties>
<release.identifier>SNAPSHOT</release.identifier>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<useLastCommittedRevision>true</useLastCommittedRevision>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题有两个部分:
您尝试在解决之前将
buildNumber
设置到版本中,因此它始终是${buildNumber}
而不是解析值。您应该将
buildNumber
设置到构建中的finalName
元素中,而不是尝试动态更改版本。 这将在本地存储库中创建具有预期名称的工件。安装插件将忽略
finalName
并将其部署为1.0.0-SNAPSHOT
无论如何,我不知道有什么方法可以解决这个问题。 如果您按如下方式配置插件,则buildNumber
将添加到清单中。所以你的配置会是这样的:
我会避免在 SNAPSHOT 项目上使用内部版本号。
Maven 提供了 SNAPSHOT 关键字来表示活跃开发中的不稳定项目。 因此,如果您引用具有
SNAPSHOT
依赖项版本的项目,Maven 将自动检查更新并保持您的依赖项同步。如果您随后在该版本的末尾添加内部版本号,则必须手动更新依赖项,因此您将失去使用 SNAPSHOT 后缀带来的任何好处。
无论如何,我个人都会尽可能避免使用内部版本号。 如果我必须更新项目,我只需更改版本号,或使用
beta-2
或RC2
等后缀。 如果您需要跟踪SNAPSHOT
中的修订,我建议将其添加到清单中,以便您可以检查构建的来源,但使用标准SNAPSHOT
后缀让Maven正常解析版本。 下面的配置显示了如何将修订添加到清单中。就您的配置而言,假设您的 SCM url 设置正确,对我来说看起来没问题。 如果您的 POM 中没有 SCM 配置,则可能是问题所在。
您可以使用
-X
运行并检查插件的任何输出,以表明它没有设置属性的原因吗?The problem has two parts:
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 thefinalName
element in the build. This will create the artifacts with the intended name in the local repository.The install plugin will ignore the
finalName
and deploy it as1.0.0-SNAPSHOT
regardless, I don't know of a way to address that. ThebuildNumber
is added to the Manifest if you configure the plugin as below.So your configuration would be something like:
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 aSNAPSHOT
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
orRC2
. If you need to track the revision in theSNAPSHOT
, I'd recommend adding it to the Manifest so you can check where the build originated, but use the standardSNAPSHOT
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?在
buildnumber-maven-plugin
后面添加:打包和部署时将识别
buildNumber
。Add this after the
buildnumber-maven-plugin
:And the
buildNumber
will be recognized by packaging and deploy.