Skip to content

Commit c4a377d

Browse files
authored
Merge pull request #444 from TheSnoozer/feature-413
#413: use maven's PluginParameterExpressionEvaluator to allow replacements with values that contain variables
2 parents 88deffe + 311a8f6 commit c4a377d

3 files changed

Lines changed: 58 additions & 36 deletions

File tree

maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.maven.execution.MavenSession;
3030
import org.apache.maven.plugin.AbstractMojo;
3131
import org.apache.maven.plugin.MojoExecutionException;
32+
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
3233
import org.apache.maven.plugins.annotations.Component;
3334
import org.apache.maven.plugins.annotations.LifecyclePhase;
3435
import org.apache.maven.plugins.annotations.Mojo;
@@ -386,9 +387,6 @@ public class GitCommitIdMojo extends AbstractMojo {
386387
@Nonnull
387388
private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log);
388389

389-
@Nonnull
390-
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log);
391-
392390
@Override
393391
public void execute() throws MojoExecutionException {
394392
try {
@@ -490,6 +488,7 @@ public void execute() throws MojoExecutionException {
490488

491489
loadGitData(properties);
492490
loadBuildData(properties);
491+
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log, new PluginParameterExpressionEvaluator(session));
493492
propertiesReplacer.performReplacement(properties, replacementProperties);
494493
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
495494
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);

maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,76 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
2021
import pl.project13.core.log.LoggerBridge;
2122

22-
import java.util.HashMap;
23-
import java.util.List;
24-
import java.util.Map;
25-
import java.util.Properties;
23+
import java.util.*;
2624
import java.util.regex.Pattern;
2725

2826
public class PropertiesReplacer {
2927
private final LoggerBridge log;
28+
private final PluginParameterExpressionEvaluator expressionEvaluator;
3029

31-
public PropertiesReplacer(LoggerBridge log) {
30+
public PropertiesReplacer(LoggerBridge log, PluginParameterExpressionEvaluator expressionEvaluator) {
3231
this.log = log;
32+
this.expressionEvaluator = expressionEvaluator;
3333
}
3434

35+
/**
36+
* @param properties all properties that are being generated by the plugin
37+
* @param replacementProperties list of all replacement actions to perform
38+
*/
3539
public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
3640
if ((replacementProperties != null) && (properties != null)) {
3741
for (ReplacementProperty replacementProperty: replacementProperties) {
3842
String propertyKey = replacementProperty.getProperty();
3943
if (propertyKey == null) {
40-
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
41-
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
42-
String key = (String)entry.getKey();
43-
String content = (String)entry.getValue();
44-
String result = performReplacement(replacementProperty, content);
45-
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
46-
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
47-
propertiesToBeAdded.put(newPropertyKey, result);
48-
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
49-
} else {
50-
entry.setValue(result);
51-
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
52-
}
53-
}
54-
properties.putAll(propertiesToBeAdded);
44+
performReplacementOnAllGeneratedProperties(properties, replacementProperty);
5545
} else {
56-
String content = properties.getProperty(propertyKey);
57-
String result = performReplacement(replacementProperty, content);
58-
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
59-
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
60-
properties.setProperty(newPropertyKey, result);
61-
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
62-
} else {
63-
properties.setProperty(propertyKey, result);
64-
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
65-
}
46+
performReplacementOnSingleProperty(properties, replacementProperty, propertyKey);
6647
}
6748
}
6849
}
6950
}
7051

52+
private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) {
53+
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
54+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
55+
String key = (String)entry.getKey();
56+
String content = (String)entry.getValue();
57+
String result = performReplacement(replacementProperty, content);
58+
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
59+
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
60+
propertiesToBeAdded.put(newPropertyKey, result);
61+
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
62+
} else {
63+
entry.setValue(result);
64+
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
65+
}
66+
}
67+
properties.putAll(propertiesToBeAdded);
68+
}
69+
70+
private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
71+
String content = properties.getProperty(propertyKey);
72+
String result = performReplacement(replacementProperty, content);
73+
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
74+
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
75+
properties.setProperty(newPropertyKey, result);
76+
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
77+
} else {
78+
properties.setProperty(propertyKey, result);
79+
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
80+
}
81+
}
82+
7183
private String performReplacement(ReplacementProperty replacementProperty, String content) {
72-
String result = content;
84+
String result = "";
85+
try {
86+
result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse("");
87+
} catch (Exception e) {
88+
log.error("Something went wrong performing the replacement.", e);
89+
}
7390
if (replacementProperty != null) {
7491
result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
7592
if (replacementProperty.isRegex()) {

maven/src/test/java/pl/project13/maven/git/PropertiesReplacerTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import junitparams.JUnitParamsRunner;
2121
import junitparams.Parameters;
22+
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
2223
import org.junit.Assert;
2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -35,7 +36,10 @@
3536
import java.util.Properties;
3637

3738
import static java.util.Arrays.asList;
39+
import static org.mockito.AdditionalAnswers.returnsFirstArg;
40+
import static org.mockito.ArgumentMatchers.anyString;
3841
import static org.mockito.Mockito.mock;
42+
import static org.mockito.Mockito.when;
3943

4044
@RunWith(JUnitParamsRunner.class)
4145
public class PropertiesReplacerTest {
@@ -46,8 +50,10 @@ public static Collection<?> useRegexReplacement() {
4650
private PropertiesReplacer propertiesReplacer;
4751

4852
@Before
49-
public void setUp() {
50-
this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class));
53+
public void setUp() throws Throwable {
54+
PluginParameterExpressionEvaluator pluginParameterExpressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
55+
when(pluginParameterExpressionEvaluator.evaluate(anyString())).then(returnsFirstArg());
56+
this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class), pluginParameterExpressionEvaluator);
5157
}
5258

5359
@Test

0 commit comments

Comments
 (0)