Home  Java   How version ...

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

  1. Initial Setup in pom.xml: Define the version of your project in the pom.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>
    
  2. Incrementing Version:

    • Manually update the <version> element in pom.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"
      
  3. 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>
    
  4. 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

  1. Initial Setup in build.gradle: Define the version of your project in the build.gradle file.

    version = '1.0.0'
    
  2. Incrementing Version:

    • Manually update the version variable in build.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"
      
  3. 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'
    
  4. Using Gradle Release Plugin:

    • Configure the release plugin:
      release {
        failOnSnapshotDependencies = true
        git {
          requireBranch = 'master'
        }
      }
      
    • Prepare and perform the release:
      ./gradlew release
      
      This command:
      • 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.

Example Workflow

Using Maven

  1. Initial Setup: Define the initial version in pom.xml.

    <version>1.0.0</version>
    
  2. Increment Version for New Release: Manually update the version to 1.0.1 in pom.xml or use Maven Release Plugin:

    mvn release:prepare
    mvn release:perform
    
  3. Commit and Tag:

    git commit -am "Release version 1.0.1"
    git tag -a v1.0.1 -m "Version 1.0.1"
    

Using Gradle

  1. Initial Setup: Define the initial version in build.gradle.

    version = '1.0.0'
    
  2. Increment Version for New Release: Manually update the version to 1.0.1 in build.gradle or use Gradle Release Plugin:

    ./gradlew release
    
  3. Commit and Tag:

    git commit -am "Release version 1.0.1"
    git tag -a v1.0.1 -m "Version 1.0.1"
    
Published on: Jul 01, 2024, 07:50 AM  
 

Comments

Add your comment