19
19
20
20
import com .fasterxml .jackson .databind .ObjectMapper ;
21
21
import 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 ;
22
26
import com .google .common .io .Closeables ;
23
27
import com .google .common .io .Files ;
24
28
import org .apache .maven .plugin .AbstractMojo ;
40
44
import java .io .FileWriter ;
41
45
import java .io .IOException ;
42
46
import 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 .*;
47
48
48
49
import static com .google .common .base .Strings .isNullOrEmpty ;
49
50
@@ -119,7 +120,7 @@ public class GitCommitIdMojo extends AbstractMojo {
119
120
* Specifies whether the execution in pom projects should be skipped.
120
121
* Override this value to false if you want to force the plugin to run on 'pom' packaged projects.
121
122
*
122
- * @parameter property="git.skipPoms" default-value="true"
123
+ * @parameter default-value="true"
123
124
*/
124
125
@ SuppressWarnings ("UnusedDeclaration" )
125
126
private boolean skipPoms ;
@@ -248,6 +249,23 @@ public class GitCommitIdMojo extends AbstractMojo {
248
249
@ SuppressWarnings ("UnusedDeclaration" )
249
250
private boolean skip = false ;
250
251
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
+
251
269
/**
252
270
* The properties we store our data in and then expose them
253
271
*/
@@ -267,7 +285,7 @@ public void execute() throws MojoExecutionException {
267
285
return ;
268
286
}
269
287
270
- if (isPomProject (project ) && skipPoms ) {
288
+ if (isPomProject (project )) {
271
289
log ("isPomProject is true and skipPoms is true, return" );
272
290
return ;
273
291
}
@@ -287,6 +305,7 @@ public void execute() throws MojoExecutionException {
287
305
prefixDot = prefix + "." ;
288
306
289
307
loadGitData (properties );
308
+ filterNot (properties , excludeProperties );
290
309
loadBuildTimeData (properties );
291
310
logProperties (properties );
292
311
@@ -303,6 +322,30 @@ public void execute() throws MojoExecutionException {
303
322
304
323
}
305
324
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
+
306
349
/**
307
350
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
308
351
* 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) {
633
676
public void setAbbrevLength (int abbrevLength ) {
634
677
this .abbrevLength = abbrevLength ;
635
678
}
679
+
680
+ public void setExcludeProperties (List <String > excludeProperties ) {
681
+ this .excludeProperties = excludeProperties ;
682
+ }
636
683
}
0 commit comments