Skip to content

Commit

Permalink
Merge f6f4ba2 into b22426b
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed Dec 14, 2019
2 parents b22426b + f6f4ba2 commit c783a40
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
11 changes: 9 additions & 2 deletions core/src/main/java/pl/project13/core/GitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,22 @@ protected String stripCredentialsFromOriginUrl(String gitRemoteString) throws Gi
// Build a new URL from the original URL, but nulling out the password
// component of the userinfo. We keep the username so that ssh uris such
// ssh://git@github.com will retain 'git@'.
String extractedUserInfo = null;
if (userInfo.length > 0) {
extractedUserInfo = userInfo[0];
if (extractedUserInfo.isEmpty()) {
extractedUserInfo = null;
}
}
return new URI(original.getScheme(),
userInfo[0],
extractedUserInfo,
original.getHost(),
original.getPort(),
original.getPath(),
original.getQuery(),
original.getFragment()).toString();

} catch (URISyntaxException e) {
} catch (Exception e) {
log.error("Something went wrong to strip the credentials from git's remote url (please report this)!", e);
return "";
}
Expand Down
9 changes: 9 additions & 0 deletions maven/docs/using-the-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ It's really simple to setup this plugin; below is a sample pom that you may base
Please note that the replacement will *only be applied to properties that are being
generated by the plugin*. If you want to replace properties that are being generated by
other plugins you may want to use the maven-replacer-plugin or any other alternative.
Since 4.0.1 the plugin allows to define a `forceValueEvaluation`-switch which forces the
plugin to evaluate the given value on *every* project.
This might come handy if *every* project needs a unique value and a user wants to
project specific variables like `${project.artifactId}`.
Be adviced that this essentially means that the plugin *must* run for every child-project of a
reactor build and thus might cause some overhead (the git properties should be cached).
For a use-case refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/457.
-->
<replacementProperties>
<!--
Expand All @@ -466,6 +474,7 @@ It's really simple to setup this plugin; below is a sample pom that you may base
<token>^([^\/]*)\/([^\/]*)$</token>
<value>$1-$2</value>
<regex>true</regex>
<forceValueEvaluation>false</forceValueEvaluation>
<transformationRules>
<transformationRule>
<apply>BEFORE</apply>
Expand Down
46 changes: 32 additions & 14 deletions maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -68,6 +69,12 @@ public class GitCommitIdMojo extends AbstractMojo {
@Parameter(defaultValue = "${reactorProjects}", readonly = true)
List<MavenProject> reactorProjects;

/**
* The Mojo Execution
*/
@Parameter(defaultValue = "${mojoExecution}", readonly = true)
MojoExecution mojoExecution;

/**
* The Maven Session Object.
*/
Expand Down Expand Up @@ -488,10 +495,15 @@ public void execute() throws MojoExecutionException {

loadGitData(properties);
loadBuildData(properties);
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log, new PluginParameterExpressionEvaluator(session));
propertiesReplacer.performReplacement(properties, replacementProperties);
// first round of publication and filtering (we need to make variables available for the ParameterExpressionEvaluator
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
publishToAllSystemEnvironments();

// now run replacements
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(
log, new PluginParameterExpressionEvaluator(session, mojoExecution));
propertiesReplacer.performReplacement(properties, replacementProperties);

logProperties();

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

publishPropertiesInto(project.getProperties());
// some plugins rely on the user properties (e.g. flatten-maven-plugin)
publishPropertiesInto(session.getUserProperties());

if (injectAllReactorProjects) {
appendPropertiesToReactorProjects();
}

if (injectIntoSysProperties) {
publishPropertiesInto(System.getProperties());
publishPropertiesInto(session.getSystemProperties());
}
// publish properties again since we might have new properties gained by the replacement
publishToAllSystemEnvironments();

} catch (Exception e) {
handlePluginFailure(e);
Expand All @@ -521,6 +523,22 @@ public void execute() throws MojoExecutionException {
}
}

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

if (injectAllReactorProjects) {
appendPropertiesToReactorProjects();
}

if (injectIntoSysProperties) {
publishPropertiesInto(System.getProperties());
publishPropertiesInto(session.getSystemProperties());
publishPropertiesInto(session.getRequest().getSystemProperties());
}
}

@Nullable
private Properties getContextProperties(MavenProject project) {
Object stored = project.getContextValue(CONTEXT_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ private void performReplacementOnSingleProperty(Properties properties, Replaceme
}

private String performReplacement(ReplacementProperty replacementProperty, String content) {
String evaluationContent = content;
if (evaluationContent == null || evaluationContent.isEmpty() || replacementProperty.isForceValueEvaluation()) {
evaluationContent = replacementProperty.getValue();
}
String result = "";
try {
result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse("");
result = Optional
.ofNullable(expressionEvaluator.evaluate(evaluationContent))
.map(x -> x.toString()).orElse(evaluationContent);
} catch (Exception e) {
log.error("Something went wrong performing the replacement.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public class ReplacementProperty {
@Parameter(defaultValue = "true")
private boolean regex = true;

/**
* Forces the plugin to evaluate the value on *every* project.
* Note that this essentially means that the plugin *must* run for every child-project of a reactor
* build and thus might cause some overhead (the git properties should be cached).
*
* For a use-case refer to https://github.com/git-commit-id/maven-git-commit-id-plugin/issues/457.
*/
@Parameter(defaultValue = "false")
private boolean forceValueEvaluation = false;

/**
* @since 2.2.4
* Provides the ability to perform certain string transformations before regex evaluation or after.
Expand All @@ -78,12 +88,13 @@ public class ReplacementProperty {
public ReplacementProperty() {
}

public ReplacementProperty(String property, String propertyOutputSuffix, String token, String value, boolean regex, List<TransformationRule> transformationRules) {
public ReplacementProperty(String property, String propertyOutputSuffix, String token, String value, boolean regex, boolean forceValueEvaluation, List<TransformationRule> transformationRules) {
this.property = property;
this.propertyOutputSuffix = propertyOutputSuffix;
this.token = token;
this.value = value;
this.regex = regex;
this.forceValueEvaluation = forceValueEvaluation;
this.transformationRules = transformationRules;
}

Expand Down Expand Up @@ -127,6 +138,15 @@ public void setRegex(boolean regex) {
this.regex = regex;
}


public boolean isForceValueEvaluation() {
return forceValueEvaluation;
}

public void setForceValueEvaluation(boolean forceValueEvaluation) {
this.forceValueEvaluation = forceValueEvaluation;
}

public List<TransformationRule> getTransformationRules() {
return transformationRules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static Collection<Object[]> parameters() {
{ "https://user@example.com:8888", "https://user@example.com:8888" },
{ "https://user:password@example.com", "https://user@example.com" },
{ "https://user:password@example.com:8888", "https://user@example.com:8888" },
{ "https://:@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
{ "https://:password@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
{ "git@github.com", "git@github.com" },
{ "git@github.com:8888", "git@github.com:8888" },
{ "user@host.xz:~user/path/to/repo.git", "user@host.xz:~user/path/to/repo.git" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testPerformReplacementWithInvalidReplacement(boolean regex) throws I
Properties actualProperties = build("key1", "value1", "key2", "value2");

List<ReplacementProperty> replacementProperties = new ArrayList<>();
replacementProperties.add(new ReplacementProperty("key1", null, null, null, regex, null));
replacementProperties.add(new ReplacementProperty("key1", null, null, null, regex, false, null));

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

List<ReplacementProperty> replacementProperties = new ArrayList<>();
replacementProperties.add(new ReplacementProperty(null, null, "value", null, regex, null));
replacementProperties.add(new ReplacementProperty(null, null, "value", null, regex, false, null));

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

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

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

propertiesReplacer.performReplacement(actualProperties, replacementProperties);

Expand Down

0 comments on commit c783a40

Please sign in to comment.