首页 > 解决方案 > maven git-commit-id-plugin:子文件夹/子模块的commitId

问题描述

我正在使用maven-git-commit-id-pluginmaven 插件来获取诸如 commitId 之类的数据。我的 maven 项目在子文件夹中有一些子模块,如下所示:

hfe/pom.xml -
            - backend/pom.xml
            - frontend/pom.xml

hfe是 git-checkout,所以存在 .git 文件夹。

hfe/pom.xml构建部分中,我添加了它,maven-git-commit-id-plugin以便它将在每个子模块中执行。

如果我现在mvn package在文件夹中执行 a hfe/backend/,我将在生成的 git-property-file 中获得整个项目的最新 commitId。是否可以获取子文件夹 hfe/backend 的最新 commitId。

为了清楚起见,我想在文件 hfe/backend/target/git.properties 中有commitId 90791bcf ...,但在我得到commitId d022a39342 ...的那一刻:

/projects/hfe>git log -n 1 backend
commit 90791bcf145ee635c61f25c0ac62d0d66c49307f
Author: me
Date:   Fri May 10 12:30:31 2019 +0200

/projects/hfe>git log -n 1
commit d022a39342ecd6bcedeafbaf4bd80db495fdf23c (HEAD -> master, origin/master, origin/HEAD)
Author: other
Date:   Sun May 12 21:58:50 2019 +0200

也许还有另一个 Maven 插件可以做到这一点?

标签: maven

解决方案


检查此git-commit-id/maven-git-commit-id-plugin问题 137是否有帮助:

  • 使用不同的 git 项目多次运行插件而不覆盖已经生成的属性。
  • 使用 Git 子模块 SHA1 而不是父项目

来自TheSnoozer

<!-- GIT COMMIT ID PLUGIN CONFIGURATION -->
<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <id>get-the-git-infos-for-repository-one</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <configuration>
                                <prefix>git1</prefix>
                <dotGitDirectory>pathToRepositoryOne/.git</dotGitDirectory>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/repository_one_git.properties</generateGitPropertiesFilename>
            </configuration>
        </execution>
        <execution>
            <phase>initialize</phase>
            <id>get-the-git-infos-for-repository-two</id>
            <goals>
                <goal>revision</goal>
            </goals>
            <configuration>
                                <prefix>git2</prefix>
                <dotGitDirectory>pathToRepositoryTwo/.git</dotGitDirectory>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/repository_two_git.properties</generateGitPropertiesFilename>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <verbose>false</verbose>
        <skipPoms>false</skipPoms>
        <injectAllReactorProjects>true</injectAllReactorProjects>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
    </configuration>
</plugin>

推荐阅读