我们日常开发中更多的是采用 Gitflow开发流程,release插件 对于多分支的支持不怎么好,本文仅针对 gitflow开发流程,使用gitflow-maven-plugin插件并进行定制配置。

POM配置

  1. gitflow插件,主要负责gitflow版本管理。

    pom.xml
    <plugin>
        <groupId>com.amashchenko.maven.plugin</groupId>
        <artifactId>gitflow-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
            <versionsForceUpdate>true</versionsForceUpdate>
            <versionDigitToIncrement>1</versionDigitToIncrement>
            <skipTestProject>true</skipTestProject>
            <versionProperty>revision</versionProperty>
            <skipUpdateVersion>true</skipUpdateVersion>
            <skipFeatureVersion>true</skipFeatureVersion>
            <useSnapshotInHotfix>true</useSnapshotInHotfix>
            <digitsOnlyDevVersion>true</digitsOnlyDevVersion>
            <commitMessages>
                <featureStartMessage>开始@{featureName}新特性开发</featureStartMessage>
                <featureFinishMessage>完成@{featureName}新特性开发</featureFinishMessage>
                <hotfixStartMessage>开始修复版本@{version}</hotfixStartMessage>
                <hotfixFinishMessage>完成修复,更新版本为@{version}</hotfixFinishMessage>
                <hotfixVersionUpdateMessage>更新为修复版本号</hotfixVersionUpdateMessage>
                <releaseStartMessage>开始发布@{version}版本</releaseStartMessage>
                <releaseFinishMessage>完成版本发布,开始下一个版本@{version}的开发</releaseFinishMessage>
                <releaseVersionUpdateMessage>更新为发布版本@{version}</releaseVersionUpdateMessage>
            </commitMessages>
        </configuration>
    </plugin>
  2. versions插件,主要负责版本号管理,多模块工程非常方便,gitflow会自动调用该插件。

    pom.xml
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.7</version>
        <configuration>
            <generateBackupPoms>false</generateBackupPoms>
        </configuration>
    </plugin>
  3. flatten插件,CI Friendly模式下使用,可以在发布的时候自动将${revision}变量替换为真实的版本号。

    pom.xml
    <version>${revision}</version>
    <properties>
        <revision>1.0.0-SNAPSHOT</revision>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Note
如果工程子模块和目录层级不一致,则所有逻辑顶层都要加,不然只有符合层级结构的模块才生效。

CI配置

使用gitflow后提交会比较频繁,需要减少不必要的CI构建。 推荐只配置两个构建:

  1. 开发部署(每次提交develop分支自动部署);

  2. 生产部署(手工部署,仅tag有效)。

    gitlab-ci.yml
    stages:
    - develop_deploy
    - deploy
    develop_deploy:
      stage: develop_deploy
      script:
        - echo "开发部署"
      only:
        - develop
        - /^hotfix\/.*$/
      except:
        - tags
      tags:
        - deploy
    prod_deploy:
      stage: prod_deploy
      script:
        - echo "生产部署"
      only:
        - tags
      when: manual
      tags:
        - deploy

命令

交互命令

命令 说明

mvn gitflow:release-start

开始发布,会新建发布分支,并更新版本为发布版本号

mvn gitflow:release-finish

完成发布,会合并发布分支到master和develop,然后更新develop分支的版本为下一个开发版本号

mvn gitflow:release

发布,不新建发布分支,直接修改develo分支为发布版本号后合并到master,然后再把develop更新为下一个开发版本号

mvn gitflow:feature-start

开始feature开发,新建feature分支,不更新版本号

mvn gitflow:feature-finish

完成feature开发,合并feature分支到develop

mvn gitflow:hotfix-start

开始缺陷修复,新建hotfix分支,并更新版本号为下一个小版本

mvn gitflow:hotfix-finish

完成缺陷修复,合并hotfix分支到master和develop

mvn gitflow:support-start

开始支持分支,从tag列表中选择一个tag新建一个支持分支

后台命令

在交互命令上加参数 -B,由于gitflow非官方插件,所以需要在setting.xml里增加如下配置:

settings.xml
<pluginGroups>
    <pluginGroup>com.amashchenko.maven.plugin</pluginGroup>
</pluginGroups>

下面几个示例是以不改settings.xml的情况下的执行命令。

release.sh
mvn com.amashchenko.maven.plugin:gitflow-maven-plugin:release -B
hotfix-start.sh
git pull
mvn -B -DpushRemote=true com.amashchenko.maven.plugin:gitflow-maven-plugin:hotfix-start
# branch_name=`git symbolic-ref --short -q HEAD`
# git push -u origin $branch_name
hotfix-finish.sh
mvn -B -DhotfixVersion=1.8.1 com.amashchenko.maven.plugin:gitflow-maven-plugin:hotfix-finish
Note