From 20ddad859771346c220422d5a06a355d7250bdd7 Mon Sep 17 00:00:00 2001 From: RoiSoleil Date: Mon, 27 Nov 2023 17:10:17 +0100 Subject: [PATCH] Add ability to add source/resource folder outside of the project basedir using linked folder --- .../parent/src/main/java/A.java | 5 ++ .../parent/src/main/resources/res.txt | 0 .../parent/src/test/java/ATest.java | 5 ++ .../parent/src/test/resources/test.txt | 0 .../projects/add-source-resource/pom.xml | 13 +++ .../add-source-resource/submoduleA/pom.xml | 80 +++++++++++++++++++ .../m2e/jdt/tests/JavaConfigurationTest.java | 15 ++++ org.eclipse.m2e.jdt/META-INF/MANIFEST.MF | 2 +- .../AbstractJavaProjectConfigurator.java | 49 +++++++++--- 9 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/java/A.java create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/resources/res.txt create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/java/ATest.java create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/resources/test.txt create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/pom.xml create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/java/A.java b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/java/A.java new file mode 100644 index 0000000000..83b86122f3 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/java/A.java @@ -0,0 +1,5 @@ +import java.io.Serializable; + +class A implements Serializable { + +} diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/resources/res.txt b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/main/resources/res.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/java/ATest.java b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/java/ATest.java new file mode 100644 index 0000000000..396f5b59a4 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/java/ATest.java @@ -0,0 +1,5 @@ +import java.io.Serializable; + +class ATest implements Serializable { + +} diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/resources/test.txt b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/src/test/resources/test.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/pom.xml b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/pom.xml new file mode 100644 index 0000000000..b77a70ef1c --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + foo.bar + add-source-resource + 0.0.1-SNAPSHOT + pom + + submoduleA + + diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml new file mode 100644 index 0000000000..1424093bf6 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + foo.bar + submoduleA + 0.0.1-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + java + generate-sources + + add-source + + + + ../parent/src/main/java + + + + + test + generate-sources + + add-test-source + + + + ../parent/src/test/java + + + + + resources + generate-resources + + add-resource + + + + + ../parent/src/main/resources + + + + + + test-resources + generate-resources + + add-test-resource + + + + + ../parent/src/test/resources + + + + + + + + + diff --git a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/JavaConfigurationTest.java b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/JavaConfigurationTest.java index e56ccb6798..80a7e4f5a1 100644 --- a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/JavaConfigurationTest.java +++ b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/JavaConfigurationTest.java @@ -35,6 +35,7 @@ import org.eclipse.jdt.core.JavaModelException; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl; +import org.eclipse.m2e.core.project.ResolverConfiguration; import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; import org.junit.Before; import org.junit.Test; @@ -132,6 +133,20 @@ public void testComplianceVsEnablePreviewSettings() throws CoreException, IOExce assertEquals(JavaCore.ENABLED, project.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, false)); assertEquals(JavaCore.IGNORE, project.getOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, false)); } + + @Test + public void testAddSourceResource() throws CoreException, IOException, InterruptedException { + File baseDir = new File(FileLocator + .toFileURL(JavaConfigurationTest.class.getResource("/projects/add-source-resource/submoduleA/pom.xml")) + .getFile()).getParentFile().getParentFile(); + waitForJobsToComplete(); + IProject project = importProjects(baseDir.getAbsolutePath(), new String[] { "submoduleA/pom.xml" }, + new ResolverConfiguration())[0]; + waitForJobsToComplete(); + IJavaProject javaProject = JavaCore.create(project); + assertEquals(8, Arrays.stream(javaProject.getRawClasspath()) + .filter(cp -> IClasspathEntry.CPE_SOURCE == cp.getEntryKind()).count()); + } // --- utility methods --- private static final Predicate TEST_SOURCES = cp -> cp.isTest() diff --git a/org.eclipse.m2e.jdt/META-INF/MANIFEST.MF b/org.eclipse.m2e.jdt/META-INF/MANIFEST.MF index d44e80dc90..f874d1c804 100644 --- a/org.eclipse.m2e.jdt/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.jdt/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true -Bundle-Version: 2.3.3.qualifier +Bundle-Version: 2.3.4.qualifier Bundle-Localization: plugin Export-Package: org.eclipse.m2e.jdt, org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui", 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 593febb513..c8d40e2f40 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 @@ -14,6 +14,7 @@ package org.eclipse.m2e.jdt.internal; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -33,6 +34,8 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.QualifiedName; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jdt.core.IAccessRule; import org.eclipse.jdt.core.IClasspathAttribute; @@ -134,6 +137,9 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon protected static final String DEFAULT_COMPILER_LEVEL = "1.5"; //$NON-NLS-1$ + private static final QualifiedName LINKED_RESOURCE_PROPERTY = new QualifiedName(MavenJdtPlugin.PLUGIN_ID, + "linkedSource"); + @Override public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException { IProject project = request.mavenProjectFacade().getProject(); @@ -361,6 +367,9 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map