1414package org .eclipse .m2e .jdt .internal ;
1515
1616import java .io .File ;
17+ import java .io .IOException ;
18+ import java .nio .file .Files ;
19+ import java .nio .file .Path ;
1720import java .util .ArrayList ;
1821import java .util .Collections ;
1922import java .util .HashMap ;
3033import org .eclipse .core .resources .IFolder ;
3134import org .eclipse .core .resources .IProject ;
3235import org .eclipse .core .resources .IResource ;
36+ import org .eclipse .core .resources .ResourcesPlugin ;
3337import org .eclipse .core .runtime .CoreException ;
3438import org .eclipse .core .runtime .IPath ;
3539import org .eclipse .core .runtime .IProgressMonitor ;
40+ import org .eclipse .core .runtime .QualifiedName ;
3641import org .eclipse .core .runtime .SubMonitor ;
3742import org .eclipse .jdt .core .IAccessRule ;
3843import org .eclipse .jdt .core .IClasspathAttribute ;
@@ -134,6 +139,9 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
134139
135140 protected static final String DEFAULT_COMPILER_LEVEL = "1.5" ; //$NON-NLS-1$
136141
142+ private static final QualifiedName LINKED_MAVEN_RESOURCE = new QualifiedName (MavenJdtPlugin .PLUGIN_ID ,
143+ "linkedSource" );
144+
137145 @ Override
138146 public void configure (ProjectConfigurationRequest request , IProgressMonitor monitor ) throws CoreException {
139147 IProject project = request .mavenProjectFacade ().getProject ();
@@ -282,8 +290,8 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map<Strin
282290 MavenProject mavenProject = request .mavenProject ();
283291 IMavenProjectFacade projectFacade = request .mavenProjectFacade ();
284292
285- IFolder classes = getFolder (project , mavenProject .getBuild ().getOutputDirectory ());
286- IFolder testClasses = getFolder (project , mavenProject .getBuild ().getTestOutputDirectory ());
293+ IContainer classes = getFolder (project , mavenProject .getBuild ().getOutputDirectory ());
294+ IContainer testClasses = getFolder (project , mavenProject .getBuild ().getTestOutputDirectory ());
287295
288296 M2EUtils .createFolder (classes , true , mon .newChild (1 ));
289297 M2EUtils .createFolder (testClasses , true , mon .newChild (1 ));
@@ -361,6 +369,9 @@ protected void addProjectSourceFolders(IClasspathDescriptor classpath, Map<Strin
361369 isTestResourcesSkipped .add (Boolean .FALSE );
362370 }
363371 }
372+
373+ cleanLinkedSourceDirs (project , monitor );
374+
364375 addSourceDirs (classpath , project , mavenProject .getCompileSourceRoots (), classes .getFullPath (), inclusion ,
365376 exclusion , mainSourceEncoding , mon .newChild (1 ), false );
366377 addResourceDirs (classpath , project , mavenProject , mavenProject .getBuild ().getResources (), classes .getFullPath (),
@@ -429,7 +440,7 @@ protected void addSourceDirs(IClasspathDescriptor classpath, IProject project, L
429440 boolean addTestFlag ) throws CoreException {
430441
431442 for (String sourceRoot : sourceRoots ) {
432- IFolder sourceFolder = getFolder (project , sourceRoot );
443+ IContainer sourceFolder = getFolder (project , sourceRoot );
433444
434445 if (sourceFolder == null ) {
435446 // this cannot actually happen, unless I misunderstand how project.getFolder works
@@ -468,6 +479,14 @@ protected void addSourceDirs(IClasspathDescriptor classpath, IProject project, L
468479
469480 }
470481
482+ private void cleanLinkedSourceDirs (IProject project , IProgressMonitor monitor ) throws CoreException {
483+ for (IResource resource : project .members ()) {
484+ if (resource instanceof IFolder && "true" .equals (resource .getPersistentProperty (LINKED_MAVEN_RESOURCE ))) {
485+ resource .delete (false , monitor );
486+ }
487+ }
488+ }
489+
471490 private IClasspathEntryDescriptor getEnclosingEntryDescriptor (IClasspathDescriptor classpath , IPath fullPath ) {
472491 for (IClasspathEntryDescriptor cped : classpath .getEntryDescriptors ()) {
473492 if (cped .getPath ().isPrefixOf (fullPath )) {
@@ -495,9 +514,13 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
495514 if (directory == null ) {
496515 continue ;
497516 }
498- File resourceDirectory = new File (directory );
499- IPath relativePath = getProjectRelativePath (project , directory );
500- IResource r = project .findMember (relativePath );
517+ File resourceDirectory = null ;
518+ try {
519+ resourceDirectory = new File (directory ).getCanonicalFile ();
520+ } catch (IOException ex ) {
521+ resourceDirectory = new File (directory ).getAbsoluteFile ();
522+ }
523+ IContainer r = getFolder (project , resourceDirectory .getPath ());
501524 if (r == project ) {
502525 /*
503526 * Workaround for the Java Model Exception:
@@ -517,10 +540,6 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
517540 log .error ("Skipping resource folder " + r .getFullPath ());
518541 return ;
519542 }
520- if (r == null ) {
521- //this means the resources does not exits (yet) but might be created later on!
522- r = project .getFolder (relativePath );
523- }
524543 if (project .equals (r .getProject ())) {
525544 IPath path = r .getFullPath ();
526545 IClasspathEntryDescriptor enclosing = getEnclosingEntryDescriptor (classpath , path );
@@ -533,12 +552,11 @@ private void addResourceDirs(IClasspathDescriptor classpath, IProject project, M
533552 addResourceFolder (classpath , path , outputPath , addTestFlag );
534553 }
535554 // Set folder encoding (null = platform default)
536- IFolder resourceFolder = project .getFolder (relativePath );
537- if (resourceFolder .exists ()) {
538- resourceFolder .setDefaultCharset (resourceEncoding , monitor );
555+ if (r .exists ()) {
556+ r .setDefaultCharset (resourceEncoding , monitor );
539557 }
540558 } else {
541- log .info ("Not adding resources folder " + resourceDirectory . getAbsolutePath () );
559+ log .info ("Not adding resources folder " + resourceDirectory );
542560 }
543561 }
544562 }
@@ -864,11 +882,22 @@ private void removeMavenClasspathContainer(IProject project) throws JavaModelExc
864882 }
865883 }
866884
867- protected IFolder getFolder (IProject project , String absolutePath ) {
868- if (project .getLocation ().makeAbsolute ().equals (IPath .fromOSString (absolutePath ))) {
869- return project .getFolder (project .getLocation ());
885+ protected IContainer getFolder (IProject project , String absolutePath ) throws CoreException {
886+ Path projectLocation = project .getLocation ().toPath ().toAbsolutePath ();
887+ Path folderPath = Path .of (absolutePath );
888+ if (projectLocation .equals (folderPath )) {
889+ return project ;
890+ }
891+ IPath relativePath = getProjectRelativePath (project , absolutePath );
892+ if (!project .exists (relativePath ) && Files .exists (folderPath )
893+ && !ResourcesPlugin .getWorkspace ().getRoot ().getLocation ().toPath ().equals (folderPath )) {
894+ String linkName = projectLocation .relativize (folderPath ).toString ().replace ("/" , "_" );
895+ IFolder folder = project .getFolder (linkName );
896+ folder .createLink (folderPath .toUri (), IResource .REPLACE , null );
897+ folder .setPersistentProperty (LINKED_MAVEN_RESOURCE , "true" );
898+ return folder ;
870899 }
871- return project .getFolder (getProjectRelativePath ( project , absolutePath ) );
900+ return project .getFolder (relativePath );
872901 }
873902
874903 protected IPath getProjectRelativePath (IProject project , String absolutePath ) {
0 commit comments