Skip to content

Commit db9c1a2

Browse files
committed
Removed additional properties and put the build version as a standard property like the build time.
1 parent e3c83da commit db9c1a2

File tree

3 files changed

+30
-59
lines changed

3 files changed

+30
-59
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ It's really simple to setup this plugin; below is a sample pom that you may base
204204
<!-- <excludeProperty>git.user.*</excludeProperty> -->
205205
</excludeProperties>
206206

207+
<!-- @since 2.1.14 -->
208+
<!--
209+
Can be used to include only certain properties into the resulting file.
210+
Will be overruled by the exclude properties.
211+
212+
Each value may be globbing, that is, you can write {@code git.commit.user.*} to include both, the {@code name},
213+
as well as {@code email} properties into the resulting files.
214+
215+
Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}).
216+
-->
217+
<includeOnlyProperties>
218+
<!-- <includeOnlyProperty>^git.commit.id$</includeOnlyProperty> -->
219+
</includeOnlyProperties>
220+
207221
<!-- @since 2.1.10 -->
208222
<!--
209223
false is default here, if set to true it uses native `git` excutable for extracting all data.
@@ -605,6 +619,7 @@ Optional parameters:
605619
* **skip** - `(default: false)` *(available since v2.1.8)* - Skip the plugin execution completely.
606620
* **runOnlyOnce** - `(default: false)` *(available since v2.1.12)* - Use with caution! In a multi-module build, only run once. This means that the plugins effects will only execute once, for the parent project. This probably won't "do the right thing" if your project has more than one git repository. Important: If you're using `generateGitPropertiesFile`, setting `runOnlyOnce` will make the plugin only generate the file in the directory where you started your build :warning:. The `git.*` maven properties are available in all modules.
607621
* **excludeProperties** - `(default: empty)` *(available since v2.1.9)* - Allows to filter out properties that you *don't* want to expose. This feature was implemented in response to [this issue](https://github.com/ktoso/maven-git-commit-id-plugin/issues/91), so if you're curious about the use-case, check that issue.
622+
* **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.
608623
* **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).
609624
* **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).
610625

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

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class GitCommitIdMojo extends AbstractMojo {
7373
public static final String BUILD_AUTHOR_NAME = "build.user.name";
7474
public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
7575
public static final String BUILD_TIME = "build.time";
76+
public static final String BUILD_VERSION = "build.version";
7677
public static final String BUILD_HOST = "build.host";
7778
public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
7879
public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
@@ -296,9 +297,8 @@ public class GitCommitIdMojo extends AbstractMojo {
296297
private List<String> excludeProperties = Collections.emptyList();
297298

298299
/**
299-
* Can be used to exclude certain properties from being emited into the resulting file.
300-
* May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?),
301-
* or the email of the committer etc.
300+
* Can be used to include only certain properties into the resulting file.
301+
* Will be overruled by the exclude properties.
302302
*
303303
* Each value may be globbing, that is, you can write {@code git.commit.user.*} to include both, the {@code name},
304304
* as well as {@code email} properties into the resulting files.
@@ -309,20 +309,7 @@ public class GitCommitIdMojo extends AbstractMojo {
309309
* @since 2.1.14
310310
*/
311311
@SuppressWarnings("UnusedDeclaration")
312-
private List<String> includeProperties = Collections.emptyList();
313-
314-
/**
315-
* Can be used to add some properties in order to gather everything in the same file.
316-
*
317-
* For instance, you can set the maven project version or any dependency information.
318-
*
319-
* The key is the property name, the value is the value.
320-
*
321-
* @parameter
322-
* @since 2.1.14
323-
*/
324-
@SuppressWarnings("UnusedDeclaration")
325-
private Map<String, String> additionalProperties = Collections.emptyMap();
312+
private List<String> includeOnlyProperties = Collections.emptyList();
326313

327314
/**
328315
* The Maven Session Object
@@ -386,12 +373,11 @@ public void execute() throws MojoExecutionException {
386373
prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
387374

388375
loadGitData(properties);
389-
loadBuildTimeData(properties);
376+
loadBuildVersionAndTimeData(properties);
390377
loadBuildHostData(properties);
391378
loadShortDescribe(properties);
392-
filter(properties, includeProperties);
379+
filter(properties, includeOnlyProperties);
393380
filterNot(properties, excludeProperties);
394-
addAdditionalProperties(properties, additionalProperties);
395381
logProperties(properties);
396382

397383
if (generateGitPropertiesFile) {
@@ -458,12 +444,6 @@ public Predicate<CharSequence> apply(String exclude) {
458444
}
459445
}
460446

461-
private void addAdditionalProperties(Properties properties, Map<String, String> additionalProperties) {
462-
for (Map.Entry<String, String> additionalProperty : additionalProperties.entrySet()) {
463-
properties.put(additionalProperty.getKey(), additionalProperty.getValue());
464-
}
465-
}
466-
467447
/**
468448
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
469449
* If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
@@ -532,10 +512,11 @@ private boolean isOurProperty(@NotNull String keyString) {
532512
return keyString.startsWith(prefixDot);
533513
}
534514

535-
void loadBuildTimeData(@NotNull Properties properties) {
515+
void loadBuildVersionAndTimeData(@NotNull Properties properties) {
536516
Date buildDate = new Date();
537517
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
538518
put(properties, BUILD_TIME, smf.format(buildDate));
519+
put(properties, BUILD_VERSION, project.getVersion());
539520
}
540521

541522
void loadBuildHostData(@NotNull Properties properties) {
@@ -818,12 +799,8 @@ public void setExcludeProperties(List<String> excludeProperties) {
818799
this.excludeProperties = excludeProperties;
819800
}
820801

821-
public void setIncludeProperties(List<String> includeProperties) {
822-
this.includeProperties = includeProperties;
823-
}
824-
825-
public void setAdditionalProperties(Map<String, String> additionalProperties) {
826-
this.additionalProperties = additionalProperties;
802+
public void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
803+
this.includeOnlyProperties = includeOnlyProperties;
827804
}
828805

829806
public void useNativeGit(boolean useNativeGit) {

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void setUp() throws Exception {
6767
mojo.runningTests = true;
6868
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
6969
when(mojo.project.getPackaging()).thenReturn("jar");
70+
when(mojo.project.getVersion()).thenReturn("3.3-SNAPSHOT");
7071

7172
jGitProvider = JGitProvider.on(mojo.lookupGitDirectory(), mojo.getLoggerBridge());
7273
}
@@ -123,9 +124,9 @@ public void shouldExcludeAsConfiguredProperties() throws Exception {
123124
}
124125

125126
@Test
126-
public void shouldIncludeAsConfiguredProperties() throws Exception {
127+
public void shouldIncludeOnlyAsConfiguredProperties() throws Exception {
127128
// given
128-
mojo.setIncludeProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*", "^git.commit.id$"));
129+
mojo.setIncludeOnlyProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*", "^git.commit.id$"));
129130

130131
// when
131132
mojo.execute();
@@ -154,7 +155,7 @@ public void shouldIncludeAsConfiguredProperties() throws Exception {
154155
@Test
155156
public void shouldExcludeAndIncludeAsConfiguredProperties() throws Exception {
156157
// given
157-
mojo.setIncludeProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*"));
158+
mojo.setIncludeOnlyProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*"));
158159
mojo.setExcludeProperties(ImmutableList.of("git.build.user.email"));
159160

160161
// when
@@ -166,7 +167,7 @@ public void shouldExcludeAndIncludeAsConfiguredProperties() throws Exception {
166167
// explicitly included
167168
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
168169

169-
// explicitly excluded
170+
// explicitly excluded -> overrules include only properties
170171
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.email"));
171172

172173
// glob included
@@ -183,28 +184,6 @@ public void shouldExcludeAndIncludeAsConfiguredProperties() throws Exception {
183184
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.time"));
184185
}
185186

186-
@Test
187-
public void shouldAddAdditionalProperties() throws Exception {
188-
// given
189-
String buildVersionKey = "build.version";
190-
String ExpectedBuildVersion = "2.0.14";
191-
String jGitVersionKey = "jgit.version";
192-
String expectedJGitVersion = "3.7.0.201502260915-r";
193-
mojo.setAdditionalProperties(ImmutableMap.of(buildVersionKey, ExpectedBuildVersion, jGitVersionKey, expectedJGitVersion));
194-
195-
// when
196-
mojo.execute();
197-
198-
// then
199-
Properties properties = mojo.getProperties();
200-
201-
// added properties
202-
assertThat(properties).satisfies(new ContainsKeyCondition(buildVersionKey));
203-
assertThat(properties).satisfies(new ContainsKeyCondition(jGitVersionKey));
204-
assertThat(properties.get(buildVersionKey)).isEqualTo(ExpectedBuildVersion);
205-
assertThat(properties.get(jGitVersionKey)).isEqualTo(expectedJGitVersion);
206-
}
207-
208187
@Test
209188
@SuppressWarnings("")
210189
public void shouldHaveNoPrefixWhenConfiguredPrefixIsEmptyStringAsConfiguredProperties() throws Exception {

0 commit comments

Comments
 (0)