2020import com .google .common .annotations .VisibleForTesting ;
2121import com .google .common .io .Closeables ;
2222import com .google .common .io .Files ;
23-
2423import org .apache .maven .plugin .AbstractMojo ;
2524import org .apache .maven .plugin .MojoExecutionException ;
2625import org .apache .maven .project .MavenProject ;
3130import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
3231import org .jetbrains .annotations .NotNull ;
3332import org .jetbrains .annotations .Nullable ;
34-
3533import pl .project13 .jgit .DescribeCommand ;
3634import pl .project13 .jgit .DescribeResult ;
3735import pl .project13 .maven .git .log .LoggerBridge ;
4644import java .util .Map ;
4745import java .util .Properties ;
4846
47+ import static com .google .common .base .Strings .isNullOrEmpty ;
48+
4949/**
5050 * Goal which puts git build-time information into property files or maven's properties.
5151 *
@@ -141,7 +141,7 @@ public class GitCommitIdMojo extends AbstractMojo {
141141 * into generateFile mode.
142142 * <p/>
143143 * The path here is relative to your projects src directory.
144- *
144+ *
145145 * @parameter default-value="${project.build.outputDirectory}/git.properties"
146146 */
147147 @ SuppressWarnings ("UnusedDeclaration" )
@@ -255,7 +255,7 @@ public void execute() throws MojoExecutionException {
255255 if (dotGitDirectory != null ) {
256256 log ("dotGitDirectory" , dotGitDirectory .getAbsolutePath ());
257257 } else {
258- log ("dotGitDirectory is null" );
258+ log ("dotGitDirectory is null, aborting execution! " );
259259 return ;
260260 }
261261
@@ -268,7 +268,7 @@ public void execute() throws MojoExecutionException {
268268 logProperties (properties );
269269
270270 if (generateGitPropertiesFile ) {
271- generatePropertiesFile (properties , generateGitPropertiesFilename );
271+ generatePropertiesFile (properties , project . getBasedir (), generateGitPropertiesFilename );
272272 }
273273
274274 if (injectAllReactorProjects ) {
@@ -278,7 +278,6 @@ public void execute() throws MojoExecutionException {
278278 handlePluginFailure (e );
279279 }
280280
281- log ();
282281 }
283282
284283 /**
@@ -292,13 +291,11 @@ private void handlePluginFailure(Exception e) throws MojoExecutionException {
292291 if (failOnUnableToExtractRepoInfo ) {
293292 throw new MojoExecutionException ("Could not complete Mojo execution..." , e );
294293 } else {
295- loggerBridge .error ();
294+ loggerBridge .error (e . getMessage () );
296295 }
297296 }
298297
299298 private void appendPropertiesToReactorProjects (@ NotNull Properties properties ) {
300- log ();
301-
302299 for (MavenProject mavenProject : reactorProjects ) {
303300 Properties mavenProperties = mavenProject .getProperties ();
304301
@@ -327,12 +324,9 @@ private File lookupGitDirectory() throws MojoExecutionException {
327324 }
328325
329326 private Properties initProperties () throws MojoExecutionException {
330- log ();
331327 if (generateGitPropertiesFile ) {
332- log ();
333328 return properties = new Properties ();
334329 } else if (!runningTests ) {
335- log ();
336330 return properties = project .getProperties ();
337331 } else {
338332 return properties = new Properties (); // that's ok for unit tests
@@ -359,7 +353,6 @@ void loadBuildTimeData(@NotNull Properties properties) {
359353 }
360354
361355 void loadGitData (@ NotNull Properties properties ) throws IOException , MojoExecutionException {
362- log ();
363356 Repository git = getGitRepository ();
364357 ObjectReader objectReader = git .newObjectReader ();
365358
@@ -382,7 +375,7 @@ void loadGitData(@NotNull Properties properties) throws IOException, MojoExecuti
382375
383376 try {
384377 // git.branch
385- String branch = determineBranchName (git );
378+ String branch = determineBranchName (git , System . getenv () );
386379 put (properties , BRANCH , branch );
387380
388381 // git.commit.id.describe
@@ -424,7 +417,7 @@ void loadGitData(@NotNull Properties properties) throws IOException, MojoExecuti
424417 }
425418
426419 private void putAbbrevCommitId (ObjectReader objectReader , Properties properties , RevCommit headCommit , int abbrevLength ) throws MojoExecutionException {
427- if (abbrevLength < 2 || abbrevLength > 40 ) {
420+ if (abbrevLength < 2 || abbrevLength > 40 ) {
428421 throw new MojoExecutionException ("Abbreviated commit id lenght must be between 2 and 40, inclusive! Was [%s]. " .codePointBefore (abbrevLength ) +
429422 "Please fix your configuration (the <abbrevLength/> element)." );
430423 }
@@ -460,14 +453,19 @@ void putGitDescribe(@NotNull Properties properties, @NotNull Repository reposito
460453 }
461454 }
462455
463- void generatePropertiesFile (@ NotNull Properties properties , String generateGitPropertiesFilename ) throws IOException {
456+ static int counter ;
457+
458+ void generatePropertiesFile (@ NotNull Properties properties , File base , String propertiesFilename ) throws IOException {
464459 FileWriter fileWriter = null ;
465- File gitPropsFile = new File (generateGitPropertiesFilename );
460+ File gitPropsFile = new File (base , propertiesFilename );
466461 try {
467462 Files .createParentDirs (gitPropsFile );
468463
469464 fileWriter = new FileWriter (gitPropsFile );
470465 properties .store (fileWriter , "Generated by Git-Commit-Id-Plugin" );
466+
467+ log ("Writing properties file to [" , gitPropsFile .getAbsolutePath (), "] (for module " , project .getName () + (++counter ), ")..." );
468+
471469 } catch (IOException ex ) {
472470 throw new RuntimeException ("Cannot create custom git properties file: " + gitPropsFile , ex );
473471 } finally {
@@ -518,8 +516,8 @@ private boolean isNotEmpty(@Nullable String value) {
518516 return null != value && !" " .equals (value .trim ().replaceAll (" " , "" ));
519517 }
520518
521- void log (String ... interpolations ) {
522- loggerBridge .log ((Object []) interpolations );
519+ void log (String ... parts ) {
520+ loggerBridge .log ((Object []) parts );
523521 }
524522
525523 private boolean directoryExists (@ Nullable File fileLocation ) {
@@ -529,30 +527,49 @@ private boolean directoryExists(@Nullable File fileLocation) {
529527 private boolean directoryDoesNotExits (File fileLocation ) {
530528 return !directoryExists (fileLocation );
531529 }
532-
530+
533531 /**
534532 * If running within Jenkins/Hudosn, honor the branch name passed via GIT_BRANCH env var. This
535533 * is necessary because Jenkins/Hudson alwways invoke build in a detached head state.
536- *
534+ *
537535 * @param git
536+ * @param env
538537 * @return results of git.getBranch() or, if in Jenkins/Hudson, value of GIT_BRANCH
539538 */
540- protected String determineBranchName (Repository git ) throws IOException {
541- Map <String ,String > env = System .getenv ();
542- return determineBranchName (git ,env );
543- }
544-
545- protected String determineBranchName (Repository git , Map <String ,String > env ) throws IOException {
546- String branch = git .getBranch ();
547-
548- // Special processing if we're in Jenkins/Hudson
549- if (env .containsKey ("HUDSON_URL" ) || env .containsKey ("JENKINS_URL" )) {
550- String branchName = env .get ("GIT_BRANCH" );
551- if (branchName !=null && branchName .length ()>0 ) {
552- branch =branchName ;
553- }
554- }
555- return branch ;
539+ protected String determineBranchName (Repository git , Map <String , String > env ) throws IOException {
540+ if (runningOnBuildServer (env )) {
541+ return determineBranchNameOnBuildServer (git , env );
542+ } else {
543+ return git .getBranch ();
544+ }
545+ }
546+
547+ /**
548+ * Is "Jenkins aware", and prefers {@code GIT_BRANCH} to getting the branch via git if that enviroment variable is set.
549+ * The {@GIT_BRANCH} variable is set by Jenkins/Hudson when put in detached HEAD state, but it still knows which branch was cloned.
550+ */
551+ protected String determineBranchNameOnBuildServer (Repository git , Map <String , String > env ) throws IOException {
552+ String enviromentBasedBranch = env .get ("GIT_BRANCH" );
553+ if (isNullOrEmpty (enviromentBasedBranch )) {
554+ log ("Detected that running on CI enviroment, but using repository branch, no GIT_BRANCH detected." );
555+ return git .getBranch ();
556+ }else {
557+ log ("Using enviroment variable based branch name." , "GIT_BRANCH =" , enviromentBasedBranch );
558+ return enviromentBasedBranch ;
559+ }
560+ }
561+
562+ /**
563+ * Detects if we're running on Jenkins or Hudson, based on expected env variables.
564+ * <p/>
565+ * TODO: How can we detect Bamboo, TeamCity etc? Pull requests welcome.
566+ *
567+ * @return true if running
568+ * @see <a href="https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables">JenkinsSetEnvironmentVariables</a>
569+ * @param env
570+ */
571+ private boolean runningOnBuildServer (Map <String , String > env ) {
572+ return env .containsKey ("HUDSON_URL" ) || env .containsKey ("JENKINS_URL" );
556573 }
557574
558575 // SETTERS FOR TESTS ----------------------------------------------------
0 commit comments