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
This commit is contained in:
Fabrizio Giustina 2005-10-31 16:37:19 +00:00
parent 47727cdc50
commit ea3a153a95
7 changed files with 126 additions and 86 deletions

View File

@ -16,6 +16,12 @@ package org.apache.maven.plugin.eclipse;
* limitations under the License. * 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.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository; 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.plugin.logging.Log;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil; 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.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter; 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. * 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 * @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, protected void write( File projectBaseDir, File basedir, MavenProject project, List referencedReactorArtifacts,
EclipseSourceDir[] sourceDirs, List classpathContainers, ArtifactRepository localRepository, EclipseSourceDir[] sourceDirs, List classpathContainers, ArtifactRepository localRepository,
ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, ArtifactResolver artifactResolver, ArtifactFactory artifactFactory,
List remoteArtifactRepositories, boolean downloadSources, String outputDirectory ) List remoteArtifactRepositories, boolean downloadSources, String outputDirectory )
throws MojoExecutionException throws MojoExecutionException
{ {
@ -72,8 +73,7 @@ public class EclipseClasspathWriter
} }
catch ( IOException ex ) catch ( IOException ex )
{ {
throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
ex ); //$NON-NLS-1$
} }
XMLWriter writer = new PrettyPrintXMLWriter( w ); XMLWriter writer = new PrettyPrintXMLWriter( w );
@ -107,7 +107,7 @@ public class EclipseClasspathWriter
writer.startElement( "classpathentry" ); //$NON-NLS-1$ writer.startElement( "classpathentry" ); //$NON-NLS-1$
writer.addAttribute( "kind", "output" ); //$NON-NLS-1$ //$NON-NLS-2$ 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 ) ); outputDirectory, false ) );
writer.endElement(); writer.endElement();
@ -126,6 +126,8 @@ public class EclipseClasspathWriter
List artifacts = project.getTestArtifacts(); List artifacts = project.getTestArtifacts();
EclipseUtils.fixSystemScopeArtifacts( artifacts, project.getDependencies() );
for ( Iterator it = artifacts.iterator(); it.hasNext(); ) for ( Iterator it = artifacts.iterator(); it.hasNext(); )
{ {
Artifact artifact = (Artifact) it.next(); Artifact artifact = (Artifact) it.next();
@ -154,9 +156,8 @@ public class EclipseClasspathWriter
} }
private void addDependency( XMLWriter writer, Artifact artifact, List referencedReactorArtifacts, private void addDependency( XMLWriter writer, Artifact artifact, List referencedReactorArtifacts,
ArtifactRepository localRepository, ArtifactResolver artifactResolver, ArtifactRepository localRepository, ArtifactResolver artifactResolver,
ArtifactFactory artifactFactory, List remoteArtifactRepositories, ArtifactFactory artifactFactory, List remoteArtifactRepositories, boolean downloadSources )
boolean downloadSources )
throws MojoExecutionException throws MojoExecutionException
{ {
@ -183,18 +184,18 @@ public class EclipseClasspathWriter
{ {
try try
{ {
path = artifactPath.getCanonicalPath(); path = StringUtils.replace( artifactPath.getCanonicalPath(), "\\", "/" );
} }
catch ( IOException e ) catch ( IOException e )
{ {
String message = Messages.getString( "EclipsePlugin.cantcanonicalize", artifactPath ); String message = Messages.getString( "EclipsePlugin.cantcanonicalize", artifactPath );
throw new MojoExecutionException( message, e ); throw new MojoExecutionException( message, e );
} }
log.info( Messages.getString( "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$ log.info( Messages.getString( "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$
new Object[] { artifact.getArtifactId(), path } ) ); new Object[] { artifact.getArtifactId(), path } ) );
log.info( Messages.getString( "EclipseClasspathWriter.sourcesnotavailable", //$NON-NLS-1$ log.info( Messages.getString( "EclipseClasspathWriter.sourcesnotavailable", //$NON-NLS-1$
artifact.getArtifactId() ) ); artifact.getArtifactId() ) );
@ -211,8 +212,9 @@ public class EclipseClasspathWriter
if ( downloadSources ) if ( downloadSources )
{ {
Artifact sourceArtifact = retrieveSourceArtifact( artifact, remoteArtifactRepositories, localRepository, Artifact sourceArtifact = retrieveSourceArtifact( artifact, remoteArtifactRepositories,
artifactResolver, artifactFactory ); localRepository, artifactResolver,
artifactFactory );
if ( !sourceArtifact.isResolved() ) if ( !sourceArtifact.isResolved() )
{ {
@ -222,12 +224,13 @@ public class EclipseClasspathWriter
else else
{ {
log.debug( Messages.getString( "EclipseClasspathWriter.sourcesavailable", //$NON-NLS-1$ log.debug( Messages.getString( "EclipseClasspathWriter.sourcesavailable", //$NON-NLS-1$
new Object[]{sourceArtifact.getArtifactId(), new Object[] {
sourceArtifact.getFile().getAbsolutePath()} ) ); sourceArtifact.getArtifactId(),
sourceArtifact.getFile().getAbsolutePath() } ) );
sourcepath = "M2_REPO/" //$NON-NLS-1$ sourcepath = "M2_REPO/" //$NON-NLS-1$
+ EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, + EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, sourceArtifact.getFile()
sourceArtifact.getFile().getAbsolutePath(), false ); .getAbsolutePath(), false );
} }
} }
@ -249,17 +252,14 @@ public class EclipseClasspathWriter
} }
private Artifact retrieveSourceArtifact( Artifact artifact, List remoteArtifactRepositories, private Artifact retrieveSourceArtifact( Artifact artifact, List remoteArtifactRepositories,
ArtifactRepository localRepository, ArtifactResolver artifactResolver, ArtifactRepository localRepository, ArtifactResolver artifactResolver,
ArtifactFactory artifactFactory ) ArtifactFactory artifactFactory )
throws MojoExecutionException throws MojoExecutionException
{ {
// source artifact: use the "sources" classifier added by the source plugin // source artifact: use the "sources" classifier added by the source plugin
Artifact sourceArtifact = artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), Artifact sourceArtifact = artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact
artifact.getArtifactId(), .getArtifactId(), artifact.getVersion(), "java-source", "sources" ); //$NON-NLS-1$ //$NON-NLS-2$
artifact.getVersion(), "java-source",
"sources" ); //$NON-NLS-1$ //$NON-NLS-2$
try try
{ {
@ -275,19 +275,18 @@ public class EclipseClasspathWriter
{ {
String message = Messages.getString( "EclipseClasspathWriter.cantresolvesources", //$NON-NLS-1$ String message = Messages.getString( "EclipseClasspathWriter.cantresolvesources", //$NON-NLS-1$
new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } ); new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } );
log.debug( message , e ); log.debug( message, e );
} }
} }
catch ( ArtifactResolutionException e ) catch ( ArtifactResolutionException e )
{ {
String message = Messages.getString( "EclipseClasspathWriter.errorresolvingsources", //$NON-NLS-1$ String message = Messages.getString( "EclipseClasspathWriter.errorresolvingsources", //$NON-NLS-1$
new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } ); new Object[] { sourceArtifact.getArtifactId(), e.getMessage() } );
throw new MojoExecutionException( message, e ); throw new MojoExecutionException( message, e );
} }
return sourceArtifact; return sourceArtifact;
} }
} }

View File

@ -18,12 +18,14 @@ package org.apache.maven.plugin.eclipse;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.model.Resource; import org.apache.maven.model.Resource;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
@ -94,39 +96,36 @@ public class EclipseUtils
return defaultValue; 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(); File projectBaseDir = project.getFile().getParentFile();
// avoid duplicated entries // avoid duplicated entries
Set directories = new TreeSet(); 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, EclipseUtils.extractResourceDirs( directories, project.getBuild().getResources(), project, basedir,
null, log ); projectBaseDir, false, null, log );
// If using the standard output location, don't mix the test output into it. // If using the standard output location, don't mix the test output into it.
String testOutput = outputDirectory.equals( project.getBuild().getOutputDirectory() ) ? String testOutput = outputDirectory.equals( project.getBuild().getOutputDirectory() ) ? EclipseUtils
EclipseUtils.toRelativeAndFixSeparator( projectBaseDir, project.getBuild().getTestOutputDirectory(), false ) : .toRelativeAndFixSeparator( projectBaseDir, project.getBuild().getTestOutputDirectory(), false ) : null;
null;
EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, true,
testOutput );
EclipseUtils.extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir, projectBaseDir, EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir,
true, testOutput, log ); true, testOutput );
EclipseUtils.extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir,
projectBaseDir, true, testOutput, log );
return (EclipseSourceDir[]) directories.toArray( new EclipseSourceDir[directories.size()] ); 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(); ) 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, 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(); ) for ( Iterator it = resources.iterator(); it.hasNext(); )
{ {
@ -209,7 +208,6 @@ public class EclipseUtils
} }
} }
/** /**
* Utility method that locates a project producing the given artifact. * Utility method that locates a project producing the given artifact.
* *
@ -238,9 +236,7 @@ public class EclipseUtils
return null; return null;
} }
/** /**
* Returns the list of referenced artifacts produced by reactor projects. * Returns the list of referenced artifacts produced by reactor projects.
* @return List of Artifacts * @return List of Artifacts
@ -266,4 +262,29 @@ public class EclipseUtils
return referencedProjects; 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;
}
}
}
}
}
} }

View File

@ -16,6 +16,13 @@ package org.apache.maven.plugin.eclipse;
* limitations under the License. * 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.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException; 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.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter; 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. * Writes eclipse .wtpmodules file.
* *
@ -49,7 +50,7 @@ public class EclipseWtpmodulesWriter
} }
protected void write( File basedir, MavenProject project, List referencedReactorArtifacts, protected void write( File basedir, MavenProject project, List referencedReactorArtifacts,
EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository ) EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository )
throws MojoExecutionException throws MojoExecutionException
{ {
FileWriter w; FileWriter w;
@ -60,8 +61,7 @@ public class EclipseWtpmodulesWriter
} }
catch ( IOException ex ) catch ( IOException ex )
{ {
throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
ex ); //$NON-NLS-1$
} }
XMLWriter writer = new PrettyPrintXMLWriter( w ); XMLWriter writer = new PrettyPrintXMLWriter( w );
@ -176,7 +176,7 @@ public class EclipseWtpmodulesWriter
} }
private void writeWarSpecificResources( XMLWriter writer, File basedir, MavenProject project, 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$ String warSourceDirectory = EclipseUtils.getPluginSetting( project, "maven-war-plugin", //$NON-NLS-1$
@ -189,16 +189,24 @@ public class EclipseWtpmodulesWriter
EclipseUtils.toRelativeAndFixSeparator( basedir, warSourceDirectory, false ) ); EclipseUtils.toRelativeAndFixSeparator( basedir, warSourceDirectory, false ) );
writer.endElement(); writer.endElement();
Set artifacts = project.getArtifacts();
EclipseUtils.fixSystemScopeArtifacts( artifacts, project.getDependencies() );
// dependencies // dependencies
for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) for ( Iterator it = artifacts.iterator(); it.hasNext(); )
{ {
Artifact artifact = (Artifact) it.next(); 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, private void addDependency( XMLWriter writer, Artifact artifact, List referencedReactorProjects,
ArtifactRepository localRepository ) ArtifactRepository localRepository )
{ {
String handle; String handle;
@ -208,8 +216,7 @@ public class EclipseWtpmodulesWriter
// <dependency-type>uses</dependency-type> // <dependency-type>uses</dependency-type>
// </dependent-module> // </dependent-module>
handle = "module:/resource/" + artifact.getArtifactId() + "/" + handle = "module:/resource/" + artifact.getArtifactId() + "/" + artifact.getArtifactId(); //$NON-NLS-1$ //$NON-NLS-2$
artifact.getArtifactId(); //$NON-NLS-1$ //$NON-NLS-2$
} }
else else
{ {
@ -226,10 +233,19 @@ public class EclipseWtpmodulesWriter
} }
String fullPath = artifactPath.getPath(); String fullPath = artifactPath.getPath();
File localRepositoryFile = new File( localRepository.getBasedir() );
handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$ if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
+ EclipseUtils.toRelativeAndFixSeparator( localRepositoryFile, fullPath, false ); {
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$ writer.startElement( "dependent-module" ); //$NON-NLS-1$

View File

@ -65,11 +65,10 @@ public class EclipsePluginTest
testProject( "project-7", null ); testProject( "project-7", null );
} }
// @todo testcase for MNG-1324 "System" dependencies path non correctly added to eclipse buildpath public void testProject8()
// public void testProject8() throws Exception
// throws Exception {
// { testProject( "project-8", null );
// testProject( "project-8", null ); }
// }
} }

View File

@ -1,5 +1,5 @@
<classpath> <classpath>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="${basedir}/sysdep.jar"/> <classpathentry kind="lib" path="${basedir}/src/test/projects/project-8/sysdep.jar"/>
</classpath> </classpath>

View File

@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging> <packaging>war</packaging>
<groupId>eclipse</groupId> <groupId>eclipse</groupId>
<artifactId>maven-eclipse-plugin-test-project-8</artifactId> <artifactId>maven-eclipse-plugin-test-project-8</artifactId>
<version>99.0</version> <version>99.0</version>

View File

@ -1,7 +1,12 @@
<project-modules id="moduleCoreId"> <project-modules id="moduleCoreId">
<wb-module deploy-name="maven-eclipse-plugin-test-project-8"> <wb-module deploy-name="maven-eclipse-plugin-test-project-8">
<module-type module-type-id="jst.utility"> <module-type module-type-id="jst.web">
<property name="java-output-path" value="/target/classes"/> <version>2.4</version>
<property name="context-root" value="maven-eclipse-plugin-test-project-8"/>
</module-type> </module-type>
<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
<dependent-module deploy-path="/WEB-INF/lib" handle="module:/classpath/lib/${basedir}/src/test/projects/project-8/sysdep.jar">
<dependency-type>uses</dependency-type>
</dependent-module>
</wb-module> </wb-module>
</project-modules> </project-modules>