Skip to content

Commit

Permalink
Improved source and test source directory resolving by climbing the p…
Browse files Browse the repository at this point in the history
…om.xml hierarchy to find the nearest ancestor that declares the source directory. Also added interpolating (project|pom|'').basedir
  • Loading branch information
Strum355 committed Sep 9, 2020
1 parent c99a186 commit aa0f9b7
Showing 1 changed file with 77 additions and 8 deletions.
85 changes: 77 additions & 8 deletions src/main/java/spoon/support/compiler/SpoonPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -165,9 +166,20 @@ public List<File> getSourceDirectories() {
sourcePath = build.getSourceDirectory();
}
if (sourcePath == null) {
sourcePath = Paths.get("src/main/java").toString();
sourcePath = getSourceDirectoryFromParent(getParentPom());
if (sourcePath == null) {
sourcePath = Paths.get("src/main/java").toString();
}
}
sourcePath = extractVariable(sourcePath);
Path path = Paths.get(sourcePath);

String absoluteSourcePath;
if (path.isAbsolute()) {
absoluteSourcePath = path.toString();
} else {
absoluteSourcePath = Paths.get(directory.getAbsolutePath(), sourcePath).toString();
}
String absoluteSourcePath = Paths.get(directory.getAbsolutePath(), sourcePath).toString();
File source = new File(absoluteSourcePath);
if (source.exists()) {
output.add(source);
Expand All @@ -182,6 +194,28 @@ public List<File> getSourceDirectories() {
return output;
}

/**
* Climbs the pom.xml hierarchy until a model is found in which
* a source directory is declared.
* @return the uninterpolated source directory declared in the nearest ancestor
*/
private String getSourceDirectoryFromParent(SpoonPom parent) {
if (parent == null) {
return null;
}
String sourcePath = null;
Build build = parent.model.getBuild();
if (build != null) {
sourcePath = build.getSourceDirectory();
if (sourcePath == null && parent.getParentPom() != null) {
return getSourceDirectoryFromParent(parent.getParentPom());
}
} else if (parent.getParentPom() != null) {
return getSourceDirectoryFromParent(parent.getParentPom());
}
return sourcePath;
}

/**
* Get the list of test directories of the project
* @return the list of test directories
Expand All @@ -195,9 +229,20 @@ public List<File> getTestDirectories() {
sourcePath = build.getTestSourceDirectory();
}
if (sourcePath == null) {
sourcePath = Paths.get("src/test/java").toString();
sourcePath = getTestSourceDirectoryFromParent(getParentPom());
if (sourcePath == null) {
sourcePath = Paths.get("src/test/java").toString();
}
}
sourcePath = extractVariable(sourcePath);
Path path = Paths.get(sourcePath);

String absoluteSourcePath;
if (path.isAbsolute()) {
absoluteSourcePath = path.toString();
} else {
absoluteSourcePath = Paths.get(directory.getAbsolutePath(), sourcePath).toString();
}
String absoluteSourcePath = Paths.get(directory.getAbsolutePath(), sourcePath).toString();
File source = new File(absoluteSourcePath);
if (source.exists()) {
output.add(source);
Expand All @@ -212,6 +257,28 @@ public List<File> getTestDirectories() {
return output;
}

/**
* Climbs the pom.xml hierarchy until a model is found in which
* a test source directory is declared.
* @return the uninterpolated test source directory declared in the nearest ancestor
*/
private String getTestSourceDirectoryFromParent(SpoonPom parent) {
if (parent == null) {
return null;
}
String sourcePath = null;
Build build = parent.model.getBuild();
if (build != null) {
sourcePath = build.getTestSourceDirectory();
if (sourcePath == null && parent.getParentPom() != null) {
return getTestSourceDirectoryFromParent(parent.getParentPom());
}
} else if (parent.getParentPom() != null) {
return getTestSourceDirectoryFromParent(parent.getParentPom());
}
return sourcePath;
}

/**
* Get the list of classpath files generated by maven
* @return the list of classpath files
Expand Down Expand Up @@ -247,29 +314,31 @@ private String extractVariable(String value) {
}

/**
* Get the value of a property
* Get the value of a property. Reference: https://maven.apache.org/ref/3.6.3/maven-model-builder/#Model_Interpolation
* @param key the key of the property
* @return the property value if key exists or null
*/
private String getProperty(String key) {
if ("project.version".equals(key) || "pom.version".equals(key)) {
if ("project.version".equals(key) || "pom.version".equals(key) || "version".equals(key)) {
if (model.getVersion() != null) {
return model.getVersion();
} else if (model.getParent() != null) {
return model.getParent().getVersion();
}
} else if ("project.groupId".equals(key) || "pom.groupId".equals(key)) {
} else if ("project.groupId".equals(key) || "pom.groupId".equals(key) || "groupId".equals(key)) {
if (model.getGroupId() != null) {
return model.getGroupId();
} else if (model.getParent() != null) {
return model.getParent().getGroupId();
}
} else if ("project.artifactId".equals(key) || "pom.artifactId".equals(key)) {
} else if ("project.artifactId".equals(key) || "pom.artifactId".equals(key) || "artifactId".equals(key)) {
if (model.getArtifactId() != null) {
return model.getArtifactId();
} else if (model.getParent() != null) {
return model.getParent().getArtifactId();
}
} else if ("project.basedir".equals(key) || "pom.basedir".equals(key) || "basedir".equals(key)) {
return pomFile.getParent();
}
String value = extractVariable(model.getProperties().getProperty(key));
if (value == null) {
Expand Down

0 comments on commit aa0f9b7

Please sign in to comment.