Read last commit of the git and commit number

In maven project with Git source code, Is it possible whenever I am compiling a build with maven, to read the last commit of the git and commit number.

I want to use that commit number to be able to find the last commit.

  • “Unable to open database file” when trying to export a fossil repo to git
  • How can I resolve a permission denied error with git-remote-https?
  • GIT hook to prevent an experimental branch pushed to a release, or master branch
  • GIT Change Submodule URL in Post-update script
  • One big Git repo or multiple smaller ones
  • Why is this Bash function within a git alias executing twice, and why does adding `exit` fix it?
  • Is it suggested to have Git repository names in upper or camel case instead of lower case?
  • Eclipse Luna git checkout ssl fails
  • Convert git repo to submodule
  • have git track changes on a renamed file (with new file to complicate matters)
  • Only allow .git clone on local machine
  • How to create a repository with ssh:// access on a server
  • 2 Solutions collect form web for “Read last commit of the git and commit number”

    This is assuming you want to read that information, then store it in a property file.
    Based on https://github.com/ktoso/maven-git-commit-id-plugin#using-the-plugin:

    pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <!-- snip other stuff... -->
        <build>
            <!-- GIT COMMIT ID PLUGIN CONFIGURATION -->
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/*.properties</include>
                    </includes>
                </resource>
            </resources>
    
            <plugins>
                <plugin>
                    <groupId>pl.project13.maven</groupId>
                    <artifactId>git-commit-id-plugin</artifactId>
                    <version>2.2.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>revision</goal>
                             </goals>
                        </execution>
                    </executions>
    
                    <configuration>
                        <commitIdGenerationMode>flat</commitIdGenerationMode>
                        <gitDescribe>
                            <skip>true</skip>
                        </gitDescribe>
                    </configuration>
    
                </plugin>
                <!-- END OF GIT COMMIT ID PLUGIN CONFIGURATION -->
    
                <!-- other plugins -->
            </plugins>
        </build>
    </project>
    

    git.properties in /src/main/resources:

    git.commit.id=${git.commit.id}
    

    You can use maven-buildnumber-plugin, which supports Git among a few other SCM systems.

    It also has additional feature related to generating unique build number, besides just getting revision/commit ID: figure out SCM branch, add timestamps, use short hashes, etc.

    Git Baby is a git and github fan, let's start git clone.