java – Using git-commit-id-plugin in maven works in package-phase but not in install-phase

java – Using git-commit-id-plugin in maven works in package-phase but not in install-phase

To incorporate git-commit-id plugin into the version number for the entire maven build cycle (till deploy)

<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>

Also refer to this to work around the plugin execution not covered by lifecycle error in Eclipse/SpringToolSuite

see: How to solve Plugin execution not covered by lifecycle configuration for Spring Data Maven Builds

Ive found a solution that worked fine for me. Just added the gmaven-plugin like in the pom-snippet below and the versions will be adapted to the last 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>

java – Using git-commit-id-plugin in maven works in package-phase but not in install-phase

Ive also used groovy-maven-plugin to update project.properties. Without this pom.xml was generated with variables that have not been parsed.

project properties

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

build final name atteribute

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

plugin definition

<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>

Related posts

Leave a Reply

Your email address will not be published.