Java 内部版本号:major.minor.revision
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1431315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build numbers: major.minor.revision
提问by Dave Jarvis
How would you write a build.xml
file, using neither custom code nor external dependencies (such as a shell script), that:
您将如何编写一个build.xml
既不使用自定义代码也不使用外部依赖项(例如 shell 脚本)的文件:
- Generates a build number of the form major.minor.revision (e.g., 01.02.34).
- Auto-increments the revision on each compile of the source code.
- Auto-increments the minor version on each execution of a dist(ribution) task.
- 生成格式为major.minor.revision(例如,01.02.34)的内部版本号。
- 在每次编译源代码时自动增加修订。
- 在每次执行 dist(ribution) 任务时自动增加次要版本。
Additionally:
此外:
- Provides an option to increment the major number.
- Provides an option to increment the minor number.
- Whenever the major number is incremented, the minor and revision numbers are set to 0.
- Whenever the minor number is incremented, the revision number is set to 0.
- 提供增加主编号的选项。
- 提供增加次要编号的选项。
- 每当主编号增加时,次编号和修订编号都设置为 0。
- 每当次要编号增加时,修订编号都设置为 0。
Bonus:
奖金:
- Creates a variable based on the
git
revision number (like a subversion revision number).
- 根据
git
修订号(如 subversion 修订号)创建一个变量。
Clarification:
澄清:
- Automatic checkout (or commit) is not required.
- Integration with Subversion is not desired.
- 不需要自动签出(或提交)。
- 不需要与 Subversion 集成。
Thank you for any examples. Here are some related sites that describe how to perform similar tasks:
感谢您提供任何示例。以下是一些描述如何执行类似任务的相关网站:
- Create a Build Numberwith Ant.
- Using the BuildNumberAnt task.
- Ant and Build VersionNumbers.
- 使用 Ant创建内部版本号。
- 使用BuildNumberAnt 任务。
- Ant 和Build 版本号。
采纳答案by rodrigoap
The build_info.properties
file:
该build_info.properties
文件:
build.major.number=00
build.revision.number=00
build.minor.number=00
The build.xml
file:
该build.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="current-number">
<property file="build_info.properties"/>
<property name="build.number" value="${build.major.number}.${build.minor.number}.${build.revision.number}"/>
<target name="current-number">
<echo>Current build number:${build.number}</echo>
</target>
<target name="compile">
<antcall target="revision"></antcall>
</target>
<target name="dist">
<antcall target="minor"></antcall>
</target>
<target name="revision">
<propertyfile file="build_info.properties">
<entry key="build.revision.number" type="int" operation="+" value="1" pattern="00"/>
</propertyfile>
</target>
<target name="minor">
<propertyfile file="build_info.properties">
<entry key="build.minor.number" type="int" operation="+" value="1" pattern="00"/>
<entry key="build.revision.number" type="int" value="0" pattern="00"/>
</propertyfile>
</target>
<target name="major">
<propertyfile file="build_info.properties">
<entry key="build.major.number" type="int" operation="+" value="1" pattern="00"/>
<entry key="build.minor.number" type="int" value="0" pattern="00"/>
<entry key="build.revision.number" type="int" value="0" pattern="00"/>
</propertyfile>
</target>
<target name="all">
<propertyfile file="build_info.properties">
<entry key="build.major.number" type="int" operation="+" value="1" pattern="00"/>
<entry key="build.minor.number" type="int" operation="+" value="1" pattern="00"/>
<entry key="build.revision.number" type="int" operation="+" value="1" pattern="00"/>
</propertyfile>
</target>
</project>
回答by Scrutator
Build Process
构建过程
build_info.properties
will be created during build in your project folder You could write all information about your build in this file.- Like build number, major and minor numbers of release, timestamp, and revision number.
- Your build script can modify these values how ever your want
- After the build was successfull commit the file 'build_info.properties' back to the repository
build_info.properties
将在构建期间在您的项目文件夹中创建 您可以在此文件中写入有关构建的所有信息。- 像内部版本号、主要和次要版本号、时间戳和修订号。
- 您的构建脚本可以根据需要修改这些值
- 构建成功后,将文件“build_info.properties”提交回存储库
During Development
开发中
After first build the file build_info.properties will be placed in the repository. You can change and commit any number (major, minor, build numbers) by your self when ever you want, or increase it automatically during build like build.number in the example below.
第一次构建后,文件 build_info.properties 将被放置在存储库中。您可以随时自行更改和提交任何编号(主要、次要、构建编号),或者在构建过程中自动增加它,例如下面示例中的 build.number。
svnant Example
svnant 示例
Using svnant 1.3.0:
使用 svnant 1.3.0:
<target name="checkout">
<echo>Checking out revision ${param_SubProjectSvnREV} of project: ${param_SubProjectSvnName}</echo>
<svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
<checkout url="${svnant.latest.url}/${param_SubProjectSvnName}/" revision="${param_SubProjectSvnREV}" destPath="${all.projects.dir}/${param_SubProjectDirName}" />
<info target="${all.projects.dir}/${param_SubProjectDirName}" ></info>
</svn>
<propertyfile file="${all.projects.dir}/${param_SubProjectDirName}/build_info.properties" comment="Modify build numbers in a properties file.">
<entry key="build.number" type="int" operation="+" value="1" pattern="00"/><!--increment it here -->
<entry key="build.revision" type="string" value="${svn.info.rev}"/>
<entry key="build.major.number" default="01"/><!-- can do some logic here to increase the values, or write value from somewhere else-->
<entry key="build.minor.number" default="01"/><!-- can do some logic here to increase the values, or write value from somewhere else-->
</propertyfile>
</target>
<target name="compile" depends="checkout">
<property file="${all.projects.dir}/${param_SubProjectDirName}/build_info.properties" />
<mkdir dir="${release.name}/${param_SubProjectDirName}/${build.major.number}.${build.minor.number}.${build.number}" />
<!-- compile it to the new folder, an so on... -->
<!-- after all, if the build wass successfull, commit the file 'build_info.properties' back to repository -->
</target>
回答by Scrutator
In my project i don't increase the minor and major number automatically. We set it from our global build properties. Like that:
在我的项目中,我不会自动增加次要和主要数字。我们从我们的全局构建属性中设置它。像那样:
<entry key="build.major.number" value="${global.release.major}"></entry>
<entry key="build.minor.number" value="${global.release.minor}"></entry>
That's because they will be changed for releases (not for test or other builds) and committed together with other sources (we have be able to build some old or branch version).
那是因为它们将针对发布(而不是测试或其他构建)进行更改并与其他源一起提交(我们已经能够构建一些旧版本或分支版本)。
But if you want to increase the minor number, you can do it like the build number in my example.
但是如果你想增加次要编号,你可以像我的例子中的内部版本号那样做。
<entry key="build.major.number" type="int" operation="+" default="1" pattern="00"/>
回答by AlBlue
The easiest way of doing this is to change the problem. Instead of making the Any build do this for you, have whatever process that you're calling Ant calculate what the version number should be, and then pass that in as a property e.g.
最简单的方法是改变问题。不要让 Any 构建为您执行此操作,而是让您调用 Ant 的任何过程计算版本号应该是什么,然后将其作为属性传入,例如
ant -Dbuild.version=1.2.3
ant -Dbuild.version=1.2.3
This has the flexibility of whatever build you're working with being able to take its cue from whatever, such as the SVN revision, the current date and time, or whatever.
这具有您正在使用的任何构建的灵活性,能够从任何内容中获取线索,例如 SVN 修订版、当前日期和时间或其他任何内容。
ant -Dbuild.version=svnversion .
ant -Dbuild.version=svnversion .
ant -Dbuild.version=date +"%Y%m%d%H%D"
ant -Dbuild.version=date +"%Y%m%d%H%D"
ant -Dbuild.version=${major}.svnversion .
.date +"%Y%m%d%H%D"
ant -Dbuild.version=${major}。svnversion .
.date +"%Y%m%d%H%D"
etc. You can get pretty comprehensive if you want.
等等。如果你愿意,你可以得到非常全面的信息。
If you want to have an ever incrementing number, then you can store it in a file and then pass that in at compile time. For example, you can do:
如果您想要一个不断增加的数字,那么您可以将它存储在一个文件中,然后在编译时将其传入。例如,您可以执行以下操作:
VER=cat build.version
VER=$((VER+1))
echo $VER > build.version
VER= cat build.version
VER=$((VER+1)) echo $VER > build.version
Lastly, if you really want this to be in the build.xml file, the best thing to do is have a separate task to execute the increment-and-build option and fork off a nested ant build with your 'main' target. You'd thus end up with
最后,如果你真的希望它在 build.xml 文件中,最好的办法是有一个单独的任务来执行 increment-and-build 选项,并用你的 'main' 目标分叉一个嵌套的 ant 构建。你最终会得到
ant -> ant -Dbuild.version=1.2.3.4 -> ...
蚂蚁 -> 蚂蚁 -Dbuild.version=1.2.3.4 -> ...
In other words, given your build.xml with a (current) default of 'build', then change it to 'version' and have the ant 'version task do the calculation followed by a nested call to and build.
换句话说,假设您的 build.xml 的(当前)默认为“build”,然后将其更改为“version”并让 ant 的“version”任务进行计算,然后嵌套调用并构建。
Implementation is left as an exercise to the reader, as is translating the approach to a non-UNIX platform.
实现留给读者作为练习,就像将方法转换到非 UNIX 平台一样。
回答by dz.
This solution does increment minor or revision number automatically if a compile or a dist target has been selected. The incrementation can be switched off if one of the following properties has been set:
如果选择了 compile 或 dist 目标,此解决方案会自动增加次要或修订号。如果设置了以下属性之一,则可以关闭增量:
- -Dno.increment.minor=true
- -Dno.increment.revision=true
- -Dno.increment.minor=true
- -Dno.increment.revision=true
If the property inc.major has been set, then the major number will be incremented and the other both values will be set to zero. The SHA-1 checksum is being calculated by the textual representation of the version file.
如果已设置属性 inc.major,则主编号将增加,其他两个值都将设置为零。SHA-1 校验和由版本文件的文本表示计算。
By the way: If would have been allowed, you could create your own ant task in java script, which is included in JDK 6.
顺便说一句:如果允许,您可以在 JDK 6 中包含的 Java 脚本中创建自己的 ant 任务。
Now here's the ant file
现在这是蚂蚁文件
<?xml version="1.0" encoding="UTF-8"?>
<project name="Numbers" default="dist" basedir=".">
<property name="version.file" location="${basedir}/version.properties"/>
<target name="inc.revision.properties" unless="no.increment.revision">
<propertyfile file="${version.file}">
<entry key="minor.number" default="00" operation="=" pattern="00" type="int"/>
<entry key="major.number" default="00" operation="=" pattern="00" type="int"/>
<entry key="build.number" default="00" operation="+" pattern="00" type="int"/>
</propertyfile>
</target>
<target name="inc.minor.properties" unless="no.increment.minor">
<propertyfile file="${version.file}">
<entry key="minor.number" default="00" operation="+" pattern="00" type="int"/>
<entry key="major.number" default="00" operation="=" pattern="00" type="int"/>
<entry key="build.number" value="00" operation="=" type="int"/>
</propertyfile>
</target>
<target name="inc.major" if="inc.major">
<property name="no.increment.minor" value="true" />
<property name="no.increment.revision" value="true" />
<propertyfile file="${version.file}">
<entry key="minor.number" value="00" operation="=" pattern="00" type="int"/>
<entry key="major.number" default="00" operation="+" pattern="00" type="int"/>
<entry key="build.number" value="00" operation="=" pattern="00" type="int"/>
</propertyfile>
<load.version.info/>
</target>
<target name="inc.minor" depends="inc.major,inc.minor.properties">
<property name="no.increment.revision" value="true"/>
<load.version.info/>
</target>
<target name="inc.revision" depends="inc.major,inc.revision.properties">
<load.version.info/>
</target>
<macrodef name="load.version.info">
<sequential>
<property file="${version.file}"/>
<checksum file="${version.file}" property="sha1.number" algorithm="SHA" format="CHECKSUM"/>
<echo>Version: ${major.number}.${minor.number}.${build.number}</echo>
<echo>SHA1: ${sha1.number}</echo>
</sequential>
</macrodef>
<target name="compile" depends="inc.revision" description="Compile Task"/>
<target name="dist" depends="inc.minor, compile" description="Dest Task"/>
</project>
回答by Nicholas Carey
This was a while ago, so this is from memory:
这是前一段时间,所以这是凭记忆:
I build a custom CruiseControl.Net labeller block that ticked up the build number on each build. It maintained an XML file with all 4 components of the version number and identified each project by name (so it could support multiple projects).
我构建了一个自定义的 CruiseControl.Net 标签器块,它在每个构建中标记了构建编号。它维护了一个 XML 文件,其中包含版本号的所有 4 个组件,并通过名称标识每个项目(因此它可以支持多个项目)。
The four values it generated get passed to the build process (nAnt, in our case), which had the responsibility of tweaking all the AssemblyInfo.cs files to reflect the proper build number.
它生成的四个值被传递到构建过程(在我们的例子中是 nAnt),它负责调整所有 AssemblyInfo.cs 文件以反映正确的构建号。
Edited to note: The only value that get automatically ticked up was the build number. Major/Minor version numbers were specified in the CC.Net project configuration. The build number restarted at 0001 for each change in major or minor revision number (e.g., if you went from version 7.1 to version 7.3, for instance, the 7.1 build might be at build number 783, but the first 7.3 build started with build number 1; the next 7.1 build would be build 784.
编辑以注意:唯一自动勾选的值是内部版本号。主要/次要版本号在 CC.Net 项目配置中指定。对于主要或次要修订号的每次更改,内部版本号从 0001 重新开始(例如,如果您从 7.1 版升级到 7.3 版,则 7.1 版本可能位于内部版本号 783,但第一个 7.3 内部版本以内部版本号开始1;下一个 7.1 版本将是 784 版本。
Change version numbers merely required tweaking 1 setting the CC.Net config file.
更改版本号只需要调整 1 设置 CC.Net 配置文件。
回答by Vasile Surdu
We can use conditions to check if we should increase the micro,minor and major version.
我们可以使用条件来检查是否应该增加微、次和主要版本。
Increase minor if micro is 9, and so on.
如果 micro 为 9,则增加小调,依此类推。
<target name="increaseBuildNumber" depends="increase.micro, increase.minor, increase.major" description="Increase Build Number"/>
<target name="increase.micro" if ="microNotEquals9">
<propertyfile file="build.properties">
<entry key="micro.number" default="0" operation="+" pattern="0" type="int"/>
</propertyfile>
</target>
<target name="increase.minor" if = "microEquals9andMinorNotEquals9">
<propertyfile file="build.properties">
<entry key="minor.number" default="0" operation="+" pattern="0" type="int"/>
<entry key="micro.number" value="0" operation="=" pattern="0" type="int"/>
</propertyfile>
</target>
<target name="increase.major" if = "microAndMinorEquals9" >
<propertyfile file="build.properties">
<entry key="major.number" default="0" operation="+" pattern="0" type="int"/>
<entry key="minor.number" value="0" operation="=" pattern="0" type="int"/>
<entry key="micro.number" value="0" operation="=" pattern="0" type="int"/>
</propertyfile>
</target>
<condition property="minorEquals9">
<equals arg1="${minor.number}" arg2="9"/>
</condition>
<condition property="microEquals9andMinorNotEquals9">
<and>
<equals arg1="${micro.number}" arg2="9"/>
<not><equals arg1="${minor.number}" arg2="9"/></not>
</and>
</condition>
<condition property="microAndMinorEquals9">
<and>
<equals arg1="${micro.number}" arg2="9"/>
<equals arg1="${minor.number}" arg2="9"/>
</and>
</condition>
<condition property="microNotEquals9">
<not><equals arg1="${micro.number}" arg2="9"/></not>
</condition>