How Version Control and Build Numbering works in Java project
In a Java project, version control and build numbering are managed using build tools like Maven or Gradle, along with version control systems like Git. Semantic Versioning (SemVer) is often used to track different releases and changes in the software.
Version Control and Build Numbering in Java with Maven
Maven is a popular build automation tool for Java projects. It uses a pom.xml
file to manage project configuration, including versioning.
Setting Up Version Control and Build Numbering with Maven
-
Initial Setup in
pom.xml
: Define the version of your project in thepom.xml
file.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> </project>
-
Incrementing Version:
- Manually update the
<version>
element inpom.xml
for each release. - Commit and tag the version in Git:
git commit -am "Release version 1.0.1" git tag -a v1.0.1 -m "Version 1.0.1"
- Manually update the
-
Automating Version Increment with Maven Release Plugin: Maven Release Plugin automates the release process, including version incrementing, tagging, and pushing to the repository.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>3.0.0-M1</version> <configuration> <autoVersionSubmodules>true</autoVersionSubmodules> </configuration> </plugin> </plugins> </build>
-
Using Maven Release Plugin:
-
Prepare for a release:
mvn release:prepare
This command:
- Prompts for the release version.
- Updates the
pom.xml
file with the new release version. - Commits the changes.
- Tags the release in Git.
-
Perform the release:
mvn release:perform
This command:
- Checks out the tagged version.
- Builds and deploys the project.
-
Version Control and Build Numbering in Java with Gradle
Gradle is another popular build automation tool for Java projects, known for its flexibility and powerful DSL.
Setting Up Version Control and Build Numbering with Gradle
-
Initial Setup in
build.gradle
: Define the version of your project in thebuild.gradle
file.version = '1.0.0'
-
Incrementing Version:
- Manually update the
version
variable inbuild.gradle
for each release. - Commit and tag the version in Git:
git commit -am "Release version 1.0.1" git tag -a v1.0.1 -m "Version 1.0.1"
- Manually update the
-
Automating Version Increment with Gradle Release Plugin: The
net.researchgate.release
plugin automates the release process, including version incrementing, tagging, and pushing to the repository.plugins { id 'net.researchgate.release' version '2.8.1' } version = '1.0.0'
-
Using Gradle Release Plugin:
- Configure the release plugin:
release { failOnSnapshotDependencies = true git { requireBranch = 'master' } }
- Prepare and perform the release:
This command:./gradlew release
- Prompts for the release version.
- Updates the
build.gradle
file with the new release version. - Commits the changes.
- Tags the release in Git.
- Builds and deploys the project.
- Configure the release plugin:
Example Workflow
Using Maven
-
Initial Setup: Define the initial version in
pom.xml
.<version>1.0.0</version>
-
Increment Version for New Release: Manually update the version to
1.0.1
inpom.xml
or use Maven Release Plugin:mvn release:prepare mvn release:perform
-
Commit and Tag:
git commit -am "Release version 1.0.1" git tag -a v1.0.1 -m "Version 1.0.1"
Using Gradle
-
Initial Setup: Define the initial version in
build.gradle
.version = '1.0.0'
-
Increment Version for New Release: Manually update the version to
1.0.1
inbuild.gradle
or use Gradle Release Plugin:./gradlew release
-
Commit and Tag:
git commit -am "Release version 1.0.1" git tag -a v1.0.1 -m "Version 1.0.1"