Skip to content

Commit 14b5fad

Browse files
author
TheSnoozer
authored
Merge pull request #327 from TheSnoozer/master
Improve properties replacement and allow to change the case of letters (#317) and fix the runningOnBuildServer check for detecting the branch name on Jenkins / Hudson to also check 'HUDSON_HOME' and 'JENKINS_HOME' (#326)
2 parents f0450de + deec4d1 commit 14b5fad

File tree

6 files changed

+278
-20
lines changed

6 files changed

+278
-20
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,30 @@ It's really simple to setup this plugin; below is a sample pom that you may base
361361
Furthermore each replacementProperty need to be configured with a token and a value.
362362
The token can be seen as the needle and the value as the text to be written over any found tokens. If using regular expressions the value can reference grouped regex matches by using $1, $2, etc.
363363
364+
Since 2.2.4 the plugin allows to define a even more sophisticated ruleset and allows to set an `propertyOutputSuffix` within each replacement property. If this option is empty the original property will be overwritten (default behaviour in 2.2.3). however when this configuration is set to `something` and a user wants to modify the `git.branch` property the plugin will keep `git.branch` as the original one (w/o modifications) but also will be creating a new `git.branch.something` property with the requested replacement.
365+
Furthermore with 2.2.4 the plugin allows to perform certain types of string manipulation either before or after the evaluation of the replacement. With this feature a user can currently easily manipulate the case (e.g. lower case VS upper case) of the input/output property. This behaviour can be achieved by defining a list of `transformationRules` for the property where those rules should take effect. Each `transformationRule` consist of two required fields `apply` and `action`. The `apply`-tag controls when the rule should be applied and can be set to `BEFORE` to have the rule being applied before or it can be set to `AFTER` to have the rule being applied after the replacement. The `action`-tag determines the string conversion rule that should be applied. Currenlty supported is `LOWER_CASE` and `UPPER_CASE`. Potential candidates in the feature are `CAPITALIZATION` and `INVERT_CASE``(open a ticket if you need them...).
366+
364367
Please note that the replacement will *only be applied to properties that are being generated by the plugin*.
365368
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.
366369
-->
367370
<replacementProperties>
368371
<!-- example: apply replacement only to the specific property git.branch and replace '/' with '-'
369372
<replacementProperty>
370373
<property>git.branch</property>
374+
<propertyOutputSuffix>something<propertyOutputSuffix>
371375
<token>^([^\/]*)\/([^\/]*)$</token>
372376
<value>$1-$2</value>
373377
<regex>true</regex>
378+
<transformationRules>
379+
<transformationRule>
380+
<apply>BEFORE</apply>
381+
<action>UPPER_CASE</action>
382+
</transformationRule>
383+
<transformationRule>
384+
<apply>AFTER</apply>
385+
<action>LOWER_CASE</action>
386+
</transformationRule>
387+
</transformationRules>
374388
</replacementProperty>
375389
-->
376390
</replacementProperties>
@@ -882,7 +896,7 @@ Worth pointing out is, that git-commit-id tries to be 1-to-1 compatible with git
882896
* **skip** - `(default: false)` when you don't use `git-describe` information in your build, you can opt to be calculate it.
883897

884898

885-
* **validationProperties** Since version **2.2.2** the maven-git-commit-id-plugin comes equipped with an additional validation utility which can be used to verify if your project properties are set as you would like to have them set. This feature ships with an additional mojo execution and for instance allows to check if the version is not a snapshot build. If you are interested in the config checkout the[validation utility documentation](https://github.com/ktoso/maven-git-commit-id-plugin#validate-if-properties-are-set-as-expected).
899+
**validationProperties** Since version **2.2.2** the maven-git-commit-id-plugin comes equipped with an additional validation utility which can be used to verify if your project properties are set as you would like to have them set. This feature ships with an additional mojo execution and for instance allows to check if the version is not a snapshot build. If you are interested in the config checkout the[validation utility documentation](https://github.com/ktoso/maven-git-commit-id-plugin#validate-if-properties-are-set-as-expected).
886900

887901
All options are documented in the code, so just use `ctrl + q` (intellij @ linux) or `f1` (intellij @ osx) when writing the options in pom.xml - you'll get examples and detailed information about each option (even more than here).
888902

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ protected String determineBranchName(Map<String, String> env) throws GitCommitId
180180
* @param env environment settings
181181
*/
182182
private boolean runningOnBuildServer(Map<String, String> env) {
183-
return env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL");
183+
return env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL") ||
184+
env.containsKey("HUDSON_HOME") || env.containsKey("JENKINS_HOME");
184185
}
185186

186187
/**
@@ -261,4 +262,4 @@ protected static String stripCredentialsFromOriginUrl(String gitRemoteString) th
261262
throw new GitCommitIdExecutionException(e);
262263
}
263264
}
264-
}
265+
}

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

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

2020
import pl.project13.maven.git.log.LoggerBridge;
2121

22+
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Properties;
@@ -38,30 +39,56 @@ public void performReplacement(Properties properties, List<ReplacementProperty>
3839
for(ReplacementProperty replacementProperty: replacementProperties) {
3940
String propertyKey = replacementProperty.getProperty();
4041
if(propertyKey == null) {
42+
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
4143
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
4244
String key = (String)entry.getKey();
4345
String content = (String)entry.getValue();
4446
String result = performReplacement(replacementProperty, content);
45-
entry.setValue(result);
46-
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
47+
if((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
48+
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
49+
propertiesToBeAdded.put(newPropertyKey, result);
50+
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
51+
} else {
52+
entry.setValue(result);
53+
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
54+
}
4755
}
56+
properties.putAll(propertiesToBeAdded);
4857
} else {
4958
String content = properties.getProperty(propertyKey);
5059
String result = performReplacement(replacementProperty, content);
51-
properties.setProperty(propertyKey, result);
52-
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
60+
if((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
61+
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
62+
properties.setProperty(newPropertyKey, result);
63+
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
64+
} else {
65+
properties.setProperty(propertyKey, result);
66+
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
67+
}
5368
}
5469
}
5570
}
5671
}
5772

5873
private String performReplacement(ReplacementProperty replacementProperty, String content) {
59-
String result = content;
74+
String result = performTransformationRules(replacementProperty, content, TransformationRule.ApplyEnum.BEFORE);
6075
if(replacementProperty != null) {
6176
if(replacementProperty.isRegex()) {
62-
result = replaceRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
77+
result = replaceRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
6378
} else {
64-
result = replaceNonRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
79+
result = replaceNonRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
80+
}
81+
}
82+
return performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.AFTER);
83+
}
84+
85+
private String performTransformationRules(ReplacementProperty replacementProperty, String content, TransformationRule.ApplyEnum forRule) {
86+
String result = content;
87+
if((replacementProperty.getTransformationRules() != null) && (!replacementProperty.getTransformationRules().isEmpty())) {
88+
for(TransformationRule transformationRule: replacementProperty.getTransformationRules()) {
89+
if(transformationRule.getApplyRule().equals(forRule)) {
90+
result = transformationRule.getActionRule().perform(result);
91+
}
6592
}
6693
}
6794
return result;

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import java.util.ArrayList;
21+
import java.util.List;
22+
2023
import org.apache.maven.plugins.annotations.Parameter;
2124

25+
/**
26+
* @since 2.2.3
27+
*/
2228
public class ReplacementProperty {
2329
/**
2430
* Defines if a replacement should only be applied to a single property.
@@ -27,6 +33,20 @@ public class ReplacementProperty {
2733
@Parameter
2834
private String property;
2935

36+
/**
37+
* @since 2.2.4
38+
* Defines an additional output property suffix.
39+
* Note:
40+
* this will only be *appended* to the current property key
41+
* (e.g. when the property is set to 'sample' the property
42+
* 'git.branch' will be transformed to 'git.branch.sample')
43+
*
44+
* Be advised that you might want to adjust your include
45+
* or exclude filters which be applied after the regex validation.
46+
*/
47+
@Parameter
48+
private String propertyOutputSuffix;
49+
3050
/**
3151
* Token to replace.
3252
* This may or may not be a regular expression.
@@ -48,13 +68,22 @@ public class ReplacementProperty {
4868
@Parameter(defaultValue = "true")
4969
private boolean regex = true;
5070

71+
/**
72+
* @since 2.2.4
73+
* Provides the ability to perform certain string transformations before regex evaluation or after.
74+
*/
75+
@Parameter
76+
private List<TransformationRule> transformationRules = new ArrayList<>();
77+
5178
public ReplacementProperty(){}
5279

53-
public ReplacementProperty(String property, String token, String value, boolean regex) {
80+
public ReplacementProperty(String property, String propertyOutputSuffix, String token, String value, boolean regex, List<TransformationRule> transformationRules) {
5481
this.property = property;
82+
this.propertyOutputSuffix = propertyOutputSuffix;
5583
this.token = token;
5684
this.value = value;
5785
this.regex = regex;
86+
this.transformationRules = transformationRules;
5887
}
5988

6089
public String getProperty() {
@@ -65,6 +94,14 @@ public void setProperty(String property) {
6594
this.property = property;
6695
}
6796

97+
public String getPropertyOutputSuffix() {
98+
return propertyOutputSuffix;
99+
}
100+
101+
public void setPropertyOutputSuffix(String propertyOutputSuffix) {
102+
this.propertyOutputSuffix = propertyOutputSuffix;
103+
}
104+
68105
public String getToken() {
69106
return token;
70107
}
@@ -89,4 +126,11 @@ public void setRegex(boolean regex) {
89126
this.regex = regex;
90127
}
91128

129+
public List<TransformationRule> getTransformationRules() {
130+
return transformationRules;
131+
}
132+
133+
public void setTransformationRules(List<TransformationRule> transformationRules) {
134+
this.transformationRules = transformationRules;
135+
}
92136
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.git;
19+
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
22+
public class TransformationRule {
23+
/**
24+
* Determines when the transformation should be taken place.
25+
* Currently supported is
26+
* - BEFORE_REGEX
27+
* - AFTER_REGEX
28+
*/
29+
@Parameter(required = true)
30+
private String apply;
31+
32+
private ApplyEnum applyRule;
33+
34+
protected enum ApplyEnum {
35+
BEFORE,
36+
AFTER,
37+
;
38+
};
39+
40+
/**
41+
* Determines the action that should be performed as transformation.
42+
* Currently supported is
43+
* - LOWER_CASE
44+
* - UPPER_CASE
45+
*/
46+
@Parameter(required = true)
47+
private String action;
48+
49+
private ActionEnum actionRule;
50+
51+
protected enum ActionEnum {
52+
LOWER_CASE {
53+
@Override
54+
protected String perform(String input) {
55+
if(input != null) {
56+
return input.toLowerCase();
57+
}
58+
return input;
59+
}
60+
},
61+
UPPER_CASE {
62+
@Override
63+
protected String perform(String input) {
64+
if(input != null) {
65+
return input.toUpperCase();
66+
}
67+
return null;
68+
}
69+
},
70+
;
71+
protected abstract String perform(String input);
72+
}
73+
74+
public TransformationRule(){}
75+
76+
public TransformationRule(String apply, String action) {
77+
this(ApplyEnum.valueOf(apply), ActionEnum.valueOf(action));
78+
this.apply = apply;
79+
this.action = action;
80+
}
81+
82+
protected TransformationRule(ApplyEnum applyRule, ActionEnum actionRule) {
83+
this.applyRule = applyRule;
84+
this.actionRule = actionRule;
85+
}
86+
87+
public String getApply() {
88+
return apply;
89+
}
90+
91+
public void setApply(String apply) {
92+
this.apply = apply;
93+
}
94+
95+
public ApplyEnum getApplyRule() {
96+
return applyRule;
97+
}
98+
99+
public String getAction() {
100+
return action;
101+
}
102+
103+
public void setAction(String action) {
104+
this.action = action;
105+
}
106+
107+
public ActionEnum getActionRule() {
108+
return actionRule;
109+
}
110+
}

0 commit comments

Comments
 (0)