简体   繁体   English

如何将语义版本控制应用于 java maven 项目以自动增加 pom.xml 中的版本

[英]how to apply semantic versioning to the java maven project to automatically increment the version inside pom.xml

Can anyone please tell me how to apply the semver to the java maven project?谁能告诉我如何将 semver 应用于 java maven 项目? 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.我正在使用 Github 操作工作流将项目部署到 GitHub 中。

Thank you.谢谢你。

After some reading, here is what I have found.经过一番阅读,这是我发现的。 Keep in mind, I have not tested any of these nor am I very knowledgeable with CI's and automation techniques.请记住,我没有测试过任何这些,我对 CI 和自动化技术也不是很了解。

Potential Simple way: Maven Release Plugin潜在的简单方式: Maven 发布插件

With CI integration:使用 CI 集成:

  1. Travis CI特拉维斯 CI
  2. Jenkins Jenkins
  3. Git CI Guide Git CI 指南

My first approach is to use the command line but you have to configuration the following in your pom file before.我的第一种方法是使用命令行,但您必须先在 pom 文件中配置以下内容。 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 .这将增加主要版本并将次要版本和补丁版本设置为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).之后,您必须将更改提交回您的版本控制系统(例如 git)。

I recommend to define this kind of setup into a parent pom and reuse it for multiple projects.我建议将这种设置定义为父 pom 并将其重用于多个项目。 A detail explanation why and how this works can be found here https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/可以在此处找到其工作原理和方式的详细说明https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/

Using the maven-release-plugin is also an option.使用 maven-release-plugin 也是一种选择。 It will make also the tags in your version control.它还将在您的版本控制中创建标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置pom.xml以在1 Maven项目中运行2个java主管 - How to Configure pom.xml to run 2 java mains in 1 Maven project 如何在 pom.xml 中自动更新版本 - How to update version automatically in pom.xml 在Maven pom.xml中自定义Jar版本 - Custom versioning of Jar in maven pom.xml Maven pom.xml 问题(缺少项目构建错误“版本”) - Maven pom.xml Problems (Project Build Error "version" is missing) 从 pom.xml 从 Java Maven 项目在 Z3B06ADACCEC858ZEDEFBBC66 管道中获取版本 - Get the version from pom.xml from a Java Maven project in Bitbucket pipeline 将AspectJ添加到pom.xml改为使用Maven的Java版本,为什么? - Adding AspectJ to pom.xml changed Java version with Maven, why? 如何在 Spring Boot 项目的 maven pom.xml 中检查另一个依赖版本? - How to check for another dependency version in the maven pom.xml in a Spring Boot project? 如何在不更新pom.xml的情况下在Maven Web项目中捆绑/打包自定义库的最新版本? - How to bundle/package latest version of custom library in maven web project without updating pom.xml? 如何在Eclipse代码模板中获取Maven pom.xml版本? - How to get Maven pom.xml version in Eclipse code template? 如何替换 Maven 子模块 pom.xml 文件中的版本号? - How to replace version number in Maven submodule pom.xml files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM