Skip to content

Commit f486e7f

Browse files
author
TheSnoozer
committed
Merge pull request #192 from Cousnouf/feature/new_parameters
Added includeOnlyProperties options as alternative to a huge list of excludeProperties. Also added project version as output parameter.
2 parents e77fb26 + db9c1a2 commit f486e7f

File tree

3 files changed

+129
-6
lines changed

3 files changed

+129
-6
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: 50 additions & 3 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";
@@ -295,6 +296,21 @@ public class GitCommitIdMojo extends AbstractMojo {
295296
@SuppressWarnings("UnusedDeclaration")
296297
private List<String> excludeProperties = Collections.emptyList();
297298

299+
/**
300+
* Can be used to include only certain properties into the resulting file.
301+
* Will be overruled by the exclude properties.
302+
*
303+
* Each value may be globbing, that is, you can write {@code git.commit.user.*} to include both, the {@code name},
304+
* as well as {@code email} properties into the resulting files.
305+
*
306+
* Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}).
307+
*
308+
* @parameter
309+
* @since 2.1.14
310+
*/
311+
@SuppressWarnings("UnusedDeclaration")
312+
private List<String> includeOnlyProperties = Collections.emptyList();
313+
298314
/**
299315
* The Maven Session Object
300316
*
@@ -357,9 +373,10 @@ public void execute() throws MojoExecutionException {
357373
prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
358374

359375
loadGitData(properties);
360-
loadBuildTimeData(properties);
376+
loadBuildVersionAndTimeData(properties);
361377
loadBuildHostData(properties);
362378
loadShortDescribe(properties);
379+
filter(properties, includeOnlyProperties);
363380
filterNot(properties, excludeProperties);
364381
logProperties(properties);
365382

@@ -378,7 +395,7 @@ public void execute() throws MojoExecutionException {
378395
}
379396

380397
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
381-
if (exclusions == null) {
398+
if (exclusions == null || exclusions.isEmpty()) {
382399
return;
383400
}
384401

@@ -402,6 +419,31 @@ public Predicate<CharSequence> apply(String exclude) {
402419
}
403420
}
404421

422+
private void filter(Properties properties, @Nullable List<String> inclusions) {
423+
if (inclusions == null || inclusions.isEmpty()) {
424+
return;
425+
}
426+
427+
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
428+
@Override
429+
public Predicate<CharSequence> apply(String exclude) {
430+
return Predicates.containsPattern(exclude);
431+
}
432+
});
433+
434+
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
435+
for (Predicate<CharSequence> predicate : includePredicates) {
436+
shouldInclude = Predicates.or(shouldInclude, predicate);
437+
}
438+
439+
for (String key : properties.stringPropertyNames()) {
440+
if (!shouldInclude.apply(key)) {
441+
loggerBridge.debug("!shouldInclude.apply(" + key + ") = " + shouldInclude.apply(key));
442+
properties.remove(key);
443+
}
444+
}
445+
}
446+
405447
/**
406448
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
407449
* If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
@@ -470,10 +512,11 @@ private boolean isOurProperty(@NotNull String keyString) {
470512
return keyString.startsWith(prefixDot);
471513
}
472514

473-
void loadBuildTimeData(@NotNull Properties properties) {
515+
void loadBuildVersionAndTimeData(@NotNull Properties properties) {
474516
Date buildDate = new Date();
475517
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
476518
put(properties, BUILD_TIME, smf.format(buildDate));
519+
put(properties, BUILD_VERSION, project.getVersion());
477520
}
478521

479522
void loadBuildHostData(@NotNull Properties properties) {
@@ -756,6 +799,10 @@ public void setExcludeProperties(List<String> excludeProperties) {
756799
this.excludeProperties = excludeProperties;
757800
}
758801

802+
public void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
803+
this.includeOnlyProperties = includeOnlyProperties;
804+
}
805+
759806
public void useNativeGit(boolean useNativeGit) {
760807
this.useNativeGit = useNativeGit;
761808
}

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
package pl.project13.maven.git;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableMap;
2122
import com.google.common.collect.Maps;
2223
import org.apache.maven.project.MavenProject;
2324
import org.eclipse.jgit.lib.Repository;
2425
import org.junit.Before;
2526
import org.junit.Test;
26-
import pl.project13.maven.git.log.StdOutLoggerBridge;
2727

2828
import java.io.File;
2929
import java.io.IOException;
@@ -53,21 +53,21 @@ public void setUp() throws Exception {
5353
String prefix = "git";
5454
int abbrevLength = 7;
5555
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
56-
boolean verbose = true;
5756

5857
mojo = new GitCommitIdMojo();
5958
mojo.setDotGitDirectory(dotGitDirectory);
6059
mojo.setPrefix(prefix);
6160
mojo.setAbbrevLength(abbrevLength);
6261
mojo.setDateFormat(dateFormat);
63-
mojo.setVerbose(verbose);
62+
mojo.setVerbose(true);
6463
mojo.useNativeGit(false);
6564
mojo.setGitDescribe(gitDescribeConfig);
6665

6766

6867
mojo.runningTests = true;
6968
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
7069
when(mojo.project.getPackaging()).thenReturn("jar");
70+
when(mojo.project.getVersion()).thenReturn("3.3-SNAPSHOT");
7171

7272
jGitProvider = JGitProvider.on(mojo.lookupGitDirectory(), mojo.getLoggerBridge());
7373
}
@@ -123,6 +123,67 @@ public void shouldExcludeAsConfiguredProperties() throws Exception {
123123
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
124124
}
125125

126+
@Test
127+
public void shouldIncludeOnlyAsConfiguredProperties() throws Exception {
128+
// given
129+
mojo.setIncludeOnlyProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*", "^git.commit.id$"));
130+
131+
// when
132+
mojo.execute();
133+
134+
// then
135+
Properties properties = mojo.getProperties();
136+
137+
// explicitly included
138+
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
139+
140+
// glob included
141+
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
142+
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
143+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
144+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
145+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
146+
147+
// these excluded
148+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.branch"));
149+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.id.abbrev"));
150+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.message.full"));
151+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.message.short"));
152+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.time"));
153+
}
154+
155+
@Test
156+
public void shouldExcludeAndIncludeAsConfiguredProperties() throws Exception {
157+
// given
158+
mojo.setIncludeOnlyProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*"));
159+
mojo.setExcludeProperties(ImmutableList.of("git.build.user.email"));
160+
161+
// when
162+
mojo.execute();
163+
164+
// then
165+
Properties properties = mojo.getProperties();
166+
167+
// explicitly included
168+
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
169+
170+
// explicitly excluded -> overrules include only properties
171+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.email"));
172+
173+
// glob included
174+
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
175+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
176+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
177+
178+
// these excluded
179+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.branch"));
180+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.id"));
181+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.id.abbrev"));
182+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.message.full"));
183+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.message.short"));
184+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.time"));
185+
}
186+
126187
@Test
127188
@SuppressWarnings("")
128189
public void shouldHaveNoPrefixWhenConfiguredPrefixIsEmptyStringAsConfiguredProperties() throws Exception {

0 commit comments

Comments
 (0)