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 committed Nov 16, 2023
1 parent 7b152e5 commit 3547584
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.io.Serializable;

class ATest implements Serializable {

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

class ATest implements Serializable {

}
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>add-source-resource</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/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../parent/test</source>
</sources>
</configuration>
</execution>
<execution>
<id>resources</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../parent/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/test-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<IClasspathEntry> TEST_SOURCES = cp -> cp.isTest()
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 @@ -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;
Expand Down Expand Up @@ -362,6 +364,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 @@ -469,6 +474,15 @@ 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(new QualifiedName(MavenJdtPlugin.PLUGIN_ID, "linkedSource")))) {
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,10 +510,14 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
if(directory == null) {
continue;
}
File resourceDirectory = new File(directory);
IPath relativePath = getProjectRelativePath(project, directory);
IResource r = project.findMember(relativePath);
if(r == project) {
File resourceDirectory = new File(resource.getDirectory());
String resourceCanonicalPath = resourceDirectory.getAbsolutePath();
try {
resourceCanonicalPath = resourceDirectory.getCanonicalPath();
} catch(IOException ex) {
}
IFolder resourceFolder = getFolder(project, resourceCanonicalPath);
if(resourceFolder == project) {
/*
* Workaround for the Java Model Exception:
* Cannot nest output folder 'xxx/src/main/resources' inside output folder 'xxx'
Expand All @@ -515,15 +533,11 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
* </includes>
* </resource>
*/
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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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('\\', '/'));
}
Expand Down

0 comments on commit 3547584

Please sign in to comment.