Maven 项目获取 git 分支、提交等信息

git-commit-id-plugin 是一个 Maven 插件,用于在 Maven 项目的构建过程中自动获取 git 仓库的信息,如最后一次提交的 ID、分支名称、构建时间等,并将这些信息注入到项目的属性文件中。这对于跟踪项目版本和构建状态非常有用。

以下是如何在 Maven 项目中使用 git-commit-id-plugin 的基本步骤:

  1. 添加插件到 pom.xml 文件中:

    在你的 Maven 项目的 pom.xml 文件中,添加 git-commit-id-plugin<plugins> 部分。

    xml 复制代码
    <build>
        <plugins>
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>4.0.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 配置选项 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
  2. 配置插件:

    <configuration> 标签中,你可以配置多种选项,例如生成的属性文件的位置、包含哪些 git 属性等。默认情况下,插件会生成一个 git.properties 文件在 target/classes 目录下。

    xml 复制代码
    <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <format>properties</format>
    </configuration>
  3. 构建项目:

    使用 Maven 命令构建项目,例如 mvn clean install。在构建过程中,git-commit-id-plugin 插件会自动运行,并在指定的位置生成包含 git 信息的 git.properties 文件。

  4. 查看 git.properties 文件:

    打开生成的 git.properties 文件,你将看到类似以下的内容,其中包含了提交时间、提交记录、分支等信息:

    复制代码
    git.branch=main
    git.commit.id=abcdef1234567890
    git.commit.id.abbrev=abcdef1
    git.commit.time=2021-05-20T12:34:56Z
    git.commit.message.full=Initial commit
    git.commit.message.short=Initial commit
    git.commit.user.name=Your Name
    git.commit.user.email=your.email@example.com
  5. 在应用中使用这些信息:

    你可以在应用程序中读取 git.properties 文件,并使用这些信息,例如显示当前版本的 Git 分支和提交 ID。

通过这种方式,你可以利用 git-commit-id-plugin 插件来自动获取和使用 Git 的提交时间、提交记录、分支等信息,从而帮助跟踪和管理你的项目版本。

相关推荐
桦说编程5 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅7 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者8 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺8 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart9 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP10 小时前
MyBatis-mybatis入门与增删改查
java
孟陬13 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌13 小时前
一站式了解四种限流算法
java·后端·go
华仔啊14 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java