diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index a1e15a54..a191fed2 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -48,6 +48,7 @@ import com.google.common.io.Files; import java.io.OutputStream; + import pl.project13.maven.git.build.BuildServerDataProvider; import pl.project13.maven.git.log.LoggerBridge; import pl.project13.maven.git.log.MavenLoggerBridge; @@ -63,6 +64,8 @@ */ @Mojo(name = "revision", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) public class GitCommitIdMojo extends AbstractMojo { + private static final String CONTEXT_KEY = GitCommitIdMojo.class.getName() + ".properties"; + /** * The Maven Project. */ @@ -324,7 +327,7 @@ public class GitCommitIdMojo extends AbstractMojo { */ @Parameter(defaultValue = "30000") long nativeGitTimeoutInMs; - + /** * Use branch name from build environment. Set to {@code 'false'} to use JGit/GIT to get current branch name. * Useful when using the JGitflow maven plugin. @@ -333,7 +336,7 @@ public class GitCommitIdMojo extends AbstractMojo { */ @Parameter(defaultValue = "true") boolean useBranchNameFromBuildEnvironment; - + /** * Injected {@link BuildContext} to recognize incremental builds. */ @@ -432,20 +435,31 @@ public void execute() throws MojoExecutionException { String trimmedPrefix = prefix.trim(); prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + "."; - loadGitData(properties); - loadBuildData(properties); - propertiesReplacer.performReplacement(properties, replacementProperties); - propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot); - propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot); + // check if properties have already been injected + Properties contextProperties = getContextProperties(project); + boolean alreadyInjected = injectAllReactorProjects && contextProperties != null; + if (alreadyInjected) { + log.info("injectAllReactorProjects is enabled and this project already contains properties"); + properties = contextProperties; + } else { + loadGitData(properties); + loadBuildData(properties); + propertiesReplacer.performReplacement(properties, replacementProperties); + propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot); + propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot); + } logProperties(); if (generateGitPropertiesFile) { maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename); } - publishPropertiesInto(project); - if (injectAllReactorProjects) { - appendPropertiesToReactorProjects(); + if (!alreadyInjected) { + publishPropertiesInto(project); + + if (injectAllReactorProjects) { + appendPropertiesToReactorProjects(); + } } } catch (Exception e) { handlePluginFailure(e); @@ -492,6 +506,7 @@ private void appendPropertiesToReactorProjects() { log.info("{}] project {}", mavenProject.getName(), mavenProject.getName()); publishPropertiesInto(mavenProject); + mavenProject.setContextValue(CONTEXT_KEY, properties); } } @@ -609,11 +624,11 @@ private void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fi } catch (final IOException ex) { throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex); } - + if (buildContext != null) { buildContext.refresh(gitPropsFile); } - + } else { log.info("Properties file [{}] is up-to-date (for module {})...", gitPropsFile.getAbsolutePath(), project.getName()); } @@ -678,6 +693,10 @@ private Properties readProperties(@Nonnull File propertiesFile) throws CannotRea } } + private Properties getContextProperties(MavenProject project) { + return (Properties) project.getContextValue(CONTEXT_KEY); + } + static class CannotReadFileException extends Exception { private static final long serialVersionUID = -6290782570018307756L;