From ea3a153a9579791fa4842c8da385a020ae3a9b30 Mon Sep 17 00:00:00 2001 From: Fabrizio Giustina Date: Mon, 31 Oct 2005 16:37:19 +0000 Subject: [PATCH] fix for MNG-1324 "System" dependencies path non correctly added to eclipse buildpath path is fixed by overwriting artifact file with the path returned by dependency.getSystemPath() git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@329851 13f79535-47bb-0310-9956-ffa450edef68 --- .../eclipse/EclipseClasspathWriter.java | 71 +++++++++---------- .../maven/plugin/eclipse/EclipseUtils.java | 65 +++++++++++------ .../eclipse/EclipseWtpmodulesWriter.java | 52 +++++++++----- .../plugin/eclipse/EclipsePluginTest.java | 11 ++- .../src/test/projects/project-8/classpath | 2 +- .../src/test/projects/project-8/pom.xml | 2 +- .../src/test/projects/project-8/wtpmodules | 9 ++- 7 files changed, 126 insertions(+), 86 deletions(-) diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java index d8ca44b93e..c9e3171e30 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java @@ -16,6 +16,12 @@ package org.apache.maven.plugin.eclipse; * limitations under the License. */ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -26,15 +32,10 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; - /** * Writes eclipse .classpath file. * @@ -58,9 +59,9 @@ public class EclipseClasspathWriter * @todo the list of needed parameters is really long, maybe this should become a Plexus component */ protected void write( File projectBaseDir, File basedir, MavenProject project, List referencedReactorArtifacts, - EclipseSourceDir[] sourceDirs, List classpathContainers, ArtifactRepository localRepository, - ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, - List remoteArtifactRepositories, boolean downloadSources, String outputDirectory ) + EclipseSourceDir[] sourceDirs, List classpathContainers, ArtifactRepository localRepository, + ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, + List remoteArtifactRepositories, boolean downloadSources, String outputDirectory ) throws MojoExecutionException { @@ -72,8 +73,7 @@ public class EclipseClasspathWriter } catch ( IOException ex ) { - throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), - ex ); //$NON-NLS-1$ + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$ } XMLWriter writer = new PrettyPrintXMLWriter( w ); @@ -107,7 +107,7 @@ public class EclipseClasspathWriter writer.startElement( "classpathentry" ); //$NON-NLS-1$ writer.addAttribute( "kind", "output" ); //$NON-NLS-1$ //$NON-NLS-2$ - writer.addAttribute( "path", EclipseUtils.toRelativeAndFixSeparator( projectBaseDir, //$NON-NLS-1$ + writer.addAttribute( "path", EclipseUtils.toRelativeAndFixSeparator( projectBaseDir, //$NON-NLS-1$ outputDirectory, false ) ); writer.endElement(); @@ -126,6 +126,8 @@ public class EclipseClasspathWriter List artifacts = project.getTestArtifacts(); + EclipseUtils.fixSystemScopeArtifacts( artifacts, project.getDependencies() ); + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) { Artifact artifact = (Artifact) it.next(); @@ -154,9 +156,8 @@ public class EclipseClasspathWriter } private void addDependency( XMLWriter writer, Artifact artifact, List referencedReactorArtifacts, - ArtifactRepository localRepository, ArtifactResolver artifactResolver, - ArtifactFactory artifactFactory, List remoteArtifactRepositories, - boolean downloadSources ) + ArtifactRepository localRepository, ArtifactResolver artifactResolver, + ArtifactFactory artifactFactory, List remoteArtifactRepositories, boolean downloadSources ) throws MojoExecutionException { @@ -183,18 +184,18 @@ public class EclipseClasspathWriter { try { - path = artifactPath.getCanonicalPath(); + path = StringUtils.replace( artifactPath.getCanonicalPath(), "\\", "/" ); } catch ( IOException e ) { String message = Messages.getString( "EclipsePlugin.cantcanonicalize", artifactPath ); - + throw new MojoExecutionException( message, e ); } - + log.info( Messages.getString( "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$ new Object[] { artifact.getArtifactId(), path } ) ); - + log.info( Messages.getString( "EclipseClasspathWriter.sourcesnotavailable", //$NON-NLS-1$ artifact.getArtifactId() ) ); @@ -211,8 +212,9 @@ public class EclipseClasspathWriter if ( downloadSources ) { - Artifact sourceArtifact = retrieveSourceArtifact( artifact, remoteArtifactRepositories, localRepository, - artifactResolver, artifactFactory ); + Artifact sourceArtifact = retrieveSourceArtifact( artifact, remoteArtifactRepositories, + localRepository, artifactResolver, + artifactFactory ); if ( !sourceArtifact.isResolved() ) { @@ -222,12 +224,13 @@ public class EclipseClasspathWriter else { log.debug( Messages.getString( "EclipseClasspathWriter.sourcesavailable", //$NON-NLS-1$ - new Object[]{sourceArtifact.getArtifactId(), - sourceArtifact.getFile().getAbsolutePath()} ) ); + new Object[] { + sourceArtifact.getArtifactId(), + sourceArtifact.getFile().getAbsolutePath() } ) ); sourcepath = "M2_REPO/" //$NON-NLS-1$ - + EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, - sourceArtifact.getFile().getAbsolutePath(), false ); + + EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, sourceArtifact.getFile() + .getAbsolutePath(), false ); } } @@ -249,17 +252,14 @@ public class EclipseClasspathWriter } - private Artifact retrieveSourceArtifact( Artifact artifact, List remoteArtifactRepositories, - ArtifactRepository localRepository, ArtifactResolver artifactResolver, - ArtifactFactory artifactFactory ) + ArtifactRepository localRepository, ArtifactResolver artifactResolver, + ArtifactFactory artifactFactory ) throws MojoExecutionException { // source artifact: use the "sources" classifier added by the source plugin - Artifact sourceArtifact = artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), - artifact.getArtifactId(), - artifact.getVersion(), "java-source", - "sources" ); //$NON-NLS-1$ //$NON-NLS-2$ + Artifact sourceArtifact = artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact + .getArtifactId(), artifact.getVersion(), "java-source", "sources" ); //$NON-NLS-1$ //$NON-NLS-2$ try { @@ -275,19 +275,18 @@ public class EclipseClasspathWriter { String message = Messages.getString( "EclipseClasspathWriter.cantresolvesources", //$NON-NLS-1$ new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } ); - - log.debug( message , e ); + + log.debug( message, e ); } } catch ( ArtifactResolutionException e ) { String message = Messages.getString( "EclipseClasspathWriter.errorresolvingsources", //$NON-NLS-1$ new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } ); - + throw new MojoExecutionException( message, e ); } return sourceArtifact; } } - diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseUtils.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseUtils.java index e6469c5664..49d44ee355 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseUtils.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseUtils.java @@ -18,12 +18,14 @@ package org.apache.maven.plugin.eclipse; import java.io.File; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Dependency; import org.apache.maven.model.Plugin; import org.apache.maven.model.Resource; import org.apache.maven.plugin.logging.Log; @@ -94,39 +96,36 @@ public class EclipseUtils return defaultValue; } - - - public static EclipseSourceDir[] buildDirectoryList( MavenProject project, File basedir, Log log, String outputDirectory ) + public static EclipseSourceDir[] buildDirectoryList( MavenProject project, File basedir, Log log, + String outputDirectory ) { File projectBaseDir = project.getFile().getParentFile(); // avoid duplicated entries Set directories = new TreeSet(); - EclipseUtils.extractSourceDirs( directories, project.getCompileSourceRoots(), basedir, projectBaseDir, false, null ); + EclipseUtils.extractSourceDirs( directories, project.getCompileSourceRoots(), basedir, projectBaseDir, false, + null ); - EclipseUtils.extractResourceDirs( directories, project.getBuild().getResources(), project, basedir, projectBaseDir, false, - null, log ); + EclipseUtils.extractResourceDirs( directories, project.getBuild().getResources(), project, basedir, + projectBaseDir, false, null, log ); // If using the standard output location, don't mix the test output into it. - String testOutput = outputDirectory.equals( project.getBuild().getOutputDirectory() ) ? - EclipseUtils.toRelativeAndFixSeparator( projectBaseDir, project.getBuild().getTestOutputDirectory(), false ) : - null; - - EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, true, - testOutput ); + String testOutput = outputDirectory.equals( project.getBuild().getOutputDirectory() ) ? EclipseUtils + .toRelativeAndFixSeparator( projectBaseDir, project.getBuild().getTestOutputDirectory(), false ) : null; - EclipseUtils.extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir, projectBaseDir, - true, testOutput, log ); + EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, + true, testOutput ); + + EclipseUtils.extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir, + projectBaseDir, true, testOutput, log ); return (EclipseSourceDir[]) directories.toArray( new EclipseSourceDir[directories.size()] ); } - - - private static void extractSourceDirs( Set directories, List sourceRoots, File basedir, File projectBaseDir, boolean test, - String output ) + private static void extractSourceDirs( Set directories, List sourceRoots, File basedir, File projectBaseDir, + boolean test, String output ) { for ( Iterator it = sourceRoots.iterator(); it.hasNext(); ) { @@ -143,7 +142,7 @@ public class EclipseUtils } private static void extractResourceDirs( Set directories, List resources, MavenProject project, File basedir, - File projectBaseDir, boolean test, String output, Log log ) + File projectBaseDir, boolean test, String output, Log log ) { for ( Iterator it = resources.iterator(); it.hasNext(); ) { @@ -209,7 +208,6 @@ public class EclipseUtils } } - /** * Utility method that locates a project producing the given artifact. * @@ -238,9 +236,7 @@ public class EclipseUtils return null; } - - /** * Returns the list of referenced artifacts produced by reactor projects. * @return List of Artifacts @@ -266,4 +262,29 @@ public class EclipseUtils return referencedProjects; } + public static void fixSystemScopeArtifacts( Collection artifacts, Collection dependencies ) + { + // fix path for system dependencies.Artifact.getFile() returns a wrong path in mvn 2.0 + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) + { + String groupid = artifact.getGroupId(); + String artifactId = artifact.getArtifactId(); + + for ( Iterator depIt = dependencies.iterator(); depIt.hasNext(); ) + { + Dependency dep = (Dependency) depIt.next(); + if ( Artifact.SCOPE_SYSTEM.equals( dep.getScope() ) && groupid.equals( dep.getGroupId() ) + && artifactId.equals( dep.getArtifactId() ) ) + { + artifact.setFile( new File( dep.getSystemPath() ) ); + break; + } + } + } + } + } + } diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseWtpmodulesWriter.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseWtpmodulesWriter.java index 5b3152f63c..a9ec8bd034 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseWtpmodulesWriter.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseWtpmodulesWriter.java @@ -16,6 +16,13 @@ package org.apache.maven.plugin.eclipse; * limitations under the License. */ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.plugin.MojoExecutionException; @@ -26,12 +33,6 @@ import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; - /** * Writes eclipse .wtpmodules file. * @@ -49,7 +50,7 @@ public class EclipseWtpmodulesWriter } protected void write( File basedir, MavenProject project, List referencedReactorArtifacts, - EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository ) + EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository ) throws MojoExecutionException { FileWriter w; @@ -60,8 +61,7 @@ public class EclipseWtpmodulesWriter } catch ( IOException ex ) { - throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), - ex ); //$NON-NLS-1$ + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$ } XMLWriter writer = new PrettyPrintXMLWriter( w ); @@ -176,7 +176,7 @@ public class EclipseWtpmodulesWriter } private void writeWarSpecificResources( XMLWriter writer, File basedir, MavenProject project, - List referencedReactorArtifacts, ArtifactRepository localRepository ) + List referencedReactorArtifacts, ArtifactRepository localRepository ) { String warSourceDirectory = EclipseUtils.getPluginSetting( project, "maven-war-plugin", //$NON-NLS-1$ @@ -189,16 +189,24 @@ public class EclipseWtpmodulesWriter EclipseUtils.toRelativeAndFixSeparator( basedir, warSourceDirectory, false ) ); writer.endElement(); + Set artifacts = project.getArtifacts(); + EclipseUtils.fixSystemScopeArtifacts( artifacts, project.getDependencies() ); + // dependencies - for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) { Artifact artifact = (Artifact) it.next(); - addDependency( writer, artifact, referencedReactorArtifacts, localRepository ); + String type = artifact.getType(); + + if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) ) + { + addDependency( writer, artifact, referencedReactorArtifacts, localRepository ); + } } } private void addDependency( XMLWriter writer, Artifact artifact, List referencedReactorProjects, - ArtifactRepository localRepository ) + ArtifactRepository localRepository ) { String handle; @@ -208,8 +216,7 @@ public class EclipseWtpmodulesWriter // uses // - handle = "module:/resource/" + artifact.getArtifactId() + "/" + - artifact.getArtifactId(); //$NON-NLS-1$ //$NON-NLS-2$ + handle = "module:/resource/" + artifact.getArtifactId() + "/" + artifact.getArtifactId(); //$NON-NLS-1$ //$NON-NLS-2$ } else { @@ -226,10 +233,19 @@ public class EclipseWtpmodulesWriter } String fullPath = artifactPath.getPath(); - File localRepositoryFile = new File( localRepository.getBasedir() ); - handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$ - + EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, fullPath, false ); + if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) + { + handle = "module:/classpath/lib/" //$NON-NLS-1$ + + StringUtils.replace( fullPath, "\\", "/" ); + } + else + { + File localRepositoryFile = new File( localRepository.getBasedir() ); + + handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$ + + EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, fullPath, false ); + } } writer.startElement( "dependent-module" ); //$NON-NLS-1$ diff --git a/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java b/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java index 5ff57ad9d0..2ca1001c77 100644 --- a/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java +++ b/maven-plugins/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/EclipsePluginTest.java @@ -65,11 +65,10 @@ public class EclipsePluginTest testProject( "project-7", null ); } - // @todo testcase for MNG-1324 "System" dependencies path non correctly added to eclipse buildpath - // public void testProject8() - // throws Exception - // { - // testProject( "project-8", null ); - // } + public void testProject8() + throws Exception + { + testProject( "project-8", null ); + } } diff --git a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/classpath b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/classpath index 9508906f7f..32769c1d72 100644 --- a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/classpath +++ b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/classpath @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/pom.xml b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/pom.xml index a835a32e1d..5b77413eb8 100644 --- a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/pom.xml +++ b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/pom.xml @@ -2,7 +2,7 @@ 4.0.0 - jar + war eclipse maven-eclipse-plugin-test-project-8 99.0 diff --git a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/wtpmodules b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/wtpmodules index 9f7d3b4235..8c75aef503 100644 --- a/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/wtpmodules +++ b/maven-plugins/maven-eclipse-plugin/src/test/projects/project-8/wtpmodules @@ -1,7 +1,12 @@ - - + + 2.4 + + + + uses + \ No newline at end of file