Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Bricon2018-01-30 19:17:02 -0500
committerFred Bricon2018-01-30 19:17:02 -0500
commitd8123a2a12a45cadd44573217567756985845491 (patch)
tree10ed72ab021f8302764bb411f0c6d041992089f3 /org.eclipse.m2e.jdt
parentfa2beee5cd0f292a306254413f68d392d46884e3 (diff)
downloadm2e-core-d8123a2a12a45cadd44573217567756985845491.tar.gz
m2e-core-d8123a2a12a45cadd44573217567756985845491.tar.xz
m2e-core-d8123a2a12a45cadd44573217567756985845491.zip
Bug 515398: fix classpath when launching class in non-default test source directory
Change-Id: I5bdfae0a85b4fc6870e48daaf8ec51de5e3912ea Signed-off-by: Fred Bricon <fbricon@gmail.com>
Diffstat (limited to 'org.eclipse.m2e.jdt')
-rw-r--r--org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java13
-rw-r--r--org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java22
2 files changed, 31 insertions, 4 deletions
diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java
index 758fbb97..a45902c2 100644
--- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java
+++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010 Sonatype, Inc.
+ * Copyright (c) 2010-2018 Sonatype, Inc. and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,6 +11,8 @@
package org.eclipse.m2e.jdt.internal;
+import java.util.stream.Stream;
+
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
@@ -33,4 +35,13 @@ public class MavenClasspathHelpers {
public static IClasspathEntry getDefaultContainerEntry(boolean isExported) {
return JavaCore.newContainerEntry(new Path(IClasspathManager.CONTAINER_ID), isExported);
}
+
+ public static boolean isTestSource(IClasspathEntry entry) {
+ if(entry == null || entry.getEntryKind() != IClasspathEntry.CPE_SOURCE || entry.getExtraAttributes().length == 0) {
+ return false;
+ }
+ return Stream.of(entry.getExtraAttributes())
+ .filter(a -> IClasspathManager.TEST_ATTRIBUTE.equals(a.getName()) && "true".equals(a.getValue())).findAny()
+ .isPresent();
+ }
}
diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java
index c15b88d4..bf9df2a0 100644
--- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java
+++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008-2010 Sonatype, Inc.
+ * Copyright (c) 2008-2018 Sonatype, Inc. and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -17,6 +17,8 @@ import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
@@ -38,6 +40,7 @@ import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
@@ -66,8 +69,6 @@ public class MavenRuntimeClasspathProvider extends StandardClasspathProvider {
public static final String MAVEN_CLASSPATH_PROVIDER = "org.eclipse.m2e.launchconfig.classpathProvider"; //$NON-NLS-1$
- private static final String TESTS_PROJECT_CLASSIFIER = "tests"; //$NON-NLS-1$
-
private static final String THIS_PROJECT_CLASSIFIER = ""; //$NON-NLS-1$
public static final String JDT_JUNIT_TEST = "org.eclipse.jdt.junit.launchconfig"; //$NON-NLS-1$
@@ -205,12 +206,19 @@ public class MavenRuntimeClasspathProvider extends StandardClasspathProvider {
// ECLIPSE-33: applications from test sources should use test scope
final Set<IPath> testSources = new HashSet<IPath>();
IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);
+
IMavenProjectFacade facade = projectManager.create(javaProject.getProject(), new NullProgressMonitor());
if(facade == null) {
return IClasspathManager.CLASSPATH_RUNTIME;
}
testSources.addAll(Arrays.asList(facade.getTestCompileSourceLocations()));
+ //If a test folder was added by a plugin (hello build-helper-maven-plugin) to the project model,
+ //facade.getTestCompileSourceLocations() would miss it.
+ //So as a complement, we add all Eclipse folders having the "test" attribute for that project
+ //XXX The following most likely makes calling facade.getTestCompileSourceLocations() redundant
+ //(we'd prolly have some issues if compile source locations didn't get that "test" flag).
+ testSources.addAll(getEclipseTestSources(javaProject));
for(int i = 0; i < resources.length; i++ ) {
for(IPath testPath : testSources) {
@@ -219,6 +227,7 @@ public class MavenRuntimeClasspathProvider extends StandardClasspathProvider {
}
}
}
+
return IClasspathManager.CLASSPATH_RUNTIME;
} else if(JDT_JUNIT_TEST.equals(typeid) || JDT_TESTNG_TEST.equals(typeid)) {
return IClasspathManager.CLASSPATH_TEST;
@@ -228,6 +237,13 @@ public class MavenRuntimeClasspathProvider extends StandardClasspathProvider {
}
}
+ private Set<IPath> getEclipseTestSources(IJavaProject javaProject) throws JavaModelException {
+ IClasspathEntry[] cpes = javaProject.getRawClasspath();
+ Set<IPath> eclipseTestSources = Stream.of(cpes).filter(MavenClasspathHelpers::isTestSource)
+ .map(cpe -> cpe.getPath().makeRelativeTo(javaProject.getPath())).collect(Collectors.toSet());
+ return eclipseTestSources;
+ }
+
protected void addProjectEntries(Set<IRuntimeClasspathEntry> resolved, IPath path, int scope, String classifier,
ILaunchConfiguration launchConfiguration, final IProgressMonitor monitor, int classpathProperty)
throws CoreException {

Back to the top