@@ -309,72 +309,76 @@ public class GitCommitIdMojo extends AbstractMojo {
309309
310310 @ Override
311311 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 );
319315
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!" );
323318 return ;
324319 }
325- }
326320
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+ }
331327
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+ }
336332
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+ }
340337
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+ }
347341
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 ;
354347 }
355348
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+ }
357356
358- String trimmedPrefix = prefix .trim ();
359- prefixDot = trimmedPrefix .equals ("" ) ? "" : trimmedPrefix + "." ;
357+ properties = initProperties ();
360358
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 + "." ;
368361
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 );
372369
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 );
375379 }
376- } catch (Exception e ) {
377- handlePluginFailure ( e );
380+ } catch (GitCommitIdExecutionException e ) {
381+ throw new MojoExecutionException ( e . getMessage (), e );
378382 }
379383 }
380384
@@ -430,14 +434,14 @@ public Predicate<CharSequence> apply(String exclude) {
430434
431435 /**
432436 * 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.
434438 *
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
436440 * {@code failOnUnableToExtractRepoInfo} setting was set to true.
437441 */
438- private void handlePluginFailure (Exception e ) throws MojoExecutionException {
442+ private void handlePluginFailure (Exception e ) throws GitCommitIdExecutionException {
439443 if (failOnUnableToExtractRepoInfo ) {
440- throw new MojoExecutionException ("Could not complete Mojo execution..." , e );
444+ throw new GitCommitIdExecutionException ("Could not complete Mojo execution..." , e );
441445 } else {
442446 log .error (e .getMessage (), e );
443447 }
@@ -464,11 +468,11 @@ private void appendPropertiesToReactorProjects(@NotNull Properties properties, @
464468 *
465469 * @return the File representation of the .git directory
466470 */
467- @ VisibleForTesting File lookupGitDirectory () throws MojoExecutionException {
471+ @ VisibleForTesting File lookupGitDirectory () throws GitCommitIdExecutionException {
468472 return new GitDirLocator (project , reactorProjects ).lookupGitDirectory (dotGitDirectory );
469473 }
470474
471- private Properties initProperties () throws MojoExecutionException {
475+ private Properties initProperties () throws GitCommitIdExecutionException {
472476 if (generateGitPropertiesFile ) {
473477 return properties = new Properties ();
474478 } else {
@@ -530,30 +534,34 @@ void loadShortDescribe(@NotNull Properties properties) {
530534 }
531535 }
532536
533- void loadGitData (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
537+ void loadGitData (@ NotNull Properties properties ) throws GitCommitIdExecutionException {
534538 if (useNativeGit ) {
535539 loadGitDataWithNativeGit (properties );
536540 } else {
537541 loadGitDataWithJGit (properties );
538542 }
539543 }
540544
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+ }
554562 }
555563
556- void loadGitDataWithJGit (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
564+ void loadGitDataWithJGit (@ NotNull Properties properties ) throws GitCommitIdExecutionException {
557565 GitDataProvider jGitProvider = JGitProvider
558566 .on (dotGitDirectory , log )
559567 .setPrefixDot (prefixDot )
@@ -566,67 +574,68 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
566574 jGitProvider .loadGitData (properties );
567575 }
568576
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 );
572581
573- boolean shouldGenerate = true ;
582+ boolean shouldGenerate = true ;
574583
575- if (gitPropsFile .exists ( )) {
576- final Properties persistedProperties ;
584+ if (gitPropsFile .exists ()) {
585+ final Properties persistedProperties ;
577586
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 ());
581590
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 ());
586594
587- persistedProperties = readProperties ( gitPropsFile );
588- }
595+ persistedProperties = readProperties (gitPropsFile );
596+ }
589597
590- final Properties propertiesCopy = (Properties ) localProperties .clone ( );
598+ final Properties propertiesCopy = (Properties ) localProperties .clone ();
591599
592- final String buildTimeProperty = prefixDot + BUILD_TIME ;
600+ final String buildTimeProperty = prefixDot + BUILD_TIME ;
593601
594- propertiesCopy .remove ( buildTimeProperty );
595- persistedProperties .remove ( buildTimeProperty );
602+ propertiesCopy .remove (buildTimeProperty );
603+ persistedProperties .remove (buildTimeProperty );
596604
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+ }
603611 }
604- }
605612
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 ;
610617
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 );
620633 }
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 ());
626636 }
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 );
630639 }
631640 }
632641
0 commit comments