Skip to content

Commit 71c58dc

Browse files
author
TheSnoozer
committed
Merge pull request #212 from TheSnoozer/master
#211 : add a switch to generate the git.commit.id the "old" way
2 parents f7b1671 + fde0620 commit 71c58dc

File tree

9 files changed

+141
-10
lines changed

9 files changed

+141
-10
lines changed

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ You can check the available versions by visiting [search.maven.org](http://searc
4444

4545
Migration Issues you may come across when using the latest 2.2.X
4646
-----------------------------
47-
If you are already using the git-commit-id-plugin and would like to move to the latest major release (2.2.X) there are some design choices we made to fix some of your issues.
48-
1. We dropped the support of Java 1.6 (if you still rely on this version, the version 2.1.15 still has support for this and you may want to check the fixed issues since then before reporting a new one)
49-
2. We renamed git.commit.id to git.commit.id.full to enable full compability with json (see https://github.com/ktoso/maven-git-commit-id-plugin/issues/122)
50-
51-
I think especially the second one will strike all of our users and we really would like to apologize for any inconvenience :-)
47+
If you are already using the git-commit-id-plugin and would like to move to the latest major release (2.2.X) there are some design choices that resulted in the fact that we needed to dropped the support of Java 1.6 (if you still rely on this version, the version 2.1.15 still has support for this and you may want to check the fixed issues since then before reporting a new one)
5248

5349
Getting SNAPSHOT versions of the plugin
5450
---------------------------------------
@@ -256,6 +252,23 @@ It's really simple to setup this plugin; below is a sample pom that you may base
256252
-->
257253
<abbrevLength>7</abbrevLength>
258254

255+
256+
<!-- @since v2.2.0 -->
257+
<!--
258+
The option can be used to tell the plugin how it should generate the 'git.commit.id' property. Due to some naming issues when exporting the properties as an json-object (https://github.com/ktoso/maven-git-commit-id-plugin/issues/122) we needed to make it possible to export all properties as a valid json-object.
259+
Due to the fact that this is one of the major properties the plugin is exporting we just don't want to change the exporting mechanism and somehow throw the backwards compatibility away.
260+
We rather provide a convient switch where you can choose if you would like the properties as they always had been, or if you rather need to support full json-object compatibility.
261+
In the case you need to fully support json-object we unfortunately need to change the 'git.commit.id' property from 'git.commit.id' to 'git.commit.id.full' in the exporting mechanism to allow the generation of a fully valid json object.
262+
263+
Currently the switch allows two different options:
264+
1. By default this property is set to 'flat' and will generate the formerly known property 'git.commit.id' as it was in the previous versions of the plugin. Keeping it to 'flat' by default preserve backwards compatibility and does not require further adjustments by the end user.
265+
2. If you set this switch to 'full' the plugin will export the formerly known property 'git.commit.id' as 'git.commit.id.full' and therefore will generate a fully valid json object in the exporting mechanism.
266+
267+
*Note*: Depending on your plugin configuration you obviously can choose the 'prefix' of your properties by setting it accordingly in the plugin's configuration. As a result this is therefore only an illustration what the switch means when the 'prefix' is set to it's default value.
268+
*Note*: If you set the value to something that's not equal to 'flat' or 'full' (ignoring the case) the plugin will output a warning and will fallback to the default 'flat' mode.
269+
-->
270+
<commitIdGenerationMode>flat</commitIdGenerationMode>
271+
259272
<!-- @since 2.1.0 -->
260273
<!--
261274
read up about git-describe on the in man, or it's homepage - it's a really powerful versioning helper
@@ -313,8 +326,9 @@ git.tags=${git.tags}
313326
git.branch=${git.branch}
314327
git.dirty=${git.dirty}
315328
git.remote.origin.url=${git.remote.origin.url}
316-
317-
git.commit.id.full=${git.commit.id.full}
329+
git.commit.id=${git.commit.id}
330+
OR (depends on commitIdGenerationMode)
331+
git.commit.id.full=${git.commit.id.full}
318332
git.commit.id.abbrev=${git.commit.id.abbrev}
319333
git.commit.id.describe=${git.commit.id.describe}
320334
git.commit.id.describe-short=${git.commit.id.describe-short}
@@ -353,6 +367,7 @@ Start out with with adding the above steps to your project, next paste this **gi
353367
<property name="remoteOriginUrl" value="${git.remote.origin.url}"/>
354368

355369
<property name="commitId" value="${git.commit.id.full}"/>
370+
<!-- OR value="${git.commit.id}" depending on your configuration of commitIdGenerationMode -->
356371
<property name="commitIdAbbrev" value="${git.commit.id.abbrev}"/>
357372
<property name="describe" value="${git.commit.id.describe}"/>
358373
<property name="describeShort" value="${git.commit.id.describe-short}"/>
@@ -392,7 +407,7 @@ public class GitRepositoryState {
392407
String dirty; // =${git.dirty}
393408
String remoteOriginUrl; // =${git.remote.origin.url}
394409

395-
String commitId; // =${git.commit.id.full}
410+
String commitId; // =${git.commit.id.full} OR ${git.commit.id}
396411
String commitIdAbbrev; // =${git.commit.id.abbrev}
397412
String describe; // =${git.commit.id.describe}
398413
String describeShort; // =${git.commit.id.describe-short}
@@ -523,7 +538,7 @@ public GitRepositoryState(Properties properties)
523538
this.dirty = properties.get("git.dirty").toString();
524539
this.remoteOriginUrl = properties.get("git.remote.origin.url").toString();
525540

526-
this.commitId = properties.get("git.commit.id.full").toString();
541+
this.commitId = properties.get("git.commit.id.full").toString(); // OR properties.get("git.commit.id") depending on your configuration
527542
this.commitIdAbbrev = properties.get("git.commit.id.abbrev").toString();
528543
this.describe = properties.get("git.commit.id.describe").toString();
529544
this.describeShort = properties.get("git.commit.id.describe-short").toString();
@@ -655,7 +670,12 @@ Optional parameters:
655670
* **includeOnlyProperties** - `(default: empty)` *(available since v2.1.14)* - Allows to include only properties that you want to expose. This feature was implemented to avoid big exclude properties tag when we only want very few specific properties.
656671
* **useNativeGit** - `(default: false)` *(available since v2.1.10)* - Uses the native `git` binary instead of the custom `jgit` implementation shipped with this plugin to obtain all information. Although this should usualy give your build some performance boost, it may randomly break if you upgrade your git version and it decides to print information in a different format suddenly. As rule of thumb, keep using the default `jgit` implementation (keep this option set to `false`) until you notice performance problems within your build (usualy when you have *hundreds* of maven modules).
657672
* **abbrevLength** - `(default: 7)` *(available since v2.0.4)* - Configure the "git.commit.id.abbrev" property to be at least of length N (see gitDescribe abbrev for special case abbrev = 0).
673+
* **commitIdGenerationMode** - `(default: flat)` *(available since v2.2.0)* is an option that can be used to tell the plugin how it should generate the 'git.commit.id' property. Due to some naming issues when exporting the properties as an json-object (https://github.com/ktoso/maven-git-commit-id-plugin/issues/122) we needed to make it possible to export all properties as a valid json-object. Currently the switch allows two different options:
674+
1. By default this property is set to 'flat' and will generate the formerly known property 'git.commit.id' as it was in the previous versions of the plugin. Keeping it to 'flat' by default preserve backwards compatibility and does not require further adjustments by the end user.
675+
2. If you set this switch to 'full' the plugin will export the formerly known property 'git.commit.id' as 'git.commit.id.full' and therefore will generate a fully valid json object in the exporting mechanism.
658676

677+
*Note*: Depending on your plugin configuration you obviously can choose the 'prefix' of your properties by setting it accordingly in the plugin's configuration. As a result this is therefore only an illustration what the switch means when the 'prefix' is set to it's default value.
678+
*Note*: If you set the value to something that's not equal to 'flat' or 'full' (ignoring the case) the plugin will output a warning and will fallback to the default 'flat' mode.
659679

660680
**gitDescribe**:
661681
Worth pointing out is, that git-commit-id tries to be 1-to-1 compatible with git's plain output, even though the describe functionality has been reimplemented manually using JGit (you don't have to have a git executable to use the plugin). So if you're familiar with [git-describe](https://github.com/ktoso/maven-git-commit-id-plugin#git-describe---short-intro-to-an-awesome-command), you probably can skip this section, as it just explains the same options that git provides.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <[email protected]>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package pl.project13.maven.git;
18+
19+
public enum CommitIdGenerationModeEnum{
20+
FULL,
21+
FLAT,
22+
UNKNOWN;
23+
24+
public static CommitIdGenerationModeEnum getValue(String o){
25+
if(o != null){
26+
for(CommitIdGenerationModeEnum v : values()){
27+
if(v.name().toString().equalsIgnoreCase(o)){
28+
return v;
29+
}
30+
}
31+
}
32+
return CommitIdGenerationModeEnum.UNKNOWN;
33+
}
34+
}

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public class GitCommitIdMojo extends AbstractMojo {
6666
// these properties will be exposed to maven
6767
public static final String BRANCH = "branch";
6868
public static final String DIRTY = "dirty";
69-
public static final String COMMIT_ID = "commit.id.full";
69+
public static final String COMMIT_ID_FLAT = "commit.id";
70+
public static final String COMMIT_ID_FULL = "commit.id.full";
71+
public static String COMMIT_ID = COMMIT_ID_FLAT;
72+
7073
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
7174
public static final String COMMIT_DESCRIBE = "commit.id.describe";
7275
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.describe-short";
@@ -327,6 +330,24 @@ public class GitCommitIdMojo extends AbstractMojo {
327330
@SuppressWarnings("UnusedDeclaration")
328331
private List<String> includeOnlyProperties = Collections.emptyList();
329332

333+
/**
334+
* The option can be used to tell the plugin how it should generate the 'git.commit.id' property. Due to some naming issues when exporting the properties as an json-object (https://github.com/ktoso/maven-git-commit-id-plugin/issues/122) we needed to make it possible to export all properties as a valid json-object.
335+
* Due to the fact that this is one of the major properties the plugin is exporting we just don't want to change the exporting mechanism and somehow throw the backwards compatibility away.
336+
* We rather provide a convient switch where you can choose if you would like the properties as they always had been, or if you rather need to support full json-object compatibility.
337+
* In the case you need to fully support json-object we unfortunately need to change the 'git.commit.id' property from 'git.commit.id' to 'git.commit.id.full' in the exporting mechanism to allow the generation of a fully valid json object.
338+
*
339+
* Currently the switch allows two different options:
340+
* 1. By default this property is set to 'flat' and will generate the formerly known property 'git.commit.id' as it was in the previous versions of the plugin. Keeping it to 'flat' by default preserve backwards compatibility and does not require further adjustments by the end user.
341+
* 2. If you set this switch to 'full' the plugin will export the formerly known property 'git.commit.id' as 'git.commit.id.full' and therefore will generate a fully valid json object in the exporting mechanism.
342+
*
343+
* *Note*: Depending on your plugin configuration you obviously can choose the 'prefix' of your properties by setting it accordingly in the plugin's configuration. As a result this is therefore only an illustration what the switch means when the 'prefix' is set to it's default value.
344+
* *Note*: If you set the value to something that's not equal to 'flat' or 'full' (ignoring the case) the plugin will output a warning and will fallback to the default 'flat' mode.
345+
*
346+
* @parameter default-value="flat"
347+
* @since 2.2.0
348+
*/
349+
private String commitIdGenerationMode;
350+
330351
/**
331352
* The Maven Session Object
332353
*
@@ -383,6 +404,17 @@ public void execute() throws MojoExecutionException {
383404
}
384405

385406
try {
407+
switch(CommitIdGenerationModeEnum.getValue(commitIdGenerationMode)){
408+
default:
409+
loggerBridge.warn("Detected wrong setting for 'commitIdGenerationMode' will fallback to default 'flat'-Mode!");
410+
case FLAT:
411+
COMMIT_ID = COMMIT_ID_FLAT;
412+
break;
413+
case FULL:
414+
COMMIT_ID = COMMIT_ID_FULL;
415+
break;
416+
}
417+
386418
properties = initProperties();
387419

388420
String trimmedPrefix = prefix.trim();
@@ -828,6 +860,10 @@ public void useNativeGit(boolean useNativeGit) {
828860
this.useNativeGit = useNativeGit;
829861
}
830862

863+
public void setCommitIdGenerationMode(String commitIdGenerationMode){
864+
this.commitIdGenerationMode = commitIdGenerationMode;
865+
}
866+
831867
public LoggerBridge getLoggerBridge() {
832868
return loggerBridge;
833869
}

src/main/java/pl/project13/maven/git/log/LoggerBridge.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public interface LoggerBridge {
2121
void log(Object... parts);
2222
void error(Object... parts);
23+
void warn(Object... parts);
2324
void debug(Object... parts);
2425
void setVerbose(boolean verbose);
2526
}

src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public void error(Object... parts) {
6161
}
6262
}
6363

64+
@Override
65+
public void warn(Object... parts) {
66+
if (verbose) {
67+
logger.warn(Joiner.on(" ").useForNull("null").join(parts));
68+
}
69+
}
70+
6471
@Override
6572
public void debug(Object... parts) {
6673
if (verbose) {

src/main/java/pl/project13/maven/git/log/StdOutLoggerBridge.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public void error(Object... parts) {
4040
System.out.println("ERR: " + Joiner.on(" ").join(parts));
4141
}
4242
}
43+
44+
@Override
45+
public void warn(Object... parts) {
46+
if(verbose) {
47+
System.out.println("WRN: " + Joiner.on(" ").join(parts));
48+
}
49+
}
4350

4451
@Override
4552
public void debug(Object... parts) {

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,30 @@ public void shouldUseDateFormatTimeZone(boolean useNativeGit) throws Exception {
792792
TimeZone.setDefault(currentDefaultTimeZone);
793793
}
794794

795+
@Test
796+
@Parameters(method = "useNativeGit")
797+
public void shouldGenerateCommitIdOldFashioned(boolean useNativeGit) throws Exception {
798+
// given
799+
mavenSandbox.withParentProject("my-pom-project", "pom")
800+
.withChildProject("my-jar-module", "jar")
801+
.withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG_DIRTY)
802+
.create(CleanUp.CLEANUP_FIRST);
803+
MavenProject targetProject = mavenSandbox.getChildProject();
804+
805+
setProjectToExecuteMojoIn(targetProject);
806+
807+
alterMojoSettings("useNativeGit", useNativeGit);
808+
alterMojoSettings("commitIdGenerationMode", "flat");
809+
810+
// when
811+
mojo.execute();
812+
813+
// then
814+
Properties properties = targetProject.getProperties();
815+
assertThat(properties.stringPropertyNames()).contains("git.commit.id");
816+
assertThat(properties.stringPropertyNames()).excludes("git.commit.id.full");
817+
}
818+
795819
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {
796820
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
797821
gitDescribeConfig.setTags(true);

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void setUp() throws Exception {
6161
mojo.setVerbose(true);
6262
mojo.useNativeGit(false);
6363
mojo.setGitDescribe(gitDescribeConfig);
64+
mojo.setCommitIdGenerationMode("full");
6465

6566

6667
mojo.runningTests = true;

src/test/java/pl/project13/maven/git/GitIntegrationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static void initializeMojoWithDefaults(GitCommitIdMojo mojo) {
7676
mojoDefaults.put("dateFormat", "dd.MM.yyyy '@' HH:mm:ss z");
7777
mojoDefaults.put("failOnNoGitDirectory", true);
7878
mojoDefaults.put("useNativeGit", false);
79+
mojoDefaults.put("commitIdGenerationMode", "full");
7980
for (Map.Entry<String, Object> entry : mojoDefaults.entrySet()) {
8081
setInternalState(mojo, entry.getKey(), entry.getValue());
8182
}

0 commit comments

Comments
 (0)