diff --git a/pom.xml b/pom.xml index 78b44e67..94d470c6 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ ${project.groupId} git-commit-id-plugin-core - 6.0.0 + 6.0.1-SNAPSHOT com.google.code.findbugs diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index ce58650a..3b8fb414 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -284,6 +284,14 @@ public class GitCommitIdMojo extends AbstractMojo { @Parameter(defaultValue = "${project.basedir}/.git") File dotGitDirectory; + /** + * Configuration to tell the git-commit-id-maven-plugin to consider only + * commits affecting the folder containing this module, rather than all + * commits in the repository. + */ + @Parameter(defaultValue = "false") + boolean enablePerModuleVersions; + /** * Configuration for the {@code 'git-describe'} command. You can modify the dirty marker, abbrev * length and other options here. The following `gitDescribe` configuration below is optional and @@ -1325,6 +1333,11 @@ public boolean getUseBranchNameFromBuildEnvironment() { return useBranchNameFromBuildEnvironment; } + @Override + public boolean getPerModuleVersions() { + return enablePerModuleVersions; + } + @Override public boolean isOffline() { return offline || settings.isOffline(); @@ -1404,6 +1417,11 @@ public boolean shouldPropertiesEscapeUnicode() { public boolean shouldFailOnNoGitDirectory() { return failOnNoGitDirectory; } + + @Override + public File getModuleBaseDir() { + return new File(project.getBasedir().getAbsolutePath()); + } }; GitCommitIdPlugin.runPlugin(cb, properties); diff --git a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java b/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java index 4adecf35..a5d016eb 100644 --- a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java +++ b/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java @@ -39,6 +39,7 @@ import org.codehaus.plexus.util.FileUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.ResetCommand; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -1716,6 +1717,48 @@ public void shouldGeneratePropertiesWithMultiplePrefixesAndReactorProject(boolea } } + @Test + public void shouldGiveCommitIdForEachFolderWhenPerModuleVersionsEnabled() + throws Exception { + // given + mavenSandbox + .withParentProject("parent-project", "pom") + .withChildProject("src/test", "jar") + .withGitRepoInParent(AvailableGitTestRepo.GIT_COMMIT_ID) + .withKeepSandboxWhenFinishedTest(true) + .create(); + + // Only supported with JGit + mojo.useNativeGit = false; + + // Don't skip the parent project + mojo.skipPoms = false; + + //Enable per module versions + mojo.enablePerModuleVersions = true; + + MavenProject parentProject = mavenSandbox.getParentProject(); // "my-pom-project" + MavenProject childProject = mavenSandbox.getChildProject(); // "my-child-module" + + // when + // Execute the mojo in both parent and child projects + setProjectToExecuteMojoIn(parentProject); + mojo.execute(); + + setProjectToExecuteMojoIn(childProject); + mojo.execute(); + + // then + // The commit IDs should be different for parent and child projects + Properties parentProperties = parentProject.getProperties(); + Properties childProperties = childProject.getProperties(); + + assertThat(parentProperties).containsKey("git.commit.id.abbrev"); + assertThat(childProperties).containsKey("git.commit.id.abbrev"); + assertThat(parentProperties.getProperty("git.commit.id.abbrev")).isNotEqualTo(childProperties.getProperty("git.commit.id.abbrev")); + } + + private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) { GitDescribeConfig gitDescribeConfig = new GitDescribeConfig(); gitDescribeConfig.setTags(true);