From a556257141dd1eac12ec357b61148f43798d2ff6 Mon Sep 17 00:00:00 2001 From: TheSnoozer Date: Sat, 12 Oct 2019 22:00:50 +0200 Subject: [PATCH 1/2] small refactoring to understand the code better --- .../maven/git/PropertiesReplacer.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java b/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java index d1161669..ba79f7e5 100644 --- a/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java +++ b/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java @@ -32,42 +32,54 @@ public PropertiesReplacer(LoggerBridge log) { this.log = log; } + /** + * @param properties all properties that are being generated by the plugin + * @param replacementProperties list of all replacement actions to perform + */ public void performReplacement(Properties properties, List replacementProperties) { if ((replacementProperties != null) && (properties != null)) { for (ReplacementProperty replacementProperty: replacementProperties) { String propertyKey = replacementProperty.getProperty(); if (propertyKey == null) { - Map propertiesToBeAdded = new HashMap<>(); - for (Map.Entry entry : properties.entrySet()) { - String key = (String)entry.getKey(); - String content = (String)entry.getValue(); - String result = performReplacement(replacementProperty, content); - if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { - String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix(); - propertiesToBeAdded.put(newPropertyKey, result); - log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); - } else { - entry.setValue(result); - log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'"); - } - } - properties.putAll(propertiesToBeAdded); + performReplacementOnAllGeneratedProperties(properties, replacementProperty); } else { - String content = properties.getProperty(propertyKey); - String result = performReplacement(replacementProperty, content); - if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { - String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix(); - properties.setProperty(newPropertyKey, result); - log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); - } else { - properties.setProperty(propertyKey, result); - log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'"); - } + performReplacementOnSingleProperty(properties, replacementProperty, propertyKey); } } } } + private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) { + Map propertiesToBeAdded = new HashMap<>(); + for (Map.Entry entry : properties.entrySet()) { + String key = (String)entry.getKey(); + String content = (String)entry.getValue(); + String result = performReplacement(replacementProperty, content); + if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { + String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix(); + propertiesToBeAdded.put(newPropertyKey, result); + log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); + } else { + entry.setValue(result); + log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'"); + } + } + properties.putAll(propertiesToBeAdded); + } + + private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) { + String content = properties.getProperty(propertyKey); + String result = performReplacement(replacementProperty, content); + if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { + String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix(); + properties.setProperty(newPropertyKey, result); + log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); + } else { + properties.setProperty(propertyKey, result); + log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'"); + } + } + private String performReplacement(ReplacementProperty replacementProperty, String content) { String result = content; if (replacementProperty != null) { From 311a8f6989aeba06fd5cf5e4d0af15f52633db65 Mon Sep 17 00:00:00 2001 From: TheSnoozer Date: Sat, 12 Oct 2019 22:43:20 +0200 Subject: [PATCH 2/2] https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/413: use maven's PluginParameterExpressionEvaluator to allow replacements with values that contain unresolved variables (maven sometimes just behaves weird) --- .../pl/project13/maven/git/GitCommitIdMojo.java | 5 ++--- .../project13/maven/git/PropertiesReplacer.java | 17 +++++++++++------ .../maven/git/PropertiesReplacerTest.java | 10 ++++++++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index 1d2882a9..67a4f4ad 100644 --- a/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -29,6 +29,7 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -386,9 +387,6 @@ public class GitCommitIdMojo extends AbstractMojo { @Nonnull private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log); - @Nonnull - PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log); - @Override public void execute() throws MojoExecutionException { try { @@ -490,6 +488,7 @@ public void execute() throws MojoExecutionException { loadGitData(properties); loadBuildData(properties); + PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log, new PluginParameterExpressionEvaluator(session)); propertiesReplacer.performReplacement(properties, replacementProperties); propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot); propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot); diff --git a/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java b/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java index ba79f7e5..f83045d2 100644 --- a/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java +++ b/maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java @@ -17,19 +17,19 @@ package pl.project13.maven.git; +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import pl.project13.core.log.LoggerBridge; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.regex.Pattern; public class PropertiesReplacer { private final LoggerBridge log; + private final PluginParameterExpressionEvaluator expressionEvaluator; - public PropertiesReplacer(LoggerBridge log) { + public PropertiesReplacer(LoggerBridge log, PluginParameterExpressionEvaluator expressionEvaluator) { this.log = log; + this.expressionEvaluator = expressionEvaluator; } /** @@ -81,7 +81,12 @@ private void performReplacementOnSingleProperty(Properties properties, Replaceme } private String performReplacement(ReplacementProperty replacementProperty, String content) { - String result = content; + String result = ""; + try { + result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse(""); + } catch (Exception e) { + log.error("Something went wrong performing the replacement.", e); + } if (replacementProperty != null) { result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE); if (replacementProperty.isRegex()) { diff --git a/maven/src/test/java/pl/project13/maven/git/PropertiesReplacerTest.java b/maven/src/test/java/pl/project13/maven/git/PropertiesReplacerTest.java index a501bcb2..36bc0465 100644 --- a/maven/src/test/java/pl/project13/maven/git/PropertiesReplacerTest.java +++ b/maven/src/test/java/pl/project13/maven/git/PropertiesReplacerTest.java @@ -19,6 +19,7 @@ import junitparams.JUnitParamsRunner; import junitparams.Parameters; +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,7 +36,10 @@ import java.util.Properties; import static java.util.Arrays.asList; +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(JUnitParamsRunner.class) public class PropertiesReplacerTest { @@ -46,8 +50,10 @@ public static Collection useRegexReplacement() { private PropertiesReplacer propertiesReplacer; @Before - public void setUp() { - this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class)); + public void setUp() throws Throwable { + PluginParameterExpressionEvaluator pluginParameterExpressionEvaluator = mock(PluginParameterExpressionEvaluator.class); + when(pluginParameterExpressionEvaluator.evaluate(anyString())).then(returnsFirstArg()); + this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class), pluginParameterExpressionEvaluator); } @Test