Skip to content

Commit 2f7a538

Browse files
committed
git-commit-id/git-commit-id-plugin-core#247 / #136 / #535: Add support for perModuleVersions
1 parent 6731f5b commit 2f7a538

File tree

5 files changed

+138
-2
lines changed

5 files changed

+138
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<dependency>
6464
<groupId>${project.groupId}</groupId>
6565
<artifactId>git-commit-id-plugin-core</artifactId>
66-
<version>6.0.0</version>
66+
<version>6.1.5</version>
6767
</dependency>
6868
<dependency>
6969
<groupId>com.google.code.findbugs</groupId>

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ public class GitCommitIdMojo extends AbstractMojo {
130130
@Parameter(defaultValue = "false")
131131
boolean injectAllReactorProjects;
132132

133+
/**
134+
* Configuration to enable per-module version generation.
135+
* When enabled, the plugin will only consider commits affecting paths within the current module.
136+
*
137+
* <p>By default this option is disabled (set to {@code false})
138+
*
139+
* <p>This is useful in multi-module builds where you want to track module-specific changes
140+
* rather than the entire repository's commit history.
141+
*
142+
* <p>Example:
143+
*
144+
* <pre>{@code
145+
* <perModuleVersions>true</perModuleVersions>
146+
* }</pre>
147+
*
148+
* See
149+
* - https://github.com/git-commit-id/git-commit-id-plugin-core/pull/247
150+
* - https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/136
151+
* - https://github.com/git-commit-id/git-commit-id-maven-plugin/pull/535
152+
*
153+
* @since 9.1.0
154+
*/
155+
@Parameter(defaultValue = "false")
156+
boolean perModuleVersions;
157+
133158
/**
134159
* Configuration to tell the git-commit-id-maven-plugin to print some more verbose information
135160
* during the build (e.g. a summary of all collected properties when it's done).
@@ -1404,6 +1429,11 @@ public boolean shouldPropertiesEscapeUnicode() {
14041429
public boolean shouldFailOnNoGitDirectory() {
14051430
return failOnNoGitDirectory;
14061431
}
1432+
1433+
@Override
1434+
public boolean isPerModuleVersions() {
1435+
return perModuleVersions;
1436+
}
14071437
};
14081438

14091439
GitCommitIdPlugin.runPlugin(cb, properties);

src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ public enum AvailableGitTestRepo {
124124
*/
125125
WITH_THREE_COMMITS_AND_TWO_TAGS_CURRENTLY_ON_COMMIT_WITHOUT_TAG(
126126
"src/test/resources/_git_three_commits_and_two_tags_currently_on_commit_without_tag"),
127+
/**
128+
* <pre>
129+
* $ git log --name-only --pretty=format:"%H '%an' '%aD' '%s' %d" --date=short
130+
* 2ed2ea209fb99c360cd8434eb2d82b929da6b908 'TheSnoozer' 'Fri, 27 Mar 2026 17:40:36 +0100' 'a change in the root pom' (HEAD -> master)
131+
* pom.xml
132+
*
133+
* 70a13b95591dac76ce92dd9087d557fca539f98a 'submodule-two Author' 'Fri, 27 Mar 2026 17:39:59 +0100' 'a change in submodule-two' (tag: tag-submodule-two)
134+
* submodule-two/pom.xml
135+
*
136+
* 91e49245092c089624d3e770d902cfc8bc53a852 'submodule-one Author' 'Fri, 27 Mar 2026 17:39:23 +0100' 'a change in submodule-one' (tag: tag-submodule-one)
137+
* submodule-one/pom.xml
138+
*
139+
* 9c5d2e13d042b0acb71c48232a9c408e42da87f7 'TheSnoozer' 'Fri, 27 Mar 2026 17:38:16 +0100' 'new repo for testing (based on git-commit-id-maven-debugging)'
140+
* [snip]
141+
* </pre>
142+
* and dirty:
143+
* <pre>
144+
* $ git status -s
145+
* M submodule-two/pom.xml
146+
* </pre>
147+
*/
148+
WITH_SUBMODULES_AND_MULTIPLE_COMMITS("src/test/resources/_git_with_submodules_and_multiple_commits"),
127149
// TODO: Why do the tests get stuck when we use .git??
128150
MAVEN_GIT_COMMIT_ID_PLUGIN("src/test/resources/_git_one_commit_with_umlaut");
129151

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,90 @@ public void shouldGeneratePropertiesWithMultiplePrefixesAndReactorProject(boolea
17161716
}
17171717
}
17181718

1719+
static Stream<Arguments> useNativeGitWithSubmoduleName() {
1720+
return useNativeGit().flatMap(arg ->
1721+
Stream.of(
1722+
"submodule-one",
1723+
"submodule-two"
1724+
).map(str ->
1725+
Arguments.of(arg.get()[0], str)
1726+
)
1727+
);
1728+
}
1729+
1730+
@ParameterizedTest
1731+
@MethodSource("useNativeGitWithSubmoduleName")
1732+
public void shouldGiveCommitIdForEachFolderWhenPerModuleVersionsEnabled(boolean useNativeGit, String submoduleName) throws Exception {
1733+
// given
1734+
mavenSandbox
1735+
.withParentProject("my-pom-project", "pom")
1736+
.withGitRepoInParent(AvailableGitTestRepo.WITH_SUBMODULES_AND_MULTIPLE_COMMITS)
1737+
.withChildProject(submoduleName, "jar")
1738+
.create();
1739+
MavenProject targetProject = mavenSandbox.getChildProject();
1740+
1741+
setProjectToExecuteMojoIn(targetProject);
1742+
1743+
GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(false, 7);
1744+
1745+
mojo.gitDescribe = gitDescribeConfig;
1746+
mojo.useNativeGit = useNativeGit;
1747+
mojo.perModuleVersions = true;
1748+
mojo.dateFormatTimeZone = "CET";
1749+
1750+
// when
1751+
mojo.execute();
1752+
// then
1753+
Properties properties = targetProject.getProperties();
1754+
assertGitPropertiesPresentInProject(properties);
1755+
1756+
// setup expectations
1757+
Map<String, Object> expectedValues = new HashMap<>();
1758+
expectedValues.put("git.commit.id.full", null);
1759+
expectedValues.put("git.closest.tag.name", "tag-" + submoduleName);
1760+
expectedValues.put("git.closest.tag.commit.count", null);
1761+
expectedValues.put("git.dirty", null);
1762+
expectedValues.put("git.commit.message.full", "a change in " + submoduleName);
1763+
expectedValues.put("git.commit.user.name", submoduleName + " Author");
1764+
expectedValues.put("git.commit.user.email", submoduleName + "@example.com");
1765+
expectedValues.put("git.commit.time", null);
1766+
expectedValues.put("git.commit.author.time", null);
1767+
expectedValues.put("git.commit.committer.time", null);
1768+
1769+
if (submoduleName.equals("submodule-one")) {
1770+
expectedValues.put("git.commit.id.full", "91e49245092c089624d3e770d902cfc8bc53a852");
1771+
expectedValues.put("git.closest.tag.commit.count", 0);
1772+
expectedValues.put("git.dirty", true); // Really?
1773+
// date -d @$(git log -1 --pretty=format:%ct 91e4924) "+%Y-%m-%dT%H:%M:%S%z"
1774+
expectedValues.put("git.commit.time", "2026-03-28T11:47:51+0100");
1775+
// date -d @$(git log -1 --pretty=format:%at 91e4924) "+%Y-%m-%dT%H:%M:%S%z"
1776+
expectedValues.put("git.commit.author.time", "2026-03-27T17:39:23+0100");
1777+
// date -d @$(git log -1 --pretty=format:%ct 91e4924) "+%Y-%m-%dT%H:%M:%S%z"
1778+
expectedValues.put("git.commit.committer.time", "2026-03-28T11:47:51+0100");
1779+
} else if (submoduleName.equals("submodule-two")) {
1780+
expectedValues.put("git.commit.id.full", "70a13b95591dac76ce92dd9087d557fca539f98a");
1781+
expectedValues.put("git.closest.tag.commit.count", 0);
1782+
expectedValues.put("git.dirty", true);
1783+
// date -d @$(git log -1 --pretty=format:%ct 70a13b9) "+%Y-%m-%dT%H:%M:%S%z"
1784+
expectedValues.put("git.commit.time", "2026-03-28T11:48:17+0100");
1785+
// date -d @$(git log -1 --pretty=format:%at 70a13b9) "+%Y-%m-%dT%H:%M:%S%z"
1786+
expectedValues.put("git.commit.author.time", "2026-03-27T17:39:59+0100");
1787+
// date -d @$(git log -1 --pretty=format:%ct 70a13b9) "+%Y-%m-%dT%H:%M:%S%z"
1788+
expectedValues.put("git.commit.committer.time", "2026-03-28T11:48:17+0100");
1789+
}
1790+
1791+
// Assertions
1792+
for (Map.Entry<String, Object> entry : expectedValues.entrySet()) {
1793+
String key = entry.getKey();
1794+
Object expectedValue = entry.getValue();
1795+
1796+
assertThat(expectedValue).isNotNull();
1797+
assertThat(properties.getProperty(key))
1798+
.as("useNativeGit=%s,submoduleName=%s,key=%s", useNativeGit, submoduleName, key)
1799+
.isEqualTo(String.valueOf(expectedValue));
1800+
}
1801+
}
1802+
17191803
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {
17201804
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
17211805
gitDescribeConfig.setTags(true);

src/test/resources

Submodule resources updated 38 files

0 commit comments

Comments
 (0)