1919
2020import com .fasterxml .jackson .databind .ObjectMapper ;
2121import com .google .common .annotations .VisibleForTesting ;
22+ import com .google .common .base .Function ;
23+ import com .google .common .base .Predicate ;
24+ import com .google .common .base .Predicates ;
25+ import com .google .common .collect .Lists ;
2226import com .google .common .io .Closeables ;
2327import com .google .common .io .Files ;
2428import org .apache .maven .plugin .AbstractMojo ;
4044import java .io .FileWriter ;
4145import java .io .IOException ;
4246import java .text .SimpleDateFormat ;
43- import java .util .Date ;
44- import java .util .List ;
45- import java .util .Map ;
46- import java .util .Properties ;
47+ import java .util .*;
4748
4849import static com .google .common .base .Strings .isNullOrEmpty ;
4950
@@ -119,7 +120,7 @@ public class GitCommitIdMojo extends AbstractMojo {
119120 * Specifies whether the execution in pom projects should be skipped.
120121 * Override this value to false if you want to force the plugin to run on 'pom' packaged projects.
121122 *
122- * @parameter property="git.skipPoms" default-value="true"
123+ * @parameter default-value="true"
123124 */
124125 @ SuppressWarnings ("UnusedDeclaration" )
125126 private boolean skipPoms ;
@@ -248,6 +249,23 @@ public class GitCommitIdMojo extends AbstractMojo {
248249 @ SuppressWarnings ("UnusedDeclaration" )
249250 private boolean skip = false ;
250251
252+ /**
253+ * Can be used to exclude certain properties from being emited into the resulting file.
254+ * May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?),
255+ * or the email of the committer etc.
256+ *
257+ * Each value may be globbing, that is, you can write {@code git.commit.user.*} to exclude both, the {@code name},
258+ * as well as {@code email} properties from being emitted into the resulting files.
259+ *
260+ * Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}).
261+ *
262+ * @parameter
263+ * @since 2.1.9
264+ */
265+ @ SuppressWarnings ("UnusedDeclaration" )
266+ private List <String > excludeProperties = Collections .emptyList ();
267+
268+
251269 /**
252270 * The properties we store our data in and then expose them
253271 */
@@ -267,7 +285,7 @@ public void execute() throws MojoExecutionException {
267285 return ;
268286 }
269287
270- if (isPomProject (project ) && skipPoms ) {
288+ if (isPomProject (project )) {
271289 log ("isPomProject is true and skipPoms is true, return" );
272290 return ;
273291 }
@@ -287,6 +305,7 @@ public void execute() throws MojoExecutionException {
287305 prefixDot = prefix + "." ;
288306
289307 loadGitData (properties );
308+ filterNot (properties , excludeProperties );
290309 loadBuildTimeData (properties );
291310 logProperties (properties );
292311
@@ -303,6 +322,30 @@ public void execute() throws MojoExecutionException {
303322
304323 }
305324
325+ private void filterNot (Properties properties , @ Nullable List <String > exclusions ) {
326+ if (exclusions == null )
327+ return ;
328+
329+ List <Predicate <CharSequence >> excludePredicates = Lists .transform (exclusions , new Function <String , Predicate <CharSequence >>() {
330+ @ Override
331+ public Predicate <CharSequence > apply (String exclude ) {
332+ return Predicates .containsPattern (exclude );
333+ }
334+ });
335+
336+ Predicate <CharSequence > shouldExclude = Predicates .alwaysFalse ();
337+ for (Predicate <CharSequence > predicate : excludePredicates ) {
338+ shouldExclude = Predicates .or (shouldExclude , predicate );
339+ }
340+
341+ for (String key : properties .stringPropertyNames ()) {
342+ if (shouldExclude .apply (key )) {
343+ System .out .println ("shouldExclude.apply(" + key +") = " + shouldExclude .apply (key ));
344+ properties .remove (key );
345+ }
346+ }
347+ }
348+
306349 /**
307350 * Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
308351 * If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
@@ -633,4 +676,8 @@ public void setGitDescribe(GitDescribeConfig gitDescribe) {
633676 public void setAbbrevLength (int abbrevLength ) {
634677 this .abbrevLength = abbrevLength ;
635678 }
679+
680+ public void setExcludeProperties (List <String > excludeProperties ) {
681+ this .excludeProperties = excludeProperties ;
682+ }
636683}
0 commit comments