Can anyone please tell me how to apply the semver to the java maven project? I tried many ways, but I didn’t find any useful resources to automatically increase the version when I push the code to the branch. I’m using Github action workflow to deploy the project into GitHub.
Thank you.
Advertisement
Answer
My first approach is to use the command line but you have to configuration the following in your pom file before. You can of course directly use the command line and put everything on the plain command without this setup but it’s very inconvenient
<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.9.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.9.0</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <executions> <execution> <id>major</id> <goals> <goal>set</goal> </goals> <configuration> <generateBackupPoms>false</generateBackupPoms> <newVersion>${parsedVersion.nextMajorVersion}.0.0-SNAPSHOT</newVersion> </configuration> </execution> <execution> <id>minor</id> <goals> <goal>set</goal> </goals> <configuration> <generateBackupPoms>false</generateBackupPoms> <newVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT</newVersion> </configuration> </execution> <execution> <id>patch</id> <goals> <goal>set</goal> </goals> <configuration> <generateBackupPoms>false</generateBackupPoms> <newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT</newVersion> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>default-cli</id> <goals> <goal>parse-version</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
By using the above configuration you can change/update the version of your project like this:
mvn build-helper:parse-version versions:set@major
This will increment the major version and set minor and patch version to 0
.
mvn build-helper:parse-version versions:set@minor
This will increment the minor version and set patch version to zero.
mvn build-helper:parse-version versions:set@patch
this will increment the patch version. Afterwards you have to commit your changed back into your version control system (for example git).
I recommend to define this kind of setup into a parent pom and reuse it for multiple projects. A detail explanation why and how this works can be found here https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/
Using the maven-release-plugin is also an option. It will make also the tags in your version control.