Skip to content

Commit

Permalink
Add ability to add source/resource folder outside of the project basedir
Browse files Browse the repository at this point in the history
using linked folder
  • Loading branch information
RoiSoleil authored and HannesWell committed Jan 23, 2024
1 parent b661399 commit c720015
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.io.Serializable;

class A implements Serializable {

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.io.Serializable;

class ATest implements Serializable {

}
Empty file.
13 changes: 13 additions & 0 deletions org.eclipse.m2e.jdt.tests/projects/add-source-resource/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>add-source-resource</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>submoduleA</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>submoduleA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>java</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../parent/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../parent/src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>resources</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../parent/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>test-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../parent/src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IClasspathEntry> TEST_SOURCES = cp -> cp.isTest()
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.m2e.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +34,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
Expand Down Expand Up @@ -134,6 +136,9 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon

protected static final String DEFAULT_COMPILER_LEVEL = "1.5"; //$NON-NLS-1$

private static final QualifiedName LINKED_MAVEN_RESOURCE = new QualifiedName(MavenJdtPlugin.PLUGIN_ID,
"linkedSource");

@Override
public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {
IProject project = request.mavenProjectFacade().getProject();
Expand Down Expand Up @@ -361,6 +366,9 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map<Strin
isTestResourcesSkipped.add(Boolean.FALSE);
}
}

cleanLinkedSourceDirs(project, monitor);

addSourceDirs(classpath, project, mavenProject.getCompileSourceRoots(), classes.getFullPath(), inclusion,
exclusion, mainSourceEncoding, mon.newChild(1), false);
addResourceDirs(classpath, project, mavenProject, mavenProject.getBuild().getResources(), classes.getFullPath(),
Expand Down Expand Up @@ -468,6 +476,14 @@ protected void addSourceDirs(IClasspathDescriptor classpath, IProject project, L

}

private void cleanLinkedSourceDirs(IProject project, IProgressMonitor monitor) throws CoreException {
for(IResource resource : project.members()) {
if(resource instanceof IFolder && "true".equals(resource.getPersistentProperty(LINKED_MAVEN_RESOURCE))) {
resource.delete(false, monitor);
}
}
}

private IClasspathEntryDescriptor getEnclosingEntryDescriptor(IClasspathDescriptor classpath, IPath fullPath) {
for(IClasspathEntryDescriptor cped : classpath.getEntryDescriptors()) {
if(cped.getPath().isPrefixOf(fullPath)) {
Expand Down Expand Up @@ -496,9 +512,14 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
continue;
}
File resourceDirectory = new File(directory);
IPath relativePath = getProjectRelativePath(project, directory);
IResource r = project.findMember(relativePath);
if(r == project) {
String resourceCanonicalPath = null;
try {
resourceCanonicalPath = resourceDirectory.getCanonicalPath();
} catch(IOException ex) {
resourceCanonicalPath = resourceDirectory.getAbsolutePath();
}
IFolder r = getFolder(project, resourceCanonicalPath);
if(r.getFullPath().equals(project.getFullPath())) {
/*
* Workaround for the Java Model Exception:
* Cannot nest output folder 'xxx/src/main/resources' inside output folder 'xxx'
Expand All @@ -517,10 +538,6 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
log.error("Skipping resource folder " + r.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();
IClasspathEntryDescriptor enclosing = getEnclosingEntryDescriptor(classpath, path);
Expand All @@ -533,9 +550,8 @@ 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);
if(r.exists()) {
r.setDefaultCharset(resourceEncoding, monitor);
}
} else {
log.info("Not adding resources folder " + resourceDirectory.getAbsolutePath());
Expand Down Expand Up @@ -864,11 +880,21 @@ private void removeMavenClasspathContainer(IProject project) throws JavaModelExc
}
}

protected IFolder getFolder(IProject project, String absolutePath) {
if(project.getLocation().makeAbsolute().equals(IPath.fromOSString(absolutePath))) {
return project.getFolder(project.getLocation());
protected IFolder getFolder(IProject project, String absolutePath) throws CoreException {
IPath projectLocation = project.getLocation();
if(projectLocation.makeAbsolute().equals(IPath.fromOSString(absolutePath))) {
return project.getFolder(projectLocation);
}
IPath relativePath = getProjectRelativePath(project, absolutePath);
File file;
if(!project.exists(relativePath) && (file = new File(absolutePath)).exists()) {
String linkName = IPath.fromFile(file).makeRelativeTo(projectLocation).toString().replace("/", "_");
IFolder folder = project.getFolder(linkName);
folder.createLink(file.toURI(), IResource.REPLACE, null);
folder.setPersistentProperty(LINKED_MAVEN_RESOURCE, "true");
return folder;
}
return project.getFolder(getProjectRelativePath(project, absolutePath));
return project.getFolder(relativePath);
}

protected IPath getProjectRelativePath(IProject project, String absolutePath) {
Expand Down

0 comments on commit c720015

Please sign in to comment.