Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Maven source and test source directory resolving #3562

Merged
merged 4 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions src/test/java/spoon/support/compiler/SpoonPomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
import java.nio.file.Paths;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -51,4 +52,20 @@ public void checkProfilesModules(String path, String[] expected, Pattern profile
assertEquals(expected[i], modules.get(i).getName());
}
}

public void getSourceDirectory() throws IOException, XmlPullParserException {
checkSourceDirectory(
"src/test/resources/maven-launcher/hierarchy",
Paths.get("src/test/resources/maven-launcher/hierarchy/child/src").toAbsolutePath().toString()
);
}

public void checkSourceDirectory(String path, String expected) throws IOException, XmlPullParserException {
SpoonPom pomModel = new SpoonPom(path, null, MavenLauncher.SOURCE_TYPE.APP_SOURCE, new StandardEnvironment());

SpoonPom childModel = pomModel.getModules().get(0);
//contract: source directory is derived from parent pom.xml if not declared in the current
// (childModel) SpoonPom
assertEquals(expected, childModel.getSourceDirectories().get(0).getAbsolutePath());
}
}
16 changes: 16 additions & 0 deletions src/test/resources/maven-launcher/hierarchy/child/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>sample.text</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<groupId>sample.text</groupId>
<artifactId>child</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package maven-launcher.hierarchy.child.src;

public class Test {

}
24 changes: 24 additions & 0 deletions src/test/resources/maven-launcher/hierarchy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>sample.text</groupId>
<artifactId>project</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>sample.text</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
<module>child</module>
</modules>

<build>
<sourceDirectory>${project.basedir}/src</sourceDirectory>
</build>
</project>