Search code examples
gitpom.xmlmaven-3

Unable to run git-commit-id-plugin once and use in child projects


In the root of my project is pom.xml and .git. My maven pom.xml looks like:

<profile>
      <id>git</id>
      <activation>
        <file>
          <exists>.git</exists>
        </file>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
            <version>7.0.0</version>
            <executions>
              <execution>
                <id>get-the-git-infos</id>
                <goals>
                  <goal>revision</goal>
                </goals>
                <phase>validate</phase>
              </execution>
            </executions>
            <configuration>
              <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

From the console output, I can see that it is run once this way, but child jars that attempt to set it in the manifest end up having: '${git.commit.id.abbrev}' verbatim.

While I could just run this each time for each project, I'd like to run it once.

Is that possible?


Solution

  • Using a profile is the wrong way. The plugin will handle all the cases required. Also setting dotGitDirectory is not necessary nor useful that is the default of the plugin. That violates the convention over configuration paradigm.

    First define the version and the basic configuration of the plugin via pluginManagement like this:

        <plugin>
          <groupId>io.github.git-commit-id</groupId>
          <artifactId>git-commit-id-maven-plugin</artifactId>
          <version>7.0.0</version>
          <configuration>
            <runOnlyOnce>true</runOnlyOnce>
            <generateGitPropertiesFile>true</generateGitPropertiesFile>
            <commitIdGenerationMode>full</commitIdGenerationMode>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    

    some of the configurations parts might be changed if required generateGitPropertiesFile maybe?

    The part runOnlyOnce is an important one within a multi module build which means the plugin itself will execute only once to read the Git information and only at the root level (where the .git directory can be found). If you don't set that to true it will try to read the Git information in each child which is not necessary (that works but it's a wast of time; only in case where you might use Git submodules/subtrees it might be required).

    The next important thing is to bind the plugin to the life cycle. That has to be done in the plugins section (NOT in pluginManagement) like this:

    <build>
      <plugins>
        ..
        <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>revision</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugin>
        ...
      </plugins>
    </build>
    

    The plugin itself binds by default to the initialize phase so it is not required to define the phase on your own.