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 gradle project classpath calculation #2236

Merged
merged 1 commit into from
Sep 23, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class JavaLaunchConfigurationInfo extends LaunchConfigurationInfo {

private static final String JAVA_APPLICATION_LAUNCH = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<launchConfiguration type=\"%s\">\n"
+ "<launchConfiguration type=\"org.eclipse.jdt.launching.localJavaApplication\">\n"
+ "<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS\">\n"
+ "</listAttribute>\n"
+ "<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES\">\n"
Expand All @@ -43,21 +43,10 @@ public class JavaLaunchConfigurationInfo extends LaunchConfigurationInfo {
public JavaLaunchConfigurationInfo(String scope) {
super();

// Since MavenRuntimeClasspathProvider will only encluding test entries when:
// 1. Launch configuration is JUnit/TestNG type
// 2. Mapped resource is in test path.
// That's why we use JUnit launch configuration here to make sure the result is right when excludeTestCode is false.
// See: {@link org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider#getArtifactScope(ILaunchConfiguration)}
String launchXml = null;
if ("test".equals(scope)) {
launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.junit.launchconfig");
} else {
launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.launching.localJavaApplication");
}
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser.setErrorHandler(new DefaultHandler());
StringReader reader = new StringReader(launchXml);
StringReader reader = new StringReader(JAVA_APPLICATION_LAUNCH);
InputSource source = new InputSource(reader);
Element root = parser.parse(source).getDocumentElement();
initializeFromXML(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,14 @@ public void testGetClasspathsForGradle() throws Exception {
IProject project = WorkspaceHelper.getProject("simple-gradle");
String uriString = project.getFile("src/main/java/Library.java").getLocationURI().toString();
ClasspathOptions options = new ClasspathOptions();
// Gradle project will always return classpath containing test dependencies.
// So we only test `scope = "test"` scenario.
options.scope = "test";
options.scope = "runtime";
ClasspathResult result = ProjectCommand.getClasspaths(uriString, options);
assertEquals(3, result.classpaths.length);
assertEquals(0, result.modulepaths.length);
assertTrue(result.classpaths[0].indexOf("junit") == -1);

options.scope = "test";
result = ProjectCommand.getClasspaths(uriString, options);
assertEquals(5, result.classpaths.length);
assertEquals(0, result.modulepaths.length);
boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> {
Expand Down