问题
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.
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.
回答1:
There are a couple of popular Maven plugins that accomplish this feat:
- Maven Release Plugin
- Build Number Maven Plugin
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:
- Open the project's
pom.xml
file. - Include the plugin within the
build
section (after thedependencies
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>
- 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>
- Save the
pom.xml
file. - Rebuild the project.
The version number should increase.
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.
回答2:
You can use git commit count of the current branch as the build number. Use this command line to get the commit count: git rev-list HEAD --count
.
Integrating with the build tools will be quite straightforward. If you are using Gradle then you can use this plugin: https://github.com/rockerhieu/Versionberg/
回答3:
I have the some problem and using the answer of "Dave Jarvis". This adds the correct version and build number to the localy generated jar file like: myProject-3.1.1234.jar. But in the maven repository the artifact is installed as version "3.1" instead of (I have expected) "3.1.1234". The build number ist still missing there...
To install the artifact into the maven repository with the correct version and build number ("3.1.1234") you must do (additionally to the answer of "Dave Jarvis"):
- Adding a major/minor version property:
<properties>
<!-- !!!!!!!!! SETTING HERE THE CORRECT MAJOR/MINOR VERSION NUMER !!!!!!!!!! -->
<major.minor.version>1.0</major.minor.version>
</properties>
- Setting the correct version number to the artifact by using the
nashorn-maven-plugin
:
<!-- set "major.minor.revision" version -->
<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 = "${major.minor.version}.${buildNumber}";
</script>
</configuration>
</execution>
</executions>
</plugin>
After rebuilding/installing the project the artifact will be in the maven repository as version "3.1.1234" :-)
Please note: to increment the major or minor version you must only change the major.minor.version
property!
来源:https://stackoverflow.com/questions/8988405/automatically-incrementing-a-build-number-in-a-java-project