自动递增Java项目中的内部版本号 [英] Automatically incrementing a build number in a Java project

查看:128
本文介绍了自动递增Java项目中的内部版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由abbuild代表的版本控制系统,其中a是整体版本(原型,alpha和beta版本为0,主要版本为1),b是里程碑版本(沿着表示原型,alpha,beta阶段)和build表示项目编译的次数。

I'm using a versioning system that is represented by a.b.build where a is the overall version (will be 0 for prototype, alpha and beta versions, 1 for major release), b is the milestone version (along the lines of representing the proto, alpha, beta stages) and build represents literally the amount of times the project has been compiled.

目前,我从文本中读取了应用程序文件,增加数字,并在设置调试标志的情况下运行应用程序时保存到文本文件。

At the moment, I have the app read from a text file, increment the number, and save to a text file when the app is run with a debug flag set.

我正在寻找更正确的方法使用Java和Netbeans做到这一点。有什么方法可以将构建数量注入到构建过程的某个地方吗?最好将数字保存到项目附带的源文件中 - 而不是依赖附近文件的存在。

I'm looking for a more "correct" way to do this using Java and Netbeans. Is there some way I can inject a build numberer into the build process somewhere? preferably saving the number into a source file that is shipped with the project - instead of relying on the existence of a nearby file.

推荐答案

有几个流行的Maven插件可以实现这一壮举:

There are a couple of popular Maven plugins that accomplish this feat:

  • Maven Release Plugin
  • Build Number Maven Plugin

Maven Release Plugin来自只需更新版本,Apache Maven项目有点矫枉过正数。因此,使用后者插件创建版本号(以MAJOR.MINOR的形式) .BUILD;例如, 3.1.4 其中 4 自动递增),如下所示:

The Maven Release Plugin from the Apache Maven Project is a bit overkill for simply updating the version number. Therefore use the latter plugin to create a version number (in the form of MAJOR.MINOR.BUILD; e.g., 3.1.4 where 4 is auto-incremented) as follows:


  1. 打开项目的 pom.xml 文件。

  2. 包括 build 部分中的插件(在依赖项部分之后):

  1. Open the project's pom.xml file.
  2. Include the plugin within the build section (after the dependencies section):



  <scm>
    <connection>scm:svn:http://127.0.0.1/dummy</connection>
    <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
    <tag>HEAD</tag>
    <url>http://127.0.0.1/dummy</url>
  </scm>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <id>buildnumber</id>
            <phase>validate</phase>
            <goals>
              <goal>create</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <format>{0,number}</format>
          <items>
            <item>buildNumber</item>
          </items>                    
          <doCheck>false</doCheck>
          <doUpdate>false</doUpdate>
          <revisionOnScmFailure>unknownbuild</revisionOnScmFailure>   
        </configuration>
      </plugin>    
    </plugins>
    <finalName>${project.artifactId}-${project.version}.${buildNumber}</finalName>
  </build>




  1. 确保 pom.xml 定义文件顶部附近的version元素中的主要版本和次要版本。例如:

  1. Ensure that the pom.xml defines the major and minor version in the version element near the top of the file. For example:



<version>3.1</version>




  1. 保存 pom .xml file。

  2. 重建项目。

  1. Save the pom.xml file.
  2. Rebuild the project.

版本号应该增加。

该插件需要配置的源代码管理存储库(< scm> ; )元素。如果您不关心存储库登记号码,请使用虚拟scm 。这可用于包括存储库中的修订号,这是读者

The plugin requires a configured source code management repository (<scm>) element. If you don't care about the repository check-in number use a dummy scm instead. This can be used to include the revision number from the repository, which is an exercise for the reader.

这篇关于自动递增Java项目中的内部版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆