From 3547584f02a07f8a196b0f1001c78444782970cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9lios=20GILLES?= Date: Mon, 13 Nov 2023 15:50:07 +0100 Subject: [PATCH] Add ability to add source/resource folder outside of the project basedir using linked folder --- .../add-source-resource/parent/java/A.java | 5 ++ .../parent/resources/res.txt | 0 .../parent/test-resources/test.txt | 0 .../parent/test/ATest.java | 5 ++ .../add-source-resource/submoduleA/pom.xml | 80 +++++++++++++++++++ .../m2e/jdt/tests/JavaConfigurationTest.java | 31 +++---- .../AbstractJavaProjectConfigurator.java | 50 ++++++++---- 7 files changed, 137 insertions(+), 34 deletions(-) create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/java/A.java create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/resources/res.txt create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test-resources/test.txt create mode 100644 org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test/ATest.java 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/java/A.java b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/java/A.java new file mode 100644 index 0000000000..396f5b59a4 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/java/A.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/resources/res.txt b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/resources/res.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test-resources/test.txt b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test-resources/test.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test/ATest.java b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test/ATest.java new file mode 100644 index 0000000000..396f5b59a4 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/parent/test/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/submoduleA/pom.xml b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml new file mode 100644 index 0000000000..061c543cd3 --- /dev/null +++ b/org.eclipse.m2e.jdt.tests/projects/add-source-resource/submoduleA/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + foo.bar + add-source-resource + 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/java + + + + + test + generate-sources + + add-test-source + + + + ../parent/test + + + + + resources + generate-resources + + add-resource + + + + + ../parent/resources + + + + + + test-resources + generate-resources + + add-test-resource + + + + + ../parent/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..c5cdd79850 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 @@ -10,34 +10,21 @@ package org.eclipse.m2e.jdt.tests; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.function.Predicate; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IncrementalProjectBuilder; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.FileLocator; -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.core.resources.*; +import org.eclipse.core.runtime.*; +import org.eclipse.jdt.core.*; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl; import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; @SuppressWarnings("restriction") public class JavaConfigurationTest extends AbstractMavenProjectTestCase { @@ -132,6 +119,12 @@ 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 { + IJavaProject project = importResourceProject("/projects/add-source-resource/submoduleA/pom.xml"); + assertEquals(4, project.getRawClasspath().length); + } // --- utility methods --- private static final Predicate TEST_SOURCES = cp -> cp.isTest() 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 5ae81a90c9..1f32613d17 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; @@ -34,6 +35,7 @@ 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; @@ -362,6 +364,9 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map * */ - log.error("Skipping resource folder " + r.getFullPath()); + log.error("Skipping resource folder " + resourceFolder.getFullPath()); return; } - if(r == null) { - //this means the resources does not exits (yet) but might be created later on! - r = project.getFolder(relativePath); - } - if(project.equals(r.getProject())) { - IPath path = r.getFullPath(); + if(project.equals(resourceFolder.getProject())) { + IPath path = resourceFolder.getFullPath(); IClasspathEntryDescriptor enclosing = getEnclosingEntryDescriptor(classpath, path); if(enclosing != null && overlapsWithSourceFolder(path, project, mavenProject)) { configureOverlapWithSource(classpath, enclosing, path); @@ -534,7 +548,6 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M addResourceFolder(classpath, path, outputPath, addTestFlag); } // Set folder encoding (null = platform default) - IFolder resourceFolder = project.getFolder(relativePath); if(resourceFolder.exists()) { resourceFolder.setDefaultCharset(resourceEncoding, monitor); } @@ -865,11 +878,18 @@ private void removeMavenClasspathContainer(IProject project) throws JavaModelExc } } - protected IFolder getFolder(IProject project, String absolutePath) { + protected IFolder getFolder(IProject project, String absolutePath) throws CoreException { if(project.getLocation().makeAbsolute().equals(Path.fromOSString(absolutePath))) { return project.getFolder(project.getLocation()); } - return project.getFolder(getProjectRelativePath(project, absolutePath)); + IPath relativePath = getProjectRelativePath(project, absolutePath); + if(relativePath.segment(0).equals("..")) { + IFolder folder = project.getFolder(relativePath.lastSegment()); + folder.createLink(new File(absolutePath).toURI(), IResource.REPLACE, null); + folder.setPersistentProperty(new QualifiedName(MavenJdtPlugin.PLUGIN_ID, "linkedSource"), "true"); + return folder; + } + return project.getFolder(relativePath); } protected IPath getProjectRelativePath(IProject project, String absolutePath) { @@ -880,7 +900,7 @@ protected IPath getProjectRelativePath(IProject project, String absolutePath) { } else if(absolutePath.startsWith(basedir.getAbsolutePath())) { relative = absolutePath.substring(basedir.getAbsolutePath().length() + 1); } else { - relative = absolutePath; + relative = Path.fromOSString(absolutePath).makeRelativeTo(project.getLocation()).toString(); } return new Path(relative.replace('\\', '/')); }