Skip to content

Commit 41152b4

Browse files
committed
Merge pull request #83 from soboko/feature/jsonSupport
Added ability to generate a git properties file in json format.
2 parents 399578b + f8f527a commit 41152b4

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
<version>${maven-plugin-api.version}</version>
9595
</dependency>
9696

97+
<dependency>
98+
<groupId>com.fasterxml.jackson.core</groupId>
99+
<artifactId>jackson-databind</artifactId>
100+
<version>2.2.3</version>
101+
</dependency>
102+
97103
<dependency>
98104
<groupId>com.google.inject</groupId>
99105
<artifactId>guice</artifactId>

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import com.google.common.annotations.VisibleForTesting;
2122
import com.google.common.io.Closeables;
2223
import com.google.common.io.Files;
@@ -188,6 +189,14 @@ public class GitCommitIdMojo extends AbstractMojo {
188189
@SuppressWarnings("UnusedDeclaration")
189190
private int abbrevLength;
190191

192+
/**
193+
* The format to save properties in. Valid options are "properties" (default) and "json".
194+
*
195+
* @parameter default-value="properties"
196+
*/
197+
@SuppressWarnings("UnusedDeclaration")
198+
private String format;
199+
191200
/**
192201
* The prefix to expose the properties on, for example 'git' would allow you to access '${git.branch}'
193202
*
@@ -462,9 +471,14 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
462471
Files.createParentDirs(gitPropsFile);
463472

464473
fileWriter = new FileWriter(gitPropsFile);
465-
properties.store(fileWriter, "Generated by Git-Commit-Id-Plugin");
466-
467-
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName() + (++counter), ")...");
474+
if ("json".equalsIgnoreCase(format)) {
475+
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName() + (++counter), ")...");
476+
ObjectMapper mapper = new ObjectMapper();
477+
mapper.writeValue(fileWriter, properties);
478+
} else {
479+
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName() + (++counter), ")...");
480+
properties.store(fileWriter, "Generated by Git-Commit-Id-Plugin");
481+
}
468482

469483
} catch (IOException ex) {
470484
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
@@ -554,7 +568,7 @@ protected String determineBranchNameOnBuildServer(Repository git, Map<String, St
554568
log("Detected that running on CI enviroment, but using repository branch, no GIT_BRANCH detected.");
555569
return git.getBranch();
556570
}else {
557-
log("Using enviroment variable based branch name.", "GIT_BRANCH =", enviromentBasedBranch);
571+
log("Using environment variable based branch name.", "GIT_BRANCH =", enviromentBasedBranch);
558572
return enviromentBasedBranch;
559573
}
560574
}
@@ -574,6 +588,10 @@ private boolean runningOnBuildServer(Map<String, String> env) {
574588

575589
// SETTERS FOR TESTS ----------------------------------------------------
576590

591+
public void setFormat(String format) {
592+
this.format = format;
593+
}
594+
577595
public void setVerbose(boolean verbose) {
578596
this.verbose = verbose;
579597
}

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

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@
1919

2020
import org.apache.maven.plugin.MojoExecutionException;
2121
import org.apache.maven.project.MavenProject;
22-
import org.fest.util.Files;
22+
import org.codehaus.plexus.util.FileUtils;
2323
import org.junit.Test;
24+
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.google.common.io.Files;
2427
import pl.project13.maven.git.FileSystemMavenSandbox.CleanUp;
2528
import pl.project13.test.utils.AssertException;
2629

2730
import java.io.File;
31+
import java.nio.charset.Charset;
32+
import java.util.HashMap;
33+
import java.util.Map;
2834
import java.util.Properties;
2935

3036
import static org.fest.assertions.Assertions.assertThat;
37+
import static org.junit.Assert.assertEquals;
3138
import static org.mockito.internal.util.reflection.Whitebox.setInternalState;
3239

3340
public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest {
@@ -128,31 +135,65 @@ public void run() throws Exception {
128135
}
129136

130137
@Test
131-
public void shouldGenerateCustomPropertiesFile() throws Exception {
132-
// given
133-
mavenSandbox.withParentProject("my-pom-project", "pom")
134-
.withChildProject("my-jar-module", "jar")
135-
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
136-
.create(CleanUp.CLEANUP_FIRST);
138+
public void shouldGenerateCustomPropertiesFileProperties() throws Exception {
139+
// given
140+
mavenSandbox.withParentProject("my-pom-project", "pom")
141+
.withChildProject("my-jar-module", "jar")
142+
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
143+
.create(CleanUp.CLEANUP_FIRST);
137144

138-
MavenProject targetProject = mavenSandbox.getChildProject();
145+
MavenProject targetProject = mavenSandbox.getChildProject();
139146

140-
String targetFilePath = "target/classes/custom-git.properties";
141-
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
147+
String targetFilePath = "target/classes/custom-git.properties";
148+
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
142149

143-
setProjectToExecuteMojoIn(targetProject);
144-
alterMojoSettings("generateGitPropertiesFile", true);
145-
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
150+
setProjectToExecuteMojoIn(targetProject);
151+
alterMojoSettings("generateGitPropertiesFile", true);
152+
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
146153

147-
// when
148-
try {
149-
mojo.execute();
150-
151-
// then
152-
assertThat(expectedFile).exists();
153-
} finally {
154-
Files.delete(expectedFile);
155-
}
154+
// when
155+
try {
156+
mojo.execute();
157+
158+
// then
159+
assertThat(expectedFile).exists();
160+
} finally {
161+
FileUtils.forceDelete(expectedFile);
162+
}
163+
}
164+
165+
@Test
166+
public void shouldGenerateCustomPropertiesFileJson() throws Exception {
167+
// given
168+
mavenSandbox.withParentProject("my-pom-project", "pom")
169+
.withChildProject("my-jar-module", "jar")
170+
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
171+
.create(CleanUp.CLEANUP_FIRST);
172+
173+
MavenProject targetProject = mavenSandbox.getChildProject();
174+
175+
String targetFilePath = "target/classes/custom-git.properties";
176+
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
177+
178+
setProjectToExecuteMojoIn(targetProject);
179+
alterMojoSettings("generateGitPropertiesFile", true);
180+
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
181+
alterMojoSettings("format", "json");
182+
183+
// when
184+
try {
185+
mojo.execute();
186+
187+
// then
188+
assertThat(expectedFile).exists();
189+
String json = Files.toString(expectedFile, Charset.defaultCharset());
190+
ObjectMapper om = new ObjectMapper();
191+
Map<String, String> map = new HashMap<String, String>();
192+
map = om.readValue(expectedFile, map.getClass());
193+
assertThat(map.size() > 10);
194+
} finally {
195+
FileUtils.forceDelete(expectedFile);
196+
}
156197
}
157198

158199
private void alterMojoSettings(String parameterName, Object parameterValue) {

0 commit comments

Comments
 (0)