Skip to content

Commit 453942b

Browse files
committed
Merge pull request #125 from TheSnoozer/master
Fix for #124
2 parents 6ec2d90 + 3e6d365 commit 453942b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,21 +486,19 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
486486
jGitProvider.loadGitData(properties);
487487
}
488488

489-
static int counter;
490-
491489
void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
492490
FileWriter fileWriter = null;
493-
File gitPropsFile = new File(base, propertiesFilename);
491+
File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
494492
try {
495493
Files.createParentDirs(gitPropsFile);
496494

497495
fileWriter = new FileWriter(gitPropsFile);
498496
if ("json".equalsIgnoreCase(format)) {
499-
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName() + (++counter), ")...");
497+
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
500498
ObjectMapper mapper = new ObjectMapper();
501499
mapper.writeValue(fileWriter, properties);
502500
} else {
503-
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName() + (++counter), ")...");
501+
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
504502
properties.store(fileWriter, "Generated by Git-Commit-Id-Plugin");
505503
}
506504

@@ -511,6 +509,19 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
511509
}
512510
}
513511

512+
@VisibleForTesting
513+
File craftPropertiesOutputFile(File base, String propertiesFilename){
514+
File returnPath = new File(base, propertiesFilename);
515+
516+
File currentPropertiesFilepath = new File(propertiesFilename);
517+
if(currentPropertiesFilepath.isAbsolute()){
518+
returnPath = currentPropertiesFilepath;
519+
}
520+
521+
return returnPath;
522+
}
523+
524+
514525
boolean isPomProject(@NotNull MavenProject project) {
515526
return project.getPackaging().equalsIgnoreCase("pom");
516527
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,27 @@ private void assertShortDescribe(String commitDescribe, String expectedShortDesc
211211
commitIdMojo.loadShortDescribe(prop);
212212
assertThat(prop.getProperty(GitCommitIdMojo.COMMIT_SHORT_DESCRIBE)).isEqualTo(expectedShortDescribe);
213213
}
214+
215+
@Test
216+
public void testCraftPropertiesOutputFileWithRelativePath() throws IOException {
217+
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
218+
File baseDir = new File(".");
219+
String targetDir = baseDir.getCanonicalPath() + "/";
220+
String generateGitPropertiesFilename = "target/classes/git.properties";
221+
222+
File result = commitIdMojo.craftPropertiesOutputFile(baseDir, generateGitPropertiesFilename);
223+
assertThat(result.getCanonicalPath()).isEqualTo(targetDir + generateGitPropertiesFilename);
224+
}
225+
226+
@Test
227+
public void testCraftPropertiesOutputFileWithFullPath() throws IOException {
228+
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
229+
File baseDir = new File(".");
230+
String targetDir = baseDir.getCanonicalPath() + "/";
231+
String generateGitPropertiesFilename = targetDir + "target/classes/git.properties";
232+
233+
File result = commitIdMojo.craftPropertiesOutputFile(baseDir, generateGitPropertiesFilename);
234+
assertThat(result.getCanonicalPath()).isEqualTo(generateGitPropertiesFilename);
235+
}
236+
214237
}

0 commit comments

Comments
 (0)