在Maven中使用git-commit-id-plugin在打包阶段有效,但在安装阶段无效

时间:2018-08-14 09:50:23

标签: java git maven

我正在使用git-commit-id-plugin(请参阅https://github.com/ktoso/maven-git-commit-id-plugin)。当我设置带有注释的代码(例如)时,它可以正确打包v1.0.0,意味着目标目录具有一个名为deploy-test-Test-v1.0.0.jar的jar文件。 问题是,maven安装阶段在我的本地.m2目录中创建了以下文件:

Test-${git.closest.tag.name}
|-   deploy-test-Test-${git.closest.tag.name}.jar
|-    deploy-test-Test-${git.closest.tag.name}.pom
|-    _remote.repositories

我已经用示例pom.xml对此进行了测试。
如何获得相同的名称(deploy-test-Test-v1.0.0.jar)?

<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.mytest</groupId>
<artifactId>deploy-test</artifactId>
<packaging>jar</packaging>
<version>Test-${git.closest.tag.name}</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <revision>Test-${git.closest.tag.name}</revision>
</properties>
<dependencies/>
<build>
    <plugins>
        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>2.2.4</version>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                </execution>
                <execution>
                    <id>validate-the-git-infos</id>
                    <goals>
                        <goal>validateRevision</goal>
                    </goals>
                    <!-- *NOTE*: The default phase of validateRevision is verify, but in case you want to change it, you can do so by adding the phase here -->
                    <phase>package</phase>
                </execution>                    
            </executions>
            <configuration>
                <!-- If you'd like to tell the plugin where your .git directory is, use this setting, otherwise we'll perform a search trying to figure out the right directory. It's better to add it explicitly IMHO. -->
                <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                <prefix>git</prefix>
                <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
                <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
                <verbose>false</verbose>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                <format>properties</format>
                <skipPoms>true</skipPoms>
                <injectAllReactorProjects>false</injectAllReactorProjects>
                <failOnNoGitDirectory>true</failOnNoGitDirectory>
                <failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
                <skip>false</skip>
                <runOnlyOnce>false</runOnlyOnce>
                <useNativeGit>false</useNativeGit>
                <abbrevLength>7</abbrevLength>
                <commitIdGenerationMode>flat</commitIdGenerationMode>
                <gitDescribe>
                    <skip>false</skip>
                    <always>false</always>
                    <abbrev>7</abbrev>
                    <dirty>-dirty</dirty>
                    <match>*</match>
                    <tags>false</tags>
                    <forceLongFormat>false</forceLongFormat>
                </gitDescribe>
                <validationProperties>
                    <validationProperty>
                        <name>validating project version</name>
                        <value>${project.version}</value>
                        <shouldMatchTo>
                            <![CDATA[^.*(?<!-SNAPSHOT)$]]>
                        </shouldMatchTo>
                    </validationProperty>
                </validationProperties>
                <validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
                <evaluateOnCommit>HEAD</evaluateOnCommit>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

3 个答案:

答案 0 :(得分:0)

我找到了一个对我来说很好的解决方案。只需在下面的pom-snippet中添加gmaven-plugin,版本就会适应最后一个git-tag。

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
    <execution>
        <phase>install</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <source>project.artifact.version='${git.closest.tag.name}';</source>
        </configuration>
    </execution>
</executions>

答案 1 :(得分:0)

在整个Maven构建周期(直到部署)中将git-commit-id插件合并到版本号中

<properties>
  <java.version>1.8</java.version>
  <snapshot.string>-SNAPSHOT</snapshot.string>
  <!-- Snapshot Version Number -->
  <!-- <version.number>${git.commit.time}.${git.commit.id.abbrev}${s`enter code here`napshot.string}</version.number> -->
  <!-- Release Version Number -->
  <version.number>${git.commit.time}.${git.commit.id.abbrev}</version.number>
  <release.repo.key>libs-release-local</release.repo.key>
  <snapshot.repo.key>libs-snapshot-local</snapshot.repo.key>
  <artifactory.url>http://xxx.xxx.x.xxx:yyyy/artifactory</artifactory.url>
  <release.repository.url>${artifactory.url}/${release.repo.key}</release.repository.url>
  <snapshot.repository.url>${artifactory.url}/${snapshot.repo.key}</snapshot.repository.url>
</properties>


<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <groupId>pl.project13.maven</groupId>
      <artifactId>git-commit-id-plugin</artifactId>
      <executions>
        <execution>
          <phase>validate</phase>
          <goals>
            <goal>revision</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <dateFormat>yyyyMMdd.HHmmss</dateFormat>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <generateGitPropertiesFile>false</generateGitPropertiesFile>
        <injectAllReactorProjects>true</injectAllReactorProjects>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.gmavenplus</groupId>
      <artifactId>gmavenplus-plugin</artifactId>
      <version>1.6</version>
      <executions>
        <execution>
          <id>change-version</id>
          <phase>validate</phase>
          <goals>
            <goal>execute</goal>
          </goals>
          <configuration>
            <scripts>
              <script>
                < ![CDATA[
                  import org.apache.maven.artifact.versioning.VersionRange; git_revision = '${version.number}'
                  if (!project.properties['revision'] ? .trim()) {
                    println 'Change `version` to ' + git_revision
                    System.properties['revision'] = git_revision
                    project.properties['revision'] = git_revision
                    project.properties['project.version'] = git_revision
                    project.properties['git.build.version'] = git_revision
                    project.version = git_revision
                    project.artifact.version = git_revision
                    project.artifact.versionRange = VersionRange.createFromVersion(git_revision)
                  }
                ]] >
              </script>
            </scripts>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.codehaus.groovy</groupId>
          <artifactId>groovy-all</artifactId>
          <version>2.4.14</version>
          <scope>runtime</scope>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
<distributionManagement>
  <repository>
    <uniqueVersion>false</uniqueVersion>
    <id>release</id>
    <name>local-releases</name>
    <url>${release.repository.url}</url>
  </repository>
  <snapshotRepository>
    <uniqueVersion>true</uniqueVersion>
    <id>snapshots</id>
    <name>local-snapshots</name>
    <url>${snapshot.repository.url}</url>
  </snapshotRepository>
</distributionManagement>

也可以参考此方法来解决Eclipse / SpringToolSuite中的“生命周期未涵盖的插件执行”错误

请参阅:How to solve "Plugin execution not covered by lifecycle configuration" for Spring Data Maven Builds

答案 2 :(得分:0)

我还使用了groovy-maven-plugin更新project.properties。没有这个,pom.xml是用尚未解析的变量生成的。

项目属性

<properties>
    <gitClosestTagName>${git.closest.tag.name}</gitClosestTagName>
    <gitClosestTagCommitCount>${git.closest.tag.commit.count}</gitClosestTagCommitCount>
</properties>

建立最终姓名称谓

<finalName>${project.artifactId}-${gitClosestTagName}.${gitClosestTagCommitCount}</finalName>

插件定义

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
    <execution>
        <id>update-finalname</id>
        <phase>validate</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <source>
                project.build.finalName="${project.artifactId}-${gitClosestTagName}.${gitClosestTagCommitCount}";
                project.properties['gitClosestTagName']=${git.closest.tag.name};
                project.properties['gitClosestTagCommitCount']=${git.closest.tag.commit.count};
                println("project.build.finalName=${project.build.finalName}");
            </source>
        </configuration>
    </execution>
</executions>