From 0706969a31f9c130f5b680a6d2d93d4d5dec6ad1 Mon Sep 17 00:00:00 2001 From: Laurent Goujon Date: Mon, 25 Feb 2019 14:41:55 -0800 Subject: [PATCH] Skip git invocation if properties are already computed In a multi-modules project, one can set injectAllReactorProjects to true to add all the properties to each reactor's project, but there's no automated effort to skip git execution if properties are already set unless runOnlyOnce is set to true. But runOnce has two issues itself: - it assumes that the top-level project is included in the reactor - it skips the whole plugin execution To address this, detect during plugin execution if properties from a previous project in the reactor have been already computed and added to the current project's context, and skip git execution if this is the case. --- .../project13/maven/git/GitCommitIdMojo.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) 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;