Skip to content

Commit a5a29f8

Browse files
authored
fix: parent pom path validation (#17)
1 parent 56baee2 commit a5a29f8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

git-versioner-maven-extension/src/main/java/com/github/manikmagar/maven/versioner/extension/GitVersionerModelProcessor.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class GitVersionerModelProcessor extends DefaultModelProcessor {
4545

4646
private final List<Path> relatedPoms = new ArrayList<>();
4747
private VersionStrategy versionStrategy;
48+
private Path dotmvnDirectory;
4849

4950
@Override
5051
public Model read(File input, Map<String, ?> options) throws IOException {
@@ -66,7 +67,11 @@ private Model processModel(Model projectModel, Map<String, ?> options) {
6667

6768
if (pomSource != null) {
6869
projectModel.setPomFile(new File(pomSource.getLocation()));
69-
} else {
70+
}
71+
72+
// Only source poms are with .xml
73+
// dependency poms are with .pom
74+
if (pomSource == null || !pomSource.getLocation().endsWith(".xml")) {
7075
return projectModel;
7176
}
7277

@@ -75,6 +80,7 @@ private Model processModel(Model projectModel, Map<String, ?> options) {
7580
// The first execution is with the project's pom though.
7681
// Use first initialized flag to avoid processing other classpath poms.
7782
if (!initialized) {
83+
dotmvnDirectory = getDOTMVNDirectory(projectModel.getPomFile().toPath());
7884
GAV extensionGAV = Util.extensionArtifact();
7985
LOGGER.info(MessageUtils.buffer().a("--- ").mojo(extensionGAV).a(" ").strong("[core-extension]").a(" ---")
8086
.toString());
@@ -98,7 +104,8 @@ private void processRelatedProjects(Model projectModel) {
98104
var path = Paths.get(parent.getRelativePath());
99105
// Parent is part of this build
100106
try {
101-
Path parentPomPath = projectModel.getPomFile().getParentFile().toPath().resolve(path).toRealPath();
107+
Path parentPomPath = Paths.get(
108+
projectModel.getPomFile().getParentFile().toPath().resolve(path).toFile().getCanonicalPath());
102109
LOGGER.debug("Looking for parent pom {}", parentPomPath);
103110
if (Files.exists(parentPomPath) && this.relatedPoms.contains(parentPomPath)) {
104111
LOGGER.info("Setting parent {} version to {}", parent, versionStrategy.toVersionString());
@@ -185,7 +192,7 @@ private VersionConfig loadConfig() {
185192
}
186193
private Properties loadExtensionProperties() {
187194
Properties props = new Properties();
188-
Path propertiesPath = Paths.get(DOT_MVN, GIT_VERSIONER_EXTENSIONS_PROPERTIES);
195+
Path propertiesPath = dotmvnDirectory.resolve(GIT_VERSIONER_EXTENSIONS_PROPERTIES);
189196
if (propertiesPath.toFile().exists()) {
190197
LOGGER.debug("Reading versioner properties from {}", propertiesPath);
191198
try (Reader reader = Files.newBufferedReader(propertiesPath)) {
@@ -230,4 +237,22 @@ private void addVersionerProperties(Model projectModel) {
230237
LOGGER.info("Adding generated properties to project model: {}", builder);
231238
projectModel.getProperties().putAll(properties);
232239
}
240+
241+
/**
242+
* Find the first .mvn directory in currentDir and parents.
243+
*
244+
* @param currentDir
245+
* Path
246+
* @return Path of .mvn directory
247+
*/
248+
private static Path getDOTMVNDirectory(final Path currentDir) {
249+
LOGGER.info("Finding .mvn in {}", currentDir);
250+
Path refDir = currentDir;
251+
Path dotMvn = refDir.resolve(DOT_MVN);
252+
while (!Files.exists(dotMvn)) {
253+
refDir = refDir.getParent();
254+
dotMvn = refDir.resolve(DOT_MVN);
255+
}
256+
return dotMvn;
257+
}
233258
}

0 commit comments

Comments
 (0)