@@ -309,72 +309,76 @@ public class GitCommitIdMojo extends AbstractMojo {
309
309
310
310
@ Override
311
311
public void execute () throws MojoExecutionException {
312
- // Set the verbose setting: now it should be correctly loaded from maven.
313
- log .setVerbose (verbose );
314
-
315
- if (skip ) {
316
- log .info ("skip is enabled, skipping execution!" );
317
- return ;
318
- }
312
+ try {
313
+ // Set the verbose setting: now it should be correctly loaded from maven.
314
+ log .setVerbose (verbose );
319
315
320
- if (runOnlyOnce ) {
321
- if (!session .getExecutionRootDirectory ().equals (session .getCurrentProject ().getBasedir ().getAbsolutePath ())) {
322
- log .info ("runOnlyOnce is enabled and this project is not the top level project, skipping execution!" );
316
+ if (skip ) {
317
+ log .info ("skip is enabled, skipping execution!" );
323
318
return ;
324
319
}
325
- }
326
320
327
- if (isPomProject (project ) && skipPoms ) {
328
- log .info ("isPomProject is true and skipPoms is true, return" );
329
- return ;
330
- }
321
+ if (runOnlyOnce ) {
322
+ if (!session .getExecutionRootDirectory ().equals (session .getCurrentProject ().getBasedir ().getAbsolutePath ())) {
323
+ log .info ("runOnlyOnce is enabled and this project is not the top level project, skipping execution!" );
324
+ return ;
325
+ }
326
+ }
331
327
332
- dotGitDirectory = lookupGitDirectory ();
333
- if ( failOnNoGitDirectory && ! directoryExists ( dotGitDirectory )) {
334
- throw new MojoExecutionException ( ".git directory is not found! Please specify a valid [dotGitDirectory] in your pom.xml" ) ;
335
- }
328
+ if ( isPomProject ( project ) && skipPoms ) {
329
+ log . info ( "isPomProject is true and skipPoms is true, return" );
330
+ return ;
331
+ }
336
332
337
- if (gitDescribe == null ) {
338
- gitDescribe = new GitDescribeConfig ();
339
- }
333
+ dotGitDirectory = lookupGitDirectory ();
334
+ if (failOnNoGitDirectory && !directoryExists (dotGitDirectory )) {
335
+ throw new GitCommitIdExecutionException (".git directory is not found! Please specify a valid [dotGitDirectory] in your pom.xml" );
336
+ }
340
337
341
- if (dotGitDirectory != null ) {
342
- log .info ("dotGitDirectory {}" , dotGitDirectory .getAbsolutePath ());
343
- } else {
344
- log .info ("dotGitDirectory is null, aborting execution!" );
345
- return ;
346
- }
338
+ if (gitDescribe == null ) {
339
+ gitDescribe = new GitDescribeConfig ();
340
+ }
347
341
348
- try {
349
- try {
350
- commitIdGenerationModeEnum = CommitIdGenerationMode .valueOf (commitIdGenerationMode .toUpperCase ());
351
- } catch (IllegalArgumentException e ) {
352
- log .warn ("Detected wrong setting for 'commitIdGenerationMode'. Falling back to default 'flat' mode!" );
353
- commitIdGenerationModeEnum = CommitIdGenerationMode .FLAT ;
342
+ if (dotGitDirectory != null ) {
343
+ log .info ("dotGitDirectory {}" , dotGitDirectory .getAbsolutePath ());
344
+ } else {
345
+ log .info ("dotGitDirectory is null, aborting execution!" );
346
+ return ;
354
347
}
355
348
356
- properties = initProperties ();
349
+ try {
350
+ try {
351
+ commitIdGenerationModeEnum = CommitIdGenerationMode .valueOf (commitIdGenerationMode .toUpperCase ());
352
+ } catch (IllegalArgumentException e ) {
353
+ log .warn ("Detected wrong setting for 'commitIdGenerationMode'. Falling back to default 'flat' mode!" );
354
+ commitIdGenerationModeEnum = CommitIdGenerationMode .FLAT ;
355
+ }
357
356
358
- String trimmedPrefix = prefix .trim ();
359
- prefixDot = trimmedPrefix .equals ("" ) ? "" : trimmedPrefix + "." ;
357
+ properties = initProperties ();
360
358
361
- loadGitData (properties );
362
- loadBuildVersionAndTimeData (properties );
363
- loadBuildHostData (properties );
364
- loadShortDescribe (properties );
365
- filter (properties , includeOnlyProperties );
366
- filterNot (properties , excludeProperties );
367
- logProperties (properties );
359
+ String trimmedPrefix = prefix .trim ();
360
+ prefixDot = trimmedPrefix .equals ("" ) ? "" : trimmedPrefix + "." ;
368
361
369
- if (generateGitPropertiesFile ) {
370
- maybeGeneratePropertiesFile (properties , project .getBasedir (), generateGitPropertiesFilename );
371
- }
362
+ loadGitData (properties );
363
+ loadBuildVersionAndTimeData (properties );
364
+ loadBuildHostData (properties );
365
+ loadShortDescribe (properties );
366
+ filter (properties , includeOnlyProperties );
367
+ filterNot (properties , excludeProperties );
368
+ logProperties (properties );
372
369
373
- if (injectAllReactorProjects ) {
374
- appendPropertiesToReactorProjects (properties , prefixDot );
370
+ if (generateGitPropertiesFile ) {
371
+ maybeGeneratePropertiesFile (properties , project .getBasedir (), generateGitPropertiesFilename );
372
+ }
373
+
374
+ if (injectAllReactorProjects ) {
375
+ appendPropertiesToReactorProjects (properties , prefixDot );
376
+ }
377
+ } catch (Exception e ) {
378
+ handlePluginFailure (e );
375
379
}
376
- } catch (Exception e ) {
377
- handlePluginFailure ( e );
380
+ } catch (GitCommitIdExecutionException e ) {
381
+ throw new MojoExecutionException ( e . getMessage (), e );
378
382
}
379
383
}
380
384
@@ -430,14 +434,14 @@ public Predicate<CharSequence> apply(String exclude) {
430
434
431
435
/**
432
436
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
433
- * If it's true, an MojoExecutionException will be throw , otherwise we just log an error message.
437
+ * If it's true, an GitCommitIdExecutionException will be thrown , otherwise we just log an error message.
434
438
*
435
- * @throws MojoExecutionException which will be should be throw within an MojoException in case the
439
+ * @throws GitCommitIdExecutionException which will be thrown in case the
436
440
* {@code failOnUnableToExtractRepoInfo} setting was set to true.
437
441
*/
438
- private void handlePluginFailure (Exception e ) throws MojoExecutionException {
442
+ private void handlePluginFailure (Exception e ) throws GitCommitIdExecutionException {
439
443
if (failOnUnableToExtractRepoInfo ) {
440
- throw new MojoExecutionException ("Could not complete Mojo execution..." , e );
444
+ throw new GitCommitIdExecutionException ("Could not complete Mojo execution..." , e );
441
445
} else {
442
446
log .error (e .getMessage (), e );
443
447
}
@@ -464,11 +468,11 @@ private void appendPropertiesToReactorProjects(@NotNull Properties properties, @
464
468
*
465
469
* @return the File representation of the .git directory
466
470
*/
467
- @ VisibleForTesting File lookupGitDirectory () throws MojoExecutionException {
471
+ @ VisibleForTesting File lookupGitDirectory () throws GitCommitIdExecutionException {
468
472
return new GitDirLocator (project , reactorProjects ).lookupGitDirectory (dotGitDirectory );
469
473
}
470
474
471
- private Properties initProperties () throws MojoExecutionException {
475
+ private Properties initProperties () throws GitCommitIdExecutionException {
472
476
if (generateGitPropertiesFile ) {
473
477
return properties = new Properties ();
474
478
} else {
@@ -530,30 +534,34 @@ void loadShortDescribe(@NotNull Properties properties) {
530
534
}
531
535
}
532
536
533
- void loadGitData (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
537
+ void loadGitData (@ NotNull Properties properties ) throws GitCommitIdExecutionException {
534
538
if (useNativeGit ) {
535
539
loadGitDataWithNativeGit (properties );
536
540
} else {
537
541
loadGitDataWithJGit (properties );
538
542
}
539
543
}
540
544
541
- void loadGitDataWithNativeGit (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
542
- final File basedir = project .getBasedir ().getCanonicalFile ();
543
-
544
- GitDataProvider nativeGitProvider = NativeGitProvider
545
- .on (basedir , log )
546
- .setPrefixDot (prefixDot )
547
- .setAbbrevLength (abbrevLength )
548
- .setDateFormat (dateFormat )
549
- .setDateFormatTimeZone (dateFormatTimeZone )
550
- .setGitDescribe (gitDescribe )
551
- .setCommitIdGenerationMode (commitIdGenerationModeEnum );
552
-
553
- nativeGitProvider .loadGitData (properties );
545
+ void loadGitDataWithNativeGit (@ NotNull Properties properties ) throws GitCommitIdExecutionException {
546
+ try {
547
+ final File basedir = project .getBasedir ().getCanonicalFile ();
548
+
549
+ GitDataProvider nativeGitProvider = NativeGitProvider
550
+ .on (basedir , log )
551
+ .setPrefixDot (prefixDot )
552
+ .setAbbrevLength (abbrevLength )
553
+ .setDateFormat (dateFormat )
554
+ .setDateFormatTimeZone (dateFormatTimeZone )
555
+ .setGitDescribe (gitDescribe )
556
+ .setCommitIdGenerationMode (commitIdGenerationModeEnum );
557
+
558
+ nativeGitProvider .loadGitData (properties );
559
+ } catch (IOException e ) {
560
+ throw new GitCommitIdExecutionException (e );
561
+ }
554
562
}
555
563
556
- void loadGitDataWithJGit (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
564
+ void loadGitDataWithJGit (@ NotNull Properties properties ) throws GitCommitIdExecutionException {
557
565
GitDataProvider jGitProvider = JGitProvider
558
566
.on (dotGitDirectory , log )
559
567
.setPrefixDot (prefixDot )
@@ -566,67 +574,68 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
566
574
jGitProvider .loadGitData (properties );
567
575
}
568
576
569
- void maybeGeneratePropertiesFile (@ NotNull Properties localProperties , File base , String propertiesFilename ) throws IOException {
570
- final File gitPropsFile = craftPropertiesOutputFile (base , propertiesFilename );
571
- final boolean isJsonFormat = "json" .equalsIgnoreCase ( format );
577
+ void maybeGeneratePropertiesFile (@ NotNull Properties localProperties , File base , String propertiesFilename ) throws GitCommitIdExecutionException {
578
+ try {
579
+ final File gitPropsFile = craftPropertiesOutputFile (base , propertiesFilename );
580
+ final boolean isJsonFormat = "json" .equalsIgnoreCase (format );
572
581
573
- boolean shouldGenerate = true ;
582
+ boolean shouldGenerate = true ;
574
583
575
- if (gitPropsFile .exists ( )) {
576
- final Properties persistedProperties ;
584
+ if (gitPropsFile .exists ()) {
585
+ final Properties persistedProperties ;
577
586
578
- try {
579
- if (isJsonFormat ) {
580
- log .info ("Reading existing json file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
587
+ try {
588
+ if (isJsonFormat ) {
589
+ log .info ("Reading existing json file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
581
590
582
- persistedProperties = readJsonProperties ( gitPropsFile );
583
- }
584
- else {
585
- log .info ("Reading existing properties file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
591
+ persistedProperties = readJsonProperties (gitPropsFile );
592
+ } else {
593
+ log .info ("Reading existing properties file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
586
594
587
- persistedProperties = readProperties ( gitPropsFile );
588
- }
595
+ persistedProperties = readProperties (gitPropsFile );
596
+ }
589
597
590
- final Properties propertiesCopy = (Properties ) localProperties .clone ( );
598
+ final Properties propertiesCopy = (Properties ) localProperties .clone ();
591
599
592
- final String buildTimeProperty = prefixDot + BUILD_TIME ;
600
+ final String buildTimeProperty = prefixDot + BUILD_TIME ;
593
601
594
- propertiesCopy .remove ( buildTimeProperty );
595
- persistedProperties .remove ( buildTimeProperty );
602
+ propertiesCopy .remove (buildTimeProperty );
603
+ persistedProperties .remove (buildTimeProperty );
596
604
597
- shouldGenerate = ! propertiesCopy .equals ( persistedProperties );
598
- }
599
- catch ( CannotReadFileException ex ) {
600
- // Read has failed, regenerate file
601
- log . info ( "Cannot read properties file [{}] (for module {})..." , gitPropsFile . getAbsolutePath (), project . getName ()) ;
602
- shouldGenerate = true ;
605
+ shouldGenerate = !propertiesCopy .equals (persistedProperties );
606
+ } catch ( CannotReadFileException ex ) {
607
+ // Read has failed, regenerate file
608
+ log . info ( "Cannot read properties file [{}] (for module {})..." , gitPropsFile . getAbsolutePath (), project . getName ());
609
+ shouldGenerate = true ;
610
+ }
603
611
}
604
- }
605
612
606
- if (shouldGenerate ) {
607
- Files .createParentDirs (gitPropsFile );
608
- Writer outputWriter = null ;
609
- boolean threw = true ;
613
+ if (shouldGenerate ) {
614
+ Files .createParentDirs (gitPropsFile );
615
+ Writer outputWriter = null ;
616
+ boolean threw = true ;
610
617
611
- try {
612
- outputWriter = new OutputStreamWriter (new FileOutputStream (gitPropsFile ), StandardCharsets .UTF_8 );
613
- if (isJsonFormat ) {
614
- log .info ("Writing json file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
615
- ObjectMapper mapper = new ObjectMapper ();
616
- mapper .writeValue (outputWriter , localProperties );
617
- } else {
618
- log .info ("Writing properties file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
619
- localProperties .store (outputWriter , "Generated by Git-Commit-Id-Plugin" );
618
+ try {
619
+ outputWriter = new OutputStreamWriter (new FileOutputStream (gitPropsFile ), StandardCharsets .UTF_8 );
620
+ if (isJsonFormat ) {
621
+ log .info ("Writing json file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
622
+ ObjectMapper mapper = new ObjectMapper ();
623
+ mapper .writeValue (outputWriter , localProperties );
624
+ } else {
625
+ log .info ("Writing properties file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
626
+ localProperties .store (outputWriter , "Generated by Git-Commit-Id-Plugin" );
627
+ }
628
+ threw = false ;
629
+ } catch (final IOException ex ) {
630
+ throw new RuntimeException ("Cannot create custom git properties file: " + gitPropsFile , ex );
631
+ } finally {
632
+ Closeables .close (outputWriter , threw );
620
633
}
621
- threw = false ;
622
- } catch (final IOException ex ) {
623
- throw new RuntimeException ("Cannot create custom git properties file: " + gitPropsFile , ex );
624
- } finally {
625
- Closeables .close (outputWriter , threw );
634
+ } else {
635
+ log .info ("Properties file [{}] is up-to-date (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
626
636
}
627
- }
628
- else {
629
- log .info ("Properties file [{}] is up-to-date (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
637
+ } catch (IOException e ) {
638
+ throw new GitCommitIdExecutionException (e );
630
639
}
631
640
}
632
641
0 commit comments