Skip to content

Commit e3c83da

Browse files
committed
Added includeProperties and additionalProperties as mojo parameters.
1 parent e77fb26 commit e3c83da

File tree

2 files changed

+156
-4
lines changed

2 files changed

+156
-4
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,35 @@ public class GitCommitIdMojo extends AbstractMojo {
295295
@SuppressWarnings("UnusedDeclaration")
296296
private List<String> excludeProperties = Collections.emptyList();
297297

298+
/**
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.
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> 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();
326+
298327
/**
299328
* The Maven Session Object
300329
*
@@ -360,7 +389,9 @@ public void execute() throws MojoExecutionException {
360389
loadBuildTimeData(properties);
361390
loadBuildHostData(properties);
362391
loadShortDescribe(properties);
392+
filter(properties, includeProperties);
363393
filterNot(properties, excludeProperties);
394+
addAdditionalProperties(properties, additionalProperties);
364395
logProperties(properties);
365396

366397
if (generateGitPropertiesFile) {
@@ -378,7 +409,7 @@ public void execute() throws MojoExecutionException {
378409
}
379410

380411
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
381-
if (exclusions == null) {
412+
if (exclusions == null || exclusions.isEmpty()) {
382413
return;
383414
}
384415

@@ -402,6 +433,37 @@ public Predicate<CharSequence> apply(String exclude) {
402433
}
403434
}
404435

436+
private void filter(Properties properties, @Nullable List<String> inclusions) {
437+
if (inclusions == null || inclusions.isEmpty()) {
438+
return;
439+
}
440+
441+
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
442+
@Override
443+
public Predicate<CharSequence> apply(String exclude) {
444+
return Predicates.containsPattern(exclude);
445+
}
446+
});
447+
448+
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
449+
for (Predicate<CharSequence> predicate : includePredicates) {
450+
shouldInclude = Predicates.or(shouldInclude, predicate);
451+
}
452+
453+
for (String key : properties.stringPropertyNames()) {
454+
if (!shouldInclude.apply(key)) {
455+
loggerBridge.debug("!shouldInclude.apply(" + key + ") = " + shouldInclude.apply(key));
456+
properties.remove(key);
457+
}
458+
}
459+
}
460+
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+
405467
/**
406468
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
407469
* If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
@@ -756,6 +818,14 @@ public void setExcludeProperties(List<String> excludeProperties) {
756818
this.excludeProperties = excludeProperties;
757819
}
758820

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;
827+
}
828+
759829
public void useNativeGit(boolean useNativeGit) {
760830
this.useNativeGit = useNativeGit;
761831
}

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

Lines changed: 85 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,14 +53,13 @@ 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

@@ -123,6 +122,89 @@ public void shouldExcludeAsConfiguredProperties() throws Exception {
123122
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
124123
}
125124

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

0 commit comments

Comments
 (0)