Maven项目配置生成由Jenkins Git插件设置的简短格式的Git提交ID?

时间:2018-02-08 14:43:21

标签: git maven jenkins

Jenkins Git插件使用Git commit SHA1 id设置GIT_COMMIT环境变量,长度为40个字符。因为我想使用Git commit id作为我的版本字符串的一部分,我需要将它缩短为6个字符(例如1.0.0-d5e8dc)。 Maven捕获GIT_COMMIT环境变量,我不知何故应该在Maven中缩短它。

1 个答案:

答案 0 :(得分:0)

正确表达的

build-helper-maven-plugin为我工作:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <id>regex-property</id>
          <goals>
            <goal>regex-property</goal>
          </goals>
          <configuration>
            <!-- 
              This property will have short Git commit id. 
            -->
            <name>short-git-commit-id</name>
            <!-- 
              Jenkins Git Plugin sets GIT_COMMIT environment
              variable which is 40 characters long SHA1 value.
            -->
            <value>${GIT_COMMIT}</value>
            <!-- 
              To get the first 6 characters of Git commit id, we 
              cut last 40-6=34 characters off. 
            --> 
            <regex>.{34}$</regex>
            <replacement></replacement>
            <failIfNoMatch>true</failIfNoMatch>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>