Skip to content

Commit

Permalink
only injects properties into every reactor project once
Browse files Browse the repository at this point in the history
Enabling `injectAllReactorProjects` in large and old reactor projects
allows to avoid repeatedly parsing the git history to retrieve the
information.

In the apache/james-project enabling this property allowed to lower the
time spent in git-commit-id from 39s[1] to 2s[2].

However, looking at the verbose logs one can see that the plugin keeps
injecting the properties into every reactor project for every project
where the plugin runs.

This commit aims to avoid reinjecting keys which have already been set
in the context.

[1] https://ge.apache.org/s/xumcl7ztpkmhw/timeline?collapse-all&outcome=success,failed&view=by-type
[2] https://ge.apache.org/s/gpnevfknmdv5a/timeline?collapse-all&hide-timeline&outcome=success,failed&view=by-type
  • Loading branch information
jeantil committed Feb 18, 2024
1 parent e7ec732 commit ee43e57
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,17 @@ public void error(String msg, Throwable t) {
commitIdPropertiesOutputFormat = CommitIdPropertiesOutputFormat.PROPERTIES;
}

Properties properties = null;
// check if properties have already been injected
Properties contextProperties = getContextProperties(project);
boolean alreadyInjected = injectAllReactorProjects && contextProperties != null;
if (alreadyInjected) {
log.info(
"injectAllReactorProjects is enabled - attempting to use the already computed values");
// makes sure the existing context properties are not mutated
properties = new Properties(contextProperties);
}

final GitCommitIdPlugin.Callback cb =
new GitCommitIdPlugin.Callback() {
@Override
Expand Down Expand Up @@ -1339,7 +1350,7 @@ public boolean shouldGenerateGitPropertiesFile() {

@Override
public void performPublishToAllSystemEnvironments(Properties properties) {
publishToAllSystemEnvironments(getLogInterface(), properties);
publishToAllSystemEnvironments(getLogInterface(), properties, contextProperties);
}

@Override
Expand Down Expand Up @@ -1393,29 +1404,27 @@ public boolean shouldPropertiesEscapeUnicode() {
}
};

Properties properties = null;
// check if properties have already been injected
Properties contextProperties = getContextProperties(project);
boolean alreadyInjected = injectAllReactorProjects && contextProperties != null;
if (alreadyInjected) {
log.info(
"injectAllReactorProjects is enabled - attempting to use the already computed values");
properties = contextProperties;
}

GitCommitIdPlugin.runPlugin(cb, properties);
} catch (GitCommitIdExecutionException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void publishToAllSystemEnvironments(LogInterface log, Properties propertiesToPublish) {
private void publishToAllSystemEnvironments(LogInterface log, Properties propertiesToPublish, Properties contextProperties) {
publishPropertiesInto(propertiesToPublish, project.getProperties());
// some plugins rely on the user properties (e.g. flatten-maven-plugin)
publishPropertiesInto(propertiesToPublish, session.getUserProperties());

if (injectAllReactorProjects) {
appendPropertiesToReactorProjects(log, propertiesToPublish);
Properties diffPropertiesToPublish = new Properties();
propertiesToPublish.forEach((k, v) -> {
if (!contextProperties.contains(k)) {
diffPropertiesToPublish.setProperty(k.toString(), v.toString());
}
});
if(!diffPropertiesToPublish.isEmpty()) {
appendPropertiesToReactorProjects(log, diffPropertiesToPublish);
}
}

if (injectIntoSysProperties) {
Expand Down Expand Up @@ -1479,7 +1488,7 @@ private void publishPropertiesInto(Properties propertiesToPublish, Properties pr
private void appendPropertiesToReactorProjects(LogInterface log, Properties propertiesToPublish) {
for (MavenProject mavenProject : reactorProjects) {
log.debug("Adding properties to project: '" + mavenProject.getName() + "'");

if(mavenProject.equals(project)) continue;
publishPropertiesInto(propertiesToPublish, mavenProject.getProperties());
mavenProject.setContextValue(CONTEXT_KEY, propertiesToPublish);
}
Expand Down

0 comments on commit ee43e57

Please sign in to comment.