Skip to content

Commit c2d2061

Browse files
author
TheSnoozer
authored
Merge pull request #311 from cbuschka/issue_310_cbuschka_project_props_removed
#310: Only properties created by maven-git-commit-id-plugin are being filtered now otherwise the git-commit-id-plugin would remove project properties generated by other plugins
2 parents 765c358 + b17ce50 commit c2d2061

File tree

7 files changed

+439
-279
lines changed

7 files changed

+439
-279
lines changed

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

Lines changed: 19 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Map;
3737
import java.util.Properties;
3838
import java.util.TimeZone;
39-
import java.util.regex.Pattern;
4039

4140
import org.apache.maven.execution.MavenSession;
4241
import org.apache.maven.plugin.AbstractMojo;
@@ -51,10 +50,6 @@
5150
import com.fasterxml.jackson.core.type.TypeReference;
5251
import com.fasterxml.jackson.databind.ObjectMapper;
5352
import com.google.common.annotations.VisibleForTesting;
54-
import com.google.common.base.Function;
55-
import com.google.common.base.Predicate;
56-
import com.google.common.base.Predicates;
57-
import com.google.common.collect.Lists;
5853
import com.google.common.io.Closeables;
5954
import com.google.common.io.Files;
6055
import java.io.OutputStream;
@@ -294,7 +289,7 @@ public class GitCommitIdMojo extends AbstractMojo {
294289
* @since 2.2.3
295290
*/
296291
@Parameter
297-
private List<ReplacementProperty> replacementProperties;
292+
@VisibleForTesting List<ReplacementProperty> replacementProperties;
298293

299294
/**
300295
* The properties we store our data in and then expose them.
@@ -309,6 +304,11 @@ public class GitCommitIdMojo extends AbstractMojo {
309304
@NotNull
310305
private final LoggerBridge log = new MavenLoggerBridge(this, false);
311306

307+
@NotNull
308+
private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log);
309+
310+
@NotNull @VisibleForTesting PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log);
311+
312312
@Override
313313
public void execute() throws MojoExecutionException {
314314
try {
@@ -364,7 +364,7 @@ public void execute() throws MojoExecutionException {
364364
commitIdGenerationModeEnum = CommitIdGenerationMode.FLAT;
365365
}
366366

367-
properties = initProperties();
367+
properties = new Properties();
368368

369369
String trimmedPrefix = prefix.trim();
370370
prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
@@ -373,18 +373,18 @@ public void execute() throws MojoExecutionException {
373373
loadBuildVersionAndTimeData(properties);
374374
loadBuildHostData(properties);
375375
loadShortDescribe(properties);
376-
performReplacement(properties, replacementProperties);
377-
filter(properties, includeOnlyProperties);
378-
filterNot(properties, excludeProperties);
379-
logProperties(properties);
376+
propertiesReplacer.performReplacement(properties, replacementProperties);
377+
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
378+
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
379+
logProperties();
380380

381381
if (generateGitPropertiesFile) {
382382
maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
383-
project.getProperties().putAll(properties); // add to maven project properties also when file is generated
384383
}
384+
publishPropertiesInto(project);
385385

386386
if (injectAllReactorProjects) {
387-
appendPropertiesToReactorProjects(properties, prefixDot);
387+
appendPropertiesToReactorProjects();
388388
}
389389
} catch (Exception e) {
390390
handlePluginFailure(e);
@@ -394,105 +394,8 @@ public void execute() throws MojoExecutionException {
394394
}
395395
}
396396

397-
@VisibleForTesting void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
398-
if((replacementProperties != null) && (properties != null)) {
399-
for(ReplacementProperty replacementProperty: replacementProperties) {
400-
String propertyKey = replacementProperty.getProperty();
401-
if(propertyKey == null) {
402-
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
403-
String key = (String)entry.getKey();
404-
String content = (String)entry.getValue();
405-
String result = performReplacement(replacementProperty, content);
406-
entry.setValue(result);
407-
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
408-
}
409-
} else {
410-
String content = properties.getProperty(propertyKey);
411-
String result = performReplacement(replacementProperty, content);
412-
properties.setProperty(propertyKey, result);
413-
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
414-
}
415-
}
416-
}
417-
}
418-
419-
private String performReplacement(ReplacementProperty replacementProperty, String content) {
420-
String result = content;
421-
if(replacementProperty != null) {
422-
if(replacementProperty.isRegex()) {
423-
result = replaceRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
424-
} else {
425-
result = replaceNonRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
426-
}
427-
}
428-
return result;
429-
}
430-
431-
private String replaceRegex(String content, String token, String value) {
432-
if((token == null) || (value == null)) {
433-
log.error("found replacementProperty without required token or value.");
434-
return content;
435-
}
436-
final Pattern compiledPattern = Pattern.compile(token);
437-
return compiledPattern.matcher(content).replaceAll(value);
438-
}
439-
440-
private String replaceNonRegex(String content, String token, String value) {
441-
if((token == null) || (value == null)) {
442-
log.error("found replacementProperty without required token or value.");
443-
return content;
444-
}
445-
return content.replace(token, value);
446-
}
447-
448-
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
449-
if (exclusions == null || exclusions.isEmpty()) {
450-
return;
451-
}
452-
453-
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
454-
@Override
455-
public Predicate<CharSequence> apply(String exclude) {
456-
return Predicates.containsPattern(exclude);
457-
}
458-
});
459-
460-
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
461-
for (Predicate<CharSequence> predicate : excludePredicates) {
462-
shouldExclude = Predicates.or(shouldExclude, predicate);
463-
}
464-
465-
for (String key : properties.stringPropertyNames()) {
466-
if (shouldExclude.apply(key)) {
467-
log.debug("shouldExclude.apply({}) = {}", key, shouldExclude.apply(key));
468-
properties.remove(key);
469-
}
470-
}
471-
}
472-
473-
private void filter(Properties properties, @Nullable List<String> inclusions) {
474-
if (inclusions == null || inclusions.isEmpty()) {
475-
return;
476-
}
477-
478-
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
479-
@Override
480-
public Predicate<CharSequence> apply(String exclude) {
481-
return Predicates.containsPattern(exclude);
482-
}
483-
});
484-
485-
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
486-
for (Predicate<CharSequence> predicate : includePredicates) {
487-
shouldInclude = Predicates.or(shouldInclude, predicate);
488-
}
489-
490-
for (String key : properties.stringPropertyNames()) {
491-
if (!shouldInclude.apply(key)) {
492-
log.debug("!shouldInclude.apply({}) = {}", key, shouldInclude.apply(key));
493-
properties.remove(key);
494-
}
495-
}
397+
private void publishPropertiesInto(MavenProject target) {
398+
target.getProperties().putAll(properties);
496399
}
497400

498401
/**
@@ -510,18 +413,13 @@ private void handlePluginFailure(Exception e) throws GitCommitIdExecutionExcepti
510413
}
511414
}
512415

513-
private void appendPropertiesToReactorProjects(@NotNull Properties properties, @NotNull String trimmedPrefixWithDot) {
416+
private void appendPropertiesToReactorProjects() {
514417
for (MavenProject mavenProject : reactorProjects) {
515-
Properties mavenProperties = mavenProject.getProperties();
516418

517419
// TODO check message
518420
log.info("{}] project {}", mavenProject.getName(), mavenProject.getName());
519421

520-
for (Object key : properties.keySet()) {
521-
if (key.toString().startsWith(trimmedPrefixWithDot)) {
522-
mavenProperties.put(key, properties.get(key));
523-
}
524-
}
422+
publishPropertiesInto(mavenProject);
525423
}
526424
}
527425

@@ -535,27 +433,13 @@ private void appendPropertiesToReactorProjects(@NotNull Properties properties, @
535433
return new GitDirLocator(project, reactorProjects).lookupGitDirectory(dotGitDirectory);
536434
}
537435

538-
private Properties initProperties() throws GitCommitIdExecutionException {
539-
if (generateGitPropertiesFile) {
540-
return properties = new Properties();
541-
} else {
542-
return properties = project.getProperties();
543-
}
544-
}
545-
546-
private void logProperties(@NotNull Properties properties) {
436+
private void logProperties() {
547437
for (Object key : properties.keySet()) {
548438
String keyString = key.toString();
549-
if (isOurProperty(keyString)) {
550-
log.info("found property {}", keyString);
551-
}
439+
log.info("found property {}", keyString);
552440
}
553441
}
554442

555-
private boolean isOurProperty(@NotNull String keyString) {
556-
return keyString.startsWith(prefixDot);
557-
}
558-
559443
void loadBuildVersionAndTimeData(@NotNull Properties properties) {
560444
Date buildDate = new Date();
561445
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 java.util.List;
21+
import java.util.Properties;
22+
23+
import org.jetbrains.annotations.Nullable;
24+
25+
import com.google.common.base.Function;
26+
import com.google.common.base.Predicate;
27+
import com.google.common.base.Predicates;
28+
import com.google.common.collect.Lists;
29+
30+
import pl.project13.maven.git.log.LoggerBridge;
31+
32+
public class PropertiesFilterer {
33+
34+
private LoggerBridge log;
35+
36+
public PropertiesFilterer(LoggerBridge log) {
37+
this.log = log;
38+
}
39+
40+
public void filterNot(Properties properties, @Nullable List<String> exclusions, String prefixDot) {
41+
if (exclusions == null || exclusions.isEmpty()) {
42+
return;
43+
}
44+
45+
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
46+
@Override
47+
public Predicate<CharSequence> apply(String exclude) {
48+
return Predicates.containsPattern(exclude);
49+
}
50+
});
51+
52+
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
53+
for (Predicate<CharSequence> predicate : excludePredicates) {
54+
shouldExclude = Predicates.or(shouldExclude, predicate);
55+
}
56+
57+
for (String key : properties.stringPropertyNames()) {
58+
if (isOurProperty(key, prefixDot) && shouldExclude.apply(key)) {
59+
log.debug("shouldExclude.apply({}) = {}", key, shouldExclude.apply(key));
60+
properties.remove(key);
61+
}
62+
}
63+
}
64+
65+
public void filter(Properties properties, @Nullable List<String> inclusions, String prefixDot) {
66+
if (inclusions == null || inclusions.isEmpty()) {
67+
return;
68+
}
69+
70+
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
71+
@Override
72+
public Predicate<CharSequence> apply(String exclude) {
73+
return Predicates.containsPattern(exclude);
74+
}
75+
});
76+
77+
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
78+
for (Predicate<CharSequence> predicate : includePredicates) {
79+
shouldInclude = Predicates.or(shouldInclude, predicate);
80+
}
81+
82+
for (String key : properties.stringPropertyNames()) {
83+
if (isOurProperty(key, prefixDot) && !shouldInclude.apply(key)) {
84+
log.debug("!shouldInclude.apply({}) = {}", key, shouldInclude.apply(key));
85+
properties.remove(key);
86+
}
87+
}
88+
}
89+
90+
private boolean isOurProperty(String key, String prefixDot) {
91+
return key.startsWith(prefixDot);
92+
}
93+
}

0 commit comments

Comments
 (0)