• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

git-commit-id / git-commit-id-maven-plugin / #219

04 Dec 2023 07:24PM UTC coverage: 71.134%. Remained the same
#219

push

web-flow
Merge pull request #676 from git-commit-id/dependabot/maven/org.apache.maven.plugins-maven-javadoc-plugin-3.6.3

Bump org.apache.maven.plugins:maven-javadoc-plugin from 3.6.2 to 3.6.3

276 of 388 relevant lines covered (71.13%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.52
/src/main/java/pl/project13/maven/git/PropertiesReplacer.java
1
/*
2
 * This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3
 *
4
 * git-commit-id-maven-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-maven-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-maven-plugin.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
package pl.project13.maven.git;
19

20
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
21
import pl.project13.core.log.LogInterface;
22

23
import java.util.*;
24
import java.util.regex.Pattern;
25

26
/**
27
 * This class encapsulates logic to perform property replacements.
28
 * For a use-case refer to https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/317.
29
 */
30
public class PropertiesReplacer {
31
  private final LogInterface log;
32
  private final PluginParameterExpressionEvaluator expressionEvaluator;
33

34
  /**
35
   * Constructor to encapsulates all references required to perform property replacements.
36
   * @param log The logger to log any messages
37
   * @param expressionEvaluator Maven's PluginParameterExpressionEvaluator
38
   *                            (see https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/413 why it's needed)
39
   */
40
  public PropertiesReplacer(LogInterface log, PluginParameterExpressionEvaluator expressionEvaluator) {
1✔
41
    this.log = log;
1✔
42
    this.expressionEvaluator = expressionEvaluator;
1✔
43
  }
1✔
44

45
  /**
46
   * Method that performs the actual property replacement.
47
   * @param properties all properties that are being generated by the plugin
48
   * @param replacementProperties list of all replacement actions to perform
49
   */
50
  public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
51
    if ((replacementProperties != null) && (properties != null)) {
1✔
52
      for (ReplacementProperty replacementProperty: replacementProperties) {
1✔
53
        String propertyKey = replacementProperty.getProperty();
1✔
54
        if (propertyKey == null) {
1✔
55
          performReplacementOnAllGeneratedProperties(properties, replacementProperty);
1✔
56
        } else {
57
          performReplacementOnSingleProperty(properties, replacementProperty, propertyKey);
1✔
58
        }
59
      }
1✔
60
    }
61
  }
1✔
62

63
  private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) {
64
    for (String propertyName : properties.stringPropertyNames()) {
1✔
65
      String content = properties.getProperty(propertyName);
1✔
66
      String result = performReplacement(replacementProperty, content);
1✔
67
      if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
1✔
68
        String newPropertyKey = propertyName + "." + replacementProperty.getPropertyOutputSuffix();
1✔
69
        properties.setProperty(newPropertyKey, result);
1✔
70
        log.info("apply replace on property " + propertyName + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
1✔
71
      } else {
1✔
72
        properties.setProperty(propertyName, result);
1✔
73
        log.info("apply replace on property " + propertyName + ": original value '" + content + "' with '" + result + "'");
1✔
74
      }
75
    }
1✔
76
  }
1✔
77

78
  private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
79
    String content = properties.getProperty(propertyKey);
1✔
80
    String result = performReplacement(replacementProperty, content);
1✔
81
    if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
1✔
82
      String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
1✔
83
      properties.setProperty(newPropertyKey, result);
1✔
84
      log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
1✔
85
    } else {
1✔
86
      properties.setProperty(propertyKey, result);
1✔
87
      log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
1✔
88
    }
89
  }
1✔
90

91
  private String performReplacement(ReplacementProperty replacementProperty, String content) {
92
    String evaluationContent = content;
1✔
93
    if (evaluationContent == null || evaluationContent.isEmpty() || replacementProperty.isForceValueEvaluation()) {
1✔
94
      evaluationContent = replacementProperty.getValue();
×
95
    }
96
    String result = "";
1✔
97
    try {
98
      result = Optional
1✔
99
              .ofNullable(expressionEvaluator.evaluate(evaluationContent))
1✔
100
              .map(x -> x.toString()).orElse(evaluationContent);
1✔
101
    } catch (Exception e) {
×
102
      log.error("Something went wrong performing the replacement.", e);
×
103
    }
1✔
104
    if (replacementProperty != null) {
1✔
105
      result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
1✔
106
      if (replacementProperty.isRegex()) {
1✔
107
        result = replaceRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
1✔
108
      } else {
109
        result = replaceNonRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
1✔
110
      }
111
      result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.AFTER);
1✔
112
    }
113
    return result;
1✔
114
  }
115

116
  private String performTransformationRules(ReplacementProperty replacementProperty, String content, TransformationRule.ApplyEnum forRule) {
117
    String result = content;
1✔
118
    if ((replacementProperty.getTransformationRules() != null) && (!replacementProperty.getTransformationRules().isEmpty())) {
1✔
119
      for (TransformationRule transformationRule: replacementProperty.getTransformationRules()) {
1✔
120
        if (transformationRule.getApplyRule().equals(forRule)) {
1✔
121
          result = transformationRule.getActionRule().perform(result);
1✔
122
        }
123
      }
1✔
124
    }
125
    return result;
1✔
126
  }
127

128
  private String replaceRegex(String content, String token, String value) {
129
    if (token == null) {
1✔
130
      log.error("found replacementProperty without required token.");
1✔
131
      return content;
1✔
132
    }
133
    final Pattern compiledPattern = Pattern.compile(token);
1✔
134
    return compiledPattern.matcher(content).replaceAll(value == null ? "" : value);
1✔
135
  }
136

137
  private String replaceNonRegex(String content, String token, String value) {
138
    if (token == null) {
1✔
139
      log.error("found replacementProperty without required token.");
1✔
140
      return content;
1✔
141
    }
142
    return content.replace(token, value == null ? "" : value);
1✔
143
  }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc