Skip to content

Commit bb9b921

Browse files
author
S L
committed
#124 : Extra directories created when default <generateGitPropertiesFilename> is used
1 parent bda31e9 commit bb9b921

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
490490

491491
void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
492492
FileWriter fileWriter = null;
493-
File gitPropsFile = new File(base, propertiesFilename);
493+
File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
494494
try {
495495
Files.createParentDirs(gitPropsFile);
496496

@@ -511,6 +511,19 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
511511
}
512512
}
513513

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

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)