我可以使用 buildnumber-maven-plugin 设置项目版本吗?

发布于 2024-07-30 14:46:33 字数 1183 浏览 10 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-08-06 14:46:34

问题有两个部分:

  1. 您尝试在解决之前将 buildNumber 设置到版本中,因此它始终是 ${buildNumber} 而不是解析值。

    您应该将 buildNumber 设置到构建中的 finalName 元素中,而不是尝试动态更改版本。 这将在本地存储库中创建具有预期名称的工件。

  2. 安装插件将忽略 finalName 并将其部署为 1.0.0-SNAPSHOT 无论如何,我不知道有什么方法可以解决这个问题。 如果您按如下方式配置插件,则 buildNumber 将添加到清单中。

    所以你的配置会是这样的:

    <版本>1.0.0-${release.identifier} 
      ... 
      <构建> 
        ${project.artifactId}-${project.version}-${buildNumber} 
        ... 
       
      

我会避免在 SNAPSHOT 项目上使用内部版本号。

Maven 提供了 SNAPSHOT 关键字来表示活跃开发中的不稳定项目。 因此,如果您引用具有 SNAPSHOT 依赖项版本的项目,Maven 将自动检查更新并保持您的依赖项同步。

如果您随后在该版本的末尾添加内部版本号,则必须手动更新依赖项,因此您将失去使用 SNAPSHOT 后缀带来的任何好处。

无论如何,我个人都会尽可能避免使用内部版本号。 如果我必须更新项目,我只需更改版本号,或使用 beta-2RC2 等后缀。 如果您需要跟踪 SNAPSHOT 中的修订,我建议将其添加到清单中,以便您可以检查构建的来源,但使用标准 SNAPSHOT 后缀让Maven正常解析版本。 下面的配置显示了如何将修订添加到清单中。

就您的配置而言,假设您的 SCM url 设置正确,对我来说看起来没问题。 如果您的 POM 中没有 SCM 配置,则可能是问题所在。

您可以使用 -X 运行并检查插件的任何输出,以表明它没有设置属性的原因吗?

<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>

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>
溺深海 2024-08-06 14:46:34

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>

打包和部署时将识别buildNumber

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文