git-commit-id / git-commit-id-maven-plugin

Maven plugin which includes build-time git repository information into an POJO / *.properties). Make your apps tell you which version exactly they were built from! Priceless in large distributed deployments... :-)
http://www.kto.so
GNU Lesser General Public License v3.0
1.62k stars 299 forks source link

Can't use generated properties when building maven #601

Closed qianlong closed 1 year ago

qianlong commented 1 year ago

The properties generated by this plugin can't be used directly as those declared in <properties>.

Steps to Reproduce

<plugin>
    <groupId>io.github.git-commit-id</groupId>
    <artifactId>git-commit-id-maven-plugin</artifactId>
    <version>5.0.0</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <includeOnlyProperties>
            <includeOnlyProperty>^git.branch.*$</includeOnlyProperty>
        </includeOnlyProperties>
        <injectAllReactorProjects>true</injectAllReactorProjects>
        <replacementProperties>
            <replacementProperty>
                <property>git.branch</property>
                <propertyOutputSuffix>version</propertyOutputSuffix>
                <token>^(feature/.+|dev)$</token>
                <value>dev</value>
            </replacementProperty>
        </replacementProperties>
    </configuration>
</plugin>

Expected behavior

TheSnoozer commented 1 year ago

Mhh tested this locally and the plugin is generating the property git.branch.version as expected.

For debugging I'd suggest to add <verbose>true</verbose> to the configuration of the plugin.

I'd almost guess that the project where the plugin is running is pom project (<packaging>pom</packaging>). For that the plugin is skipped by default:

[INFO] --- git-commit-id-maven-plugin:5.0.0:revision (get-the-git-infos) @ git-commit-id-plugin-debugging ---
[INFO] isPomProject is true and skipPoms is true, return

Here is what I tested for reference:

            <plugin>
                <groupId>io.github.git-commit-id</groupId>
                <artifactId>git-commit-id-maven-plugin</artifactId>
                <version>5.0.0</version>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                        <phase>initialize</phase>
                    </execution>
                </executions>
                <configuration>
                    <verbose>true</verbose>
                    <skipPoms>false</skipPoms>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                    <includeOnlyProperties>
                        <includeOnlyProperty>^git.branch.*$</includeOnlyProperty>
                    </includeOnlyProperties>
                    <injectAllReactorProjects>true</injectAllReactorProjects>
                    <replacementProperties>
                        <replacementProperty>
                            <property>git.branch</property>
                            <propertyOutputSuffix>version</propertyOutputSuffix>
                            <token>^(feature/.+|dev)$</token>
                            <value>dev</value>
                        </replacementProperty>
                    </replacementProperties>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <echo>===========================================================================</echo>
                                <echo>git.branch.version: ${git.branch.version}</echo>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

generates

[INFO] --- git-commit-id-maven-plugin:5.0.0:revision (get-the-git-infos) @ git-commit-id-plugin-debugging ---
[INFO] dotGitDirectory /workspace/git-commit-id/git-commit-id-debugging/.git
[INFO] Collected git.branch with value master
[INFO] Added properties to 3 projects
[INFO] apply replace on property git.branch and save to git.branch.version: original value 'master' with 'master'
[INFO] including property git.branch in results
[INFO] including property git.branch.version in results
[INFO] Writing properties file to [/workspace/git-commit-id/git-commit-id-debugging/target/classes/git.properties] (for module Git Commit Id Plugin Maven Mojo Debugging)...
[INFO] Added properties to 3 projects
[INFO] 
[INFO] --- maven-antrun-plugin:1.8:run (default) @ git-commit-id-plugin-debugging ---
[INFO] Executing tasks

main:
     [echo] ===========================================================================
     [echo] git.branch.version: master
qianlong commented 1 year ago

Thanks for your response, the property is generated as expected, but can't be used in project and dependencies' version. The packaging is not pom. What I was trying to do is to run mvn install and mvn deploy with generated version. But I've got an error when running mvn deploy:

'dependencies.dependency.version' for test.group:test1:jar must be a valid version but is '${git.branch.version}-SNAPSHOT'


<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>test.group</groupId>
<artifactId>test2</artifactId>
<version>${revision}-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <dependency.version>${git.branch.version}-SNAPSHOT</dependency.version>
    <revision>${git.branch.version}</revision>
</properties>

<dependencies>
    <dependency>
        <groupId>test.group</groupId>
        <artifactId>test1</artifactId>
        <version>${dependency.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
            <version>5.0.0</version>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                    <phase>initialize</phase>
                </execution>
            </executions>
            <configuration>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                <includeOnlyProperties>
                    <includeOnlyProperty>^git.branch.*$</includeOnlyProperty>
                </includeOnlyProperties>
                <forceValueEvaluation>true</forceValueEvaluation>
                <injectIntoSysProperties>true</injectIntoSysProperties>
                <replacementProperties>
                    <replacementProperty>
                        <property>git.branch</property>
                        <propertyOutputSuffix>version</propertyOutputSuffix>
                        <token>^(feature/.+|dev)$</token>
                        <value>dev</value>
                    </replacementProperty>
                    <replacementProperty>
                        <property>git.branch</property>
                        <propertyOutputSuffix>version</propertyOutputSuffix>
                        <token>master</token>
                        <value>uat</value>
                        <regex>false</regex>
                    </replacementProperty>
                </replacementProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

TheSnoozer commented 1 year ago

This is unsupported: https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/master/docs/faq.md#generated-properties-are-not-being-used-in-install-andor-deploy

Attempt to use the workarounds suggested, or solutions like generating the pom on the fly with the flatten-maven-plugin.

See also https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/256#issuecomment-376416054 et. al.