Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Bricon2018-08-28 16:37:15 -0400
committerFred Bricon2018-08-28 19:28:11 -0400
commited068cca90bb62554018335b60f8e1118fb96ea6 (patch)
treef2396389990b1920ca2672082fe8597e817a1631
parentd8b346fe01eaac5538de85db08d421e4a6d45dee (diff)
downloadm2e-core-ed068cca90bb62554018335b60f8e1118fb96ea6.tar.gz
m2e-core-ed068cca90bb62554018335b60f8e1118fb96ea6.tar.xz
m2e-core-ed068cca90bb62554018335b60f8e1118fb96ea6.zip
Bug 537578: provide a way to disable the test classpath flag
Projects adding <m2e.disableTestClasspathFlag>true</m2e.disableTestClasspathFlag> to their <properties> section will disable the test classpath segregation in JDT, allowing test resources of this project to be used as test-jar dependencies with the compile scope, in other projects. Change-Id: Id1f567732f6f7a0628fb8e2e90cc1f88fc918af9 Signed-off-by: Fred Bricon <fbricon@gmail.com>
-rw-r--r--org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java22
-rw-r--r--org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java15
-rw-r--r--org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/MavenClasspathHelpers.java7
3 files changed, 28 insertions, 16 deletions
diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java
index abd8533d..7a739536 100644
--- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java
+++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java
@@ -293,7 +293,6 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
String testResourcesEncoding = null;
List<MojoExecution> executions = getCompilerMojoExecutions(request, mon.newChild(1));
-
for(MojoExecution compile : executions) {
if(isCompileExecution(compile)) {
mainSourceEncoding = maven.getMojoParameterValue(mavenProject, compile, "encoding", String.class, monitor); //$NON-NLS-1$
@@ -339,16 +338,17 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
RESOURCES_PLUGIN_ARTIFACT_ID, mon.newChild(1), GOAL_TESTRESOURCES)) {
testResourcesEncoding = maven.getMojoParameterValue(mavenProject, resources, "encoding", String.class, monitor); //$NON-NLS-1$
}
-
addSourceDirs(classpath, project, mavenProject.getCompileSourceRoots(), classes.getFullPath(), inclusion,
exclusion, mainSourceEncoding, mon.newChild(1), false);
addResourceDirs(classpath, project, mavenProject, mavenProject.getBuild().getResources(), classes.getFullPath(),
mainResourcesEncoding, mon.newChild(1), false);
+ //If the project properties contain m2e.disableTestClasspathFlag=true, then the test flag must not be set
+ boolean addTestFlag = !MavenClasspathHelpers.hasTestFlagDisabled(mavenProject);
addSourceDirs(classpath, project, mavenProject.getTestCompileSourceRoots(), testClasses.getFullPath(),
- inclusionTest, exclusionTest, testSourceEncoding, mon.newChild(1), true);
+ inclusionTest, exclusionTest, testSourceEncoding, mon.newChild(1), addTestFlag);
addResourceDirs(classpath, project, mavenProject, mavenProject.getBuild().getTestResources(),
- testClasses.getFullPath(), testResourcesEncoding, mon.newChild(1), true);
+ testClasses.getFullPath(), testResourcesEncoding, mon.newChild(1), addTestFlag);
} finally {
mon.done();
}
@@ -377,7 +377,7 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
private void addSourceDirs(IClasspathDescriptor classpath, IProject project, List<String> sourceRoots,
IPath outputPath, IPath[] inclusion, IPath[] exclusion, String sourceEncoding, IProgressMonitor monitor,
- boolean isTest) throws CoreException {
+ boolean addTestFlag) throws CoreException {
for(int i = 0; i < sourceRoots.size(); i++ ) {
IFolder sourceFolder = getFolder(project, sourceRoots.get(i));
@@ -410,7 +410,7 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
// all source entries are marked as generated (a.k.a. optional)
IClasspathEntryDescriptor descriptor = classpath.addSourceEntry(sourceFolder.getFullPath(), outputPath,
inclusion, exclusion, true /*generated*/);
- descriptor.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, isTest ? "true" : null);
+ descriptor.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, addTestFlag ? "true" : null);
} else {
log.info("Not adding source folder " + sourceFolder.getFullPath() + " because it overlaps with "
+ enclosing.getPath());
@@ -438,8 +438,8 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
}
private void addResourceDirs(IClasspathDescriptor classpath, IProject project, MavenProject mavenProject,
- List<Resource> resources, IPath outputPath, String resourceEncoding, IProgressMonitor monitor, boolean isTest)
- throws CoreException {
+ List<Resource> resources, IPath outputPath, String resourceEncoding, IProgressMonitor monitor,
+ boolean addTestFlag) throws CoreException {
for(Resource resource : resources) {
String directory = resource.getDirectory();
@@ -478,7 +478,7 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
// skip adding resource folders that are included by other resource folders
log.info("Skipping resource folder " + path + " since it's contained by another resource folder");
} else {
- addResourceFolder(classpath, path, outputPath, isTest);
+ addResourceFolder(classpath, path, outputPath, addTestFlag);
}
// Set folder encoding (null = platform default)
@@ -493,11 +493,11 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
}
private void addResourceFolder(IClasspathDescriptor classpath, IPath resourceFolder, IPath outputPath,
- boolean isTest) {
+ boolean addTestFlag) {
log.info("Adding resource folder " + resourceFolder);
IClasspathEntryDescriptor descriptor = classpath.addSourceEntry(resourceFolder, outputPath, DEFAULT_INCLUSIONS,
new IPath[] {new Path("**")}, false /*optional*/);
- descriptor.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, isTest ? "true" : null);
+ descriptor.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, addTestFlag ? "true" : null);
}
private void configureOverlapWithSource(IClasspathDescriptor classpath, IClasspathEntryDescriptor enclosing,
diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java
index 45d360eb..3b7a3a6e 100644
--- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java
+++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java
@@ -105,6 +105,10 @@ public class DefaultClasspathManagerDelegate implements IClasspathManagerDelegat
MavenProject mavenProject = facade.getMavenProject(monitor);
Set<Artifact> artifacts = mavenProject.getArtifacts();
+ //if the project is used as a test-jar by another one, the test flag must be disabled on
+ //its test sources to make them visible by other main code, AND test dependencies.
+ // so that its test sources can compile
+ boolean isTestFlagDisabled = MavenClasspathHelpers.hasTestFlagDisabled(mavenProject);
Map<IPath, ProjectTestAttributes> projectTestAttributes = new HashMap<>(artifacts.size());
for(Artifact a : artifacts) {
@@ -120,24 +124,25 @@ public class DefaultClasspathManagerDelegate implements IClasspathManagerDelegat
}
IClasspathEntryDescriptor entry = null;
-
+ boolean addTestFlag = !isTestFlagDisabled && isOnlyVisibleByTests(a);
if(dependency != null && dependency.getFullPath(a.getFile()) != null) {
IPath projectPath = dependency.getFullPath();
entry = classpath.addProjectEntry(projectPath);
ProjectTestAttributes testAttributes = projectTestAttributes.get(projectPath);
+ boolean isTestArtifact = isTestArtifact(a);
if(testAttributes == null) {
- testAttributes = new ProjectTestAttributes(isOnlyVisibleByTests(a), !isTestArtifact(a));
+ testAttributes = new ProjectTestAttributes(addTestFlag, !isTestArtifact);
projectTestAttributes.put(projectPath, testAttributes);
} else {
- testAttributes.isTest &= isOnlyVisibleByTests(a);
- testAttributes.excludeTestSources &= !isTestArtifact(a);
+ testAttributes.isTest &= addTestFlag;
+ testAttributes.excludeTestSources &= !isTestArtifact;
}
} else {
File artifactFile = a.getFile();
if(artifactFile != null /*&& artifactFile.canRead()*/) {
entry = classpath.addLibraryEntry(Path.fromOSString(artifactFile.getAbsolutePath()));
- entry.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, isOnlyVisibleByTests(a) ? "true" : null);
+ entry.setClasspathAttribute(IClasspathManager.TEST_ATTRIBUTE, addTestFlag ? "true" : null);
}
}
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 d9254a4c..0aedbad2 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
@@ -19,6 +19,8 @@ import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
+import org.apache.maven.project.MavenProject;
+
import org.eclipse.m2e.jdt.IClasspathManager;
@@ -48,4 +50,9 @@ public class MavenClasspathHelpers {
return Stream.of(entry.getExtraAttributes()).filter(a -> key.equals(a.getName())).findFirst()
.map(IClasspathAttribute::getValue).orElse(null);
}
+
+ public static boolean hasTestFlagDisabled(MavenProject mavenProject) {
+ return mavenProject != null
+ && Boolean.valueOf(mavenProject.getProperties().getProperty("m2e.disableTestClasspathFlag", "false"));
+ }
}

Back to the top