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.
*/
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,7 +184,7 @@ public class EclipseClasspathWriter
{
try
{
path = artifactPath.getCanonicalPath();
path = StringUtils.replace( artifactPath.getCanonicalPath(), "\\", "/" );
}
catch ( IOException e )
{
@ -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
{
@ -276,7 +276,7 @@ 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 )
@ -290,4 +290,3 @@ public class EclipseClasspathWriter
return sourceArtifact;
}
}

View File

@ -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;
@ -95,38 +97,35 @@ 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;
String testOutput = outputDirectory.equals( project.getBuild().getOutputDirectory() ) ? EclipseUtils
.toRelativeAndFixSeparator( projectBaseDir, project.getBuild().getTestOutputDirectory(), false ) : null;
EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, true,
testOutput );
EclipseUtils.extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir,
true, testOutput );
EclipseUtils.extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir, projectBaseDir,
true, testOutput, log );
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.
*
@ -239,8 +237,6 @@ 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;
}
}
}
}
}
}

View File

@ -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
// <dependency-type>uses</dependency-type>
// </dependent-module>
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$

View File

@ -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 );
}
}

View File

@ -1,5 +1,5 @@
<classpath>
<classpathentry kind="output" path="target/classes"/>
<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>

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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<packaging>war</packaging>
<groupId>eclipse</groupId>
<artifactId>maven-eclipse-plugin-test-project-8</artifactId>
<version>99.0</version>

View File

@ -1,7 +1,12 @@
<project-modules id="moduleCoreId">
<wb-module deploy-name="maven-eclipse-plugin-test-project-8">
<module-type module-type-id="jst.utility">
<property name="java-output-path" value="/target/classes"/>
<module-type module-type-id="jst.web">
<version>2.4</version>
<property name="context-root" value="maven-eclipse-plugin-test-project-8"/>
</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>
</project-modules>