Skip to content

Commit 247d9df

Browse files
authored
Merge pull request #460 from TheSnoozer/master
Fix #457 & #459
2 parents b22426b + f6f4ba2 commit 247d9df

File tree

7 files changed

+90
-28
lines changed

7 files changed

+90
-28
lines changed

core/src/main/java/pl/project13/core/GitDataProvider.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,22 @@ protected String stripCredentialsFromOriginUrl(String gitRemoteString) throws Gi
315315
// Build a new URL from the original URL, but nulling out the password
316316
// component of the userinfo. We keep the username so that ssh uris such
317317
// ssh://git@github.com will retain 'git@'.
318+
String extractedUserInfo = null;
319+
if (userInfo.length > 0) {
320+
extractedUserInfo = userInfo[0];
321+
if (extractedUserInfo.isEmpty()) {
322+
extractedUserInfo = null;
323+
}
324+
}
318325
return new URI(original.getScheme(),
319-
userInfo[0],
326+
extractedUserInfo,
320327
original.getHost(),
321328
original.getPort(),
322329
original.getPath(),
323330
original.getQuery(),
324331
original.getFragment()).toString();
325332

326-
} catch (URISyntaxException e) {
333+
} catch (Exception e) {
327334
log.error("Something went wrong to strip the credentials from git's remote url (please report this)!", e);
328335
return "";
329336
}

maven/docs/using-the-plugin.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ It's really simple to setup this plugin; below is a sample pom that you may base
454454
Please note that the replacement will *only be applied to properties that are being
455455
generated by the plugin*. If you want to replace properties that are being generated by
456456
other plugins you may want to use the maven-replacer-plugin or any other alternative.
457+
458+
Since 4.0.1 the plugin allows to define a `forceValueEvaluation`-switch which forces the
459+
plugin to evaluate the given value on *every* project.
460+
This might come handy if *every* project needs a unique value and a user wants to
461+
project specific variables like `${project.artifactId}`.
462+
Be adviced that this essentially means that the plugin *must* run for every child-project of a
463+
reactor build and thus might cause some overhead (the git properties should be cached).
464+
For a use-case refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/457.
457465
-->
458466
<replacementProperties>
459467
<!--
@@ -466,6 +474,7 @@ It's really simple to setup this plugin; below is a sample pom that you may base
466474
<token>^([^\/]*)\/([^\/]*)$</token>
467475
<value>$1-$2</value>
468476
<regex>true</regex>
477+
<forceValueEvaluation>false</forceValueEvaluation>
469478
<transformationRules>
470479
<transformationRule>
471480
<apply>BEFORE</apply>

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.apache.maven.execution.MavenSession;
3030
import org.apache.maven.plugin.AbstractMojo;
31+
import org.apache.maven.plugin.MojoExecution;
3132
import org.apache.maven.plugin.MojoExecutionException;
3233
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
3334
import org.apache.maven.plugins.annotations.Component;
@@ -68,6 +69,12 @@ public class GitCommitIdMojo extends AbstractMojo {
6869
@Parameter(defaultValue = "${reactorProjects}", readonly = true)
6970
List<MavenProject> reactorProjects;
7071

72+
/**
73+
* The Mojo Execution
74+
*/
75+
@Parameter(defaultValue = "${mojoExecution}", readonly = true)
76+
MojoExecution mojoExecution;
77+
7178
/**
7279
* The Maven Session Object.
7380
*/
@@ -488,10 +495,15 @@ public void execute() throws MojoExecutionException {
488495

489496
loadGitData(properties);
490497
loadBuildData(properties);
491-
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log, new PluginParameterExpressionEvaluator(session));
492-
propertiesReplacer.performReplacement(properties, replacementProperties);
498+
// first round of publication and filtering (we need to make variables available for the ParameterExpressionEvaluator
493499
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
494500
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
501+
publishToAllSystemEnvironments();
502+
503+
// now run replacements
504+
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(
505+
log, new PluginParameterExpressionEvaluator(session, mojoExecution));
506+
propertiesReplacer.performReplacement(properties, replacementProperties);
495507

496508
logProperties();
497509

@@ -500,18 +512,8 @@ public void execute() throws MojoExecutionException {
500512
properties, project.getBasedir(), generateGitPropertiesFilename, sourceCharset);
501513
}
502514

503-
publishPropertiesInto(project.getProperties());
504-
// some plugins rely on the user properties (e.g. flatten-maven-plugin)
505-
publishPropertiesInto(session.getUserProperties());
506-
507-
if (injectAllReactorProjects) {
508-
appendPropertiesToReactorProjects();
509-
}
510-
511-
if (injectIntoSysProperties) {
512-
publishPropertiesInto(System.getProperties());
513-
publishPropertiesInto(session.getSystemProperties());
514-
}
515+
// publish properties again since we might have new properties gained by the replacement
516+
publishToAllSystemEnvironments();
515517

516518
} catch (Exception e) {
517519
handlePluginFailure(e);
@@ -521,6 +523,22 @@ public void execute() throws MojoExecutionException {
521523
}
522524
}
523525

526+
private void publishToAllSystemEnvironments() {
527+
publishPropertiesInto(project.getProperties());
528+
// some plugins rely on the user properties (e.g. flatten-maven-plugin)
529+
publishPropertiesInto(session.getUserProperties());
530+
531+
if (injectAllReactorProjects) {
532+
appendPropertiesToReactorProjects();
533+
}
534+
535+
if (injectIntoSysProperties) {
536+
publishPropertiesInto(System.getProperties());
537+
publishPropertiesInto(session.getSystemProperties());
538+
publishPropertiesInto(session.getRequest().getSystemProperties());
539+
}
540+
}
541+
524542
@Nullable
525543
private Properties getContextProperties(MavenProject project) {
526544
Object stored = project.getContextValue(CONTEXT_KEY);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ private void performReplacementOnSingleProperty(Properties properties, Replaceme
8181
}
8282

8383
private String performReplacement(ReplacementProperty replacementProperty, String content) {
84+
String evaluationContent = content;
85+
if (evaluationContent == null || evaluationContent.isEmpty() || replacementProperty.isForceValueEvaluation()) {
86+
evaluationContent = replacementProperty.getValue();
87+
}
8488
String result = "";
8589
try {
86-
result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse("");
90+
result = Optional
91+
.ofNullable(expressionEvaluator.evaluate(evaluationContent))
92+
.map(x -> x.toString()).orElse(evaluationContent);
8793
} catch (Exception e) {
8894
log.error("Something went wrong performing the replacement.", e);
8995
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public class ReplacementProperty {
6868
@Parameter(defaultValue = "true")
6969
private boolean regex = true;
7070

71+
/**
72+
* Forces the plugin to evaluate the value on *every* project.
73+
* Note that this essentially means that the plugin *must* run for every child-project of a reactor
74+
* build and thus might cause some overhead (the git properties should be cached).
75+
*
76+
* For a use-case refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/457.
77+
*/
78+
@Parameter(defaultValue = "false")
79+
private boolean forceValueEvaluation = false;
80+
7181
/**
7282
* @since 2.2.4
7383
* Provides the ability to perform certain string transformations before regex evaluation or after.
@@ -78,12 +88,13 @@ public class ReplacementProperty {
7888
public ReplacementProperty() {
7989
}
8090

81-
public ReplacementProperty(String property, String propertyOutputSuffix, String token, String value, boolean regex, List<TransformationRule> transformationRules) {
91+
public ReplacementProperty(String property, String propertyOutputSuffix, String token, String value, boolean regex, boolean forceValueEvaluation, List<TransformationRule> transformationRules) {
8292
this.property = property;
8393
this.propertyOutputSuffix = propertyOutputSuffix;
8494
this.token = token;
8595
this.value = value;
8696
this.regex = regex;
97+
this.forceValueEvaluation = forceValueEvaluation;
8798
this.transformationRules = transformationRules;
8899
}
89100

@@ -127,6 +138,15 @@ public void setRegex(boolean regex) {
127138
this.regex = regex;
128139
}
129140

141+
142+
public boolean isForceValueEvaluation() {
143+
return forceValueEvaluation;
144+
}
145+
146+
public void setForceValueEvaluation(boolean forceValueEvaluation) {
147+
this.forceValueEvaluation = forceValueEvaluation;
148+
}
149+
130150
public List<TransformationRule> getTransformationRules() {
131151
return transformationRules;
132152
}

maven/src/test/java/pl/project13/core/UriUserInfoRemoverTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public static Collection<Object[]> parameters() {
4343
{ "https://user@example.com:8888", "https://user@example.com:8888" },
4444
{ "https://user:password@example.com", "https://user@example.com" },
4545
{ "https://user:password@example.com:8888", "https://user@example.com:8888" },
46+
{ "https://:@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
47+
{ "https://:password@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
4648
{ "git@github.com", "git@github.com" },
4749
{ "git@github.com:8888", "git@github.com:8888" },
4850
{ "user@host.xz:~user/path/to/repo.git", "user@host.xz:~user/path/to/repo.git" },

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void testPerformReplacementWithInvalidReplacement(boolean regex) throws I
6969
Properties actualProperties = build("key1", "value1", "key2", "value2");
7070

7171
List<ReplacementProperty> replacementProperties = new ArrayList<>();
72-
replacementProperties.add(new ReplacementProperty("key1", null, null, null, regex, null));
72+
replacementProperties.add(new ReplacementProperty("key1", null, null, null, regex, false, null));
7373

7474
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
7575
}
@@ -80,7 +80,7 @@ public void testPerformReplacementWithSingleProperty(boolean regex) throws IOExc
8080
Properties actualProperties = build("key1", "value1", "key2", "value2");
8181

8282
List<ReplacementProperty> replacementProperties = new ArrayList<>();
83-
replacementProperties.add(new ReplacementProperty("key1", null, "value", "another", regex, null));
83+
replacementProperties.add(new ReplacementProperty("key1", null, "value", "another", regex, false, null));
8484

8585
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
8686

@@ -94,7 +94,7 @@ public void testPerformReplacementWithSinglePropertyEmptyValue(boolean regex) th
9494
Properties actualProperties = build("key1", "value1", "key2", "value2");
9595

9696
List<ReplacementProperty> replacementProperties = new ArrayList<>();
97-
replacementProperties.add(new ReplacementProperty("key1", null, "value", null, regex, null));
97+
replacementProperties.add(new ReplacementProperty("key1", null, "value", null, regex, false, null));
9898

9999
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
100100

@@ -108,7 +108,7 @@ public void testPerformReplacementWithMultipleProperties(boolean regex) throws I
108108
Properties actualProperties = build("key1", "value1", "key2", "value2");
109109

110110
List<ReplacementProperty> replacementProperties = new ArrayList<>();
111-
replacementProperties.add(new ReplacementProperty(null, null, "value", "another", regex, null));
111+
replacementProperties.add(new ReplacementProperty(null, null, "value", "another", regex, false, null));
112112

113113
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
114114

@@ -122,7 +122,7 @@ public void testPerformReplacementWithMultiplePropertiesEmptyValue(boolean regex
122122
Properties actualProperties = build("key1", "value1", "key2", "value2");
123123

124124
List<ReplacementProperty> replacementProperties = new ArrayList<>();
125-
replacementProperties.add(new ReplacementProperty(null, null, "value", null, regex, null));
125+
replacementProperties.add(new ReplacementProperty(null, null, "value", null, regex, false, null));
126126

127127
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
128128

@@ -135,7 +135,7 @@ public void testPerformReplacementWithPatternGroupAndMatching() throws IOExcepti
135135
Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author/name");
136136

137137
List<ReplacementProperty> replacementProperties = new ArrayList<>();
138-
replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, null));
138+
replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null));
139139

140140
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
141141

@@ -148,7 +148,7 @@ public void testPerformReplacementWithPatternGroupAndNoMatch() throws IOExceptio
148148
Properties actualProperties = build("git.branch", "feature#feature_name", "git.commit.author", "author#");
149149

150150
List<ReplacementProperty> replacementProperties = new ArrayList<>();
151-
replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, null));
151+
replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null));
152152

153153
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
154154

@@ -161,7 +161,7 @@ public void testPerformReplacementOnSinglePropertyAndExpectNewPropertyGenerated(
161161
Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author#");
162162

163163
List<ReplacementProperty> replacementProperties = new ArrayList<>();
164-
replacementProperties.add(new ReplacementProperty("git.branch", "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, null));
164+
replacementProperties.add(new ReplacementProperty("git.branch", "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null));
165165

166166
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
167167

@@ -174,7 +174,7 @@ public void testPerformReplacementOnEveryPropertyAndExpectNewPropertyGenerated()
174174
Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author#");
175175

176176
List<ReplacementProperty> replacementProperties = new ArrayList<>();
177-
replacementProperties.add(new ReplacementProperty(null, "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, null));
177+
replacementProperties.add(new ReplacementProperty(null, "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null));
178178

179179
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
180180

@@ -200,7 +200,7 @@ public void runTransformationTestHelper(String input, String regex, Transformati
200200
transformationRules.add(new TransformationRule(applyRule, actionRule));
201201

202202
List<ReplacementProperty> replacementProperties = new ArrayList<>();
203-
replacementProperties.add(new ReplacementProperty(null, null, regex, "-", true, transformationRules));
203+
replacementProperties.add(new ReplacementProperty(null, null, regex, "-", true, false, transformationRules));
204204

205205
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
206206

0 commit comments

Comments
 (0)