Skip to content

Commit 0372376

Browse files
author
S L
committed
#211 : add a switch to generate the git.commit.id the "old" way
1 parent f7b1671 commit 0372376

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ 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+
@Deprecated
70+
public static final String COMMIT_ID_OLD = "commit.id";
71+
public static final String COMMIT_ID_NEW = "commit.id.full";
72+
public static String COMMIT_ID = COMMIT_ID_NEW;
73+
7074
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
7175
public static final String COMMIT_DESCRIBE = "commit.id.describe";
7276
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.describe-short";
@@ -327,6 +331,21 @@ public class GitCommitIdMojo extends AbstractMojo {
327331
@SuppressWarnings("UnusedDeclaration")
328332
private List<String> includeOnlyProperties = Collections.emptyList();
329333

334+
/**
335+
* The option can be used to tell the plugin how it should generate the formerly known property 'git.commit.id'. 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 change the export of the property from 'git.commit.id' to 'git.commit.id.full'.
336+
* However, 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.
337+
* That's the point where this switch comes into place!
338+
* By default it is set to 'true' and will generate the formerly known property 'git.commit.id' as it was in the previous versions of the plugin. With keeping the switch set to 'true' the plugin will print a warning that using this switch set to 'true' is deprecated and may be removed in a future release. However keeping it to 'true' by default preserve backwards compatibility and allows to migrate to the new properties when it's convenient.
339+
* If you set this switch to 'false' the plugin will export the formerly known property 'git.commit.id' to 'git.commit.id.full'.
340+
*
341+
* Note: Depending on your plugin configuration you obviously can choose the 'prefix' of your properties by setting the accordingly in the plugin's configuration. As a result this is therefore only an illustration what the switch means when 'prefix' is set to it's default value.
342+
*
343+
* @parameter default-value="true"
344+
* @since 2.2.0
345+
*/
346+
@Deprecated
347+
private boolean generateCommitIdOldFashioned;
348+
330349
/**
331350
* The Maven Session Object
332351
*
@@ -383,6 +402,15 @@ public void execute() throws MojoExecutionException {
383402
}
384403

385404
try {
405+
if(generateCommitIdOldFashioned){
406+
loggerBridge.warn("Using the property 'generateCommitIdOldFashioned' set to 'true' is deprecated and may be removed in a future release! Please refer to the readme on this issue.");
407+
COMMIT_ID = COMMIT_ID_OLD;
408+
}else{
409+
// need to have this for the tests due the fact that they are switching back and forth
410+
// for the end-user setting this shouldn't perform any changes (it's set to COMMIT_ID_NEW by default)
411+
COMMIT_ID = COMMIT_ID_NEW;
412+
}
413+
386414
properties = initProperties();
387415

388416
String trimmedPrefix = prefix.trim();

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("generateCommitIdOldFashioned", true);
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/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("generateCommitIdOldFashioned", false);
7980
for (Map.Entry<String, Object> entry : mojoDefaults.entrySet()) {
8081
setInternalState(mojo, entry.getKey(), entry.getValue());
8182
}

0 commit comments

Comments
 (0)