3636import java .util .Map ;
3737import java .util .Properties ;
3838import java .util .TimeZone ;
39- import java .util .regex .Pattern ;
4039
4140import org .apache .maven .execution .MavenSession ;
4241import org .apache .maven .plugin .AbstractMojo ;
5150import com .fasterxml .jackson .core .type .TypeReference ;
5251import com .fasterxml .jackson .databind .ObjectMapper ;
5352import 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 ;
5853import com .google .common .io .Closeables ;
5954import com .google .common .io .Files ;
6055import 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 );
0 commit comments