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.63k stars 300 forks source link

git.branch not working #409

Closed wtmtgmgxj closed 5 years ago

wtmtgmgxj commented 5 years ago

I have included the plugin as shown , but its not working. Please note that I need Git Branch in Version Tag.

<?xml version="1.0" encoding="UTF-8"?>
<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.bank.upi.jobs</groupId>
    <artifactId>upijobs</artifactId>
    <version>1-${git.branch}</version>
    <packaging>jar</packaging>

    <name>upi-jobs</name>
    <description>UPI Jobs</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-kafka.version>2.1.0.RELEASE</spring-kafka.version>
        <guava.version>23.6-jre</guava.version>
        <spring.version>5.0.2.RELEASE</spring.version>
        <jackson.version>2.9.3</jackson.version>
        <powermock.version>1.6.4</powermock.version>
        <git-commit.version>2.2.3</git-commit.version>
        <spring.boot.version>1.5.10.RELEASE</spring.boot.version>
        <quartz.version>2.2.1</quartz.version>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss.mmm</maven.build.timestamp.format>
        <lmax.version>3.3.6</lmax.version>
        <lombok.version>1.16.18</lombok.version>
        <start-class>com.paytm.bank.upi.jobs.UpiJobsApplication</start-class>
        <main-class>com.paytm.bank.upi.jobs.UpiJobsApplication</main-class>
        <spring-kafka.version>2.1.0.RELEASE</spring-kafka.version>
        <upi.commons.version>0.0.26-RELEASE</upi.commons.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>${lmax.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>${quartz.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <!--<version>1.9.33</version>-->
            <version>1.11.136</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.7.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>${spring-kafka.version}</version>
        </dependency>

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>

        <!-- Elasticsearch Dependencies -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client-sniffer</artifactId>
            <version>6.1.2</version>
        </dependency>

        <!-- spring-kafka -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>${spring-kafka.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.datadoghq</groupId>
            <artifactId>java-dogstatsd-client</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <!-- spring-boot-maven-plugin -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.2.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <prefix>git</prefix>
                    <injectAllReactorProjects>true</injectAllReactorProjects>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Expected behavior

Here's My Console on running "maven clean install"

[INFO] --- git-commit-id-plugin:2.2.3:revision (default) @ upijobs ---
[INFO] dotGitDirectory /Users/Dipanshu/Desktop/Z_BANK/upi-jobs/.git
[INFO] git.build.user.name 
[INFO] git.build.user.email 
[INFO] git.branch ExternalTxnAndRefundWithRetry
[INFO] --always = true
[INFO] --dirty = -dirty
[INFO] --abbrev = 7
[INFO] Tag refs [[]]
[INFO] Created map: [{}]
[INFO] HEAD is [91c1598e21e732c11b96b52652596c2e8613d53f]
[INFO] git.commit.id.describe 91c1598-dirty
[INFO] git.commit.id 91c1598e21e732c11b96b52652596c2e8613d53f
[INFO] git.commit.id.abbrev 91c1598
[INFO] git.dirty true
[INFO] git.commit.user.name Dipanshu
[INFO] git.commit.user.email Dipanshu.local
[INFO] git.commit.message.full Refactored to a better job
[INFO] git.commit.message.short Refactored to a better job
[INFO] git.commit.time 2019-01-30T12:13:54+0530
[INFO] git.remote.origin.url git@bitbucket.org:paytmteam/upi-jobs.git
[INFO] git.tags 
[INFO] Tag refs [[]]
[INFO] git.closest.tag.name 
[INFO] Tag refs [[]]
[INFO] git.closest.tag.commit.count 
[INFO] git.build.time 2019-02-01T13:15:42+0530
[INFO] git.build.version 1-${git.branch}
[INFO] git.build.hos.local
[INFO] git.commit.id.describe-short 91c1598-dirty
[INFO] found property git.build.user.email
[INFO] found property git.build.host
[INFO] found property git.dirty
[INFO] found property git.remote.origin.url
[INFO] found property git.closest.tag.name
[INFO] found property git.commit.id.describe-short
[INFO] found property git.commit.user.email
[INFO] found property git.commit.time
[INFO] found property git.commit.message.full
[INFO] found property git.build.version
[INFO] found property git.commit.message.short
[INFO] found property git.commit.id.abbrev
[INFO] found property git.branch
[INFO] found property git.build.user.name
[INFO] found property git.closest.tag.commit.count
[INFO] found property git.commit.id.describe
[INFO] found property git.commit.id
[INFO] found property git.tags
[INFO] found property git.build.time
[INFO] found property git.commit.user.name

[INFO] Installing /Users/Dipanshu/Desktop/Z_BANK/pom.xml to /Users/Dipanshu/.m2/repository/com/******/upijobs/1-${git.branch}/upijobs-1-${git.branch}.pom

Please see the last line of the output, I was expecting ${git.branch} to be replaced by real branch name

Other configs-- Maven home: /usr/local/Cellar/maven/3.6.0/libexec Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"

TheSnoozer commented 5 years ago

Hi, thanks for reporting your issue. I believe this is a known issue outlined in the FAQ section (https://github.com/git-commit-id/maven-git-commit-id-plugin/blob/master/docs/faq.md#generated-properties-are-not-being-used-in-install-andor-deploy)

I'd refer to the full workaround linked in the docs.

Sadly there is nothing we could do differently on our side, since it's simply not intended by the maven eco system to have dynamic version numbers (hence the plugins need to be used with the workaround suggested).

wtmtgmgxj commented 5 years ago

I read and implemented the full workaround, however I was stuck in one case. I have a parent module which is pom packaging, whereas the subModules are jar packaging.

I have included the Maven Install Plugins and Maven Jar Plugins in all of my subModules, and they are working fine, installing jars with correct name. However when I run mvn clean install on the parent module, I get the following error. [ERROR] 'dependencies.dependency.version' for com.submodule:jar must be a valid version but is '${git.branch}'.

Do you have any hints on why this configuration is not working when building the parent pom

TheSnoozer commented 5 years ago

Sadly, I need to say: welcome to the pain of maven. The maven eco-system is essentially not designed to have version to be dynamic and I don't see anything we can do about it.

In your situation I'd also refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/377#issuecomment-413723156 that might help to fix your problem.

Maybe overall alternative solutions like generating the pom on the fly with https://www.mojohaus.org/flatten-maven-plugin/usage.html might be an interesting alternative. I'd guess there might be other plugins available to generate the pom with a dynamic version on the fly. So I'm thinking here in a direction where maven just sees a pom with a fixed version.

All together, I'd say dynamic versions are not really supported and hence I can't do anything besides referring to lengthy workarounds. Trust me, this pain was one of the reasons why I stopped using maven.

Hope this helps anyways.

rpajaziti commented 5 years ago

Hello @TheSnoozer . I have implemented the plugin, but git.branch returns the appropriate value(actual branch) for some projects, but it returns git.commit.id instead of the branch for some others. I did exactly the same on all maven projects..

TheSnoozer commented 5 years ago

Hi, thanks for the follow up.

Usually the branch can become the 'commit id' when the current repo is in a 'detached head' state. In such state the branch is not known from the perspective of git. CI Tools like Jenkins/Hudson/Travis will usually perform builds in a 'detached head' state, however in most cases they are configured to perform tests on a specific branch. To help to resole the 'right' branch anyways they also export the current branch as environment variable for the current build.

If you run this plugin in a CI environment, this plugin will check if a 'branch' environment variable is present. If not will attempt to resolve the branch with the power of git. If all of this does not lead to a specific branch, this plugin will return the commit-id instead (so from my perspective this is expected and documented behavior).

I don't know much about your current build procedure, but I'd suspect that the builds where git.branch will become git.commit.id are the builds where your repo is a detached head.

From the plugin side I'd refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/blob/master/docs/using-the-plugin.md#generated-properties

For more general info's on the 'git behaviour' I'd refer to https://git-scm.com/docs/git-checkout#_detached_head

TheSnoozer commented 5 years ago

Hi, I hopefully provided sufficient context why this is just a hell with maven and since this now seem to have gotten stale I think I can go ahead and close this specific issue. If there is any more feedback, please feel free to share it, since it might help someone else who bumps into the same issue.