o Project references are now written to .project, so that eclipse

can calculate the build order - no more rebuilds needed to fix this!

o Added a switch -Declipse.workpace=/path/to/workspace that allows for the
  .classpath and .project files to be written to a new directory:
  - For each project a new directory is created.
  - Eclipse 'Links' are created to the original project location for the
    sources and resources directories.
  - The target directory will be in the new workspace location, not in
    the original project tree.
  
  This offers the following advantages:
  
  - Project trees that normally couldn't be imported into eclipse
  because of the overlap error now can be imported succesfully
  (m2 works now!)

  - Projects that reside on a ClearCase SMB mounted drive (which are
  very slow) can now have a target directory on a local drive without
  modifying the pom, greatly improving build speed in eclipse.

o Fixed some minor bugs where the wrong project instance was used.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@226669 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kenney Westerhof 2005-07-31 18:00:13 +00:00
parent d09c2affad
commit b294585343
2 changed files with 209 additions and 40 deletions

View File

@ -29,7 +29,7 @@ import java.util.List;
* *
* @goal eclipse * @goal eclipse
* @requiresDependencyResolution test * @requiresDependencyResolution test
* @executePhase generate-sources * @execute phase="generate-sources"
* *
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a> * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$ * @version $Id$
@ -66,6 +66,11 @@ public class EclipsePlugin
*/ */
private List reactorProjects; private List reactorProjects;
/**
* @parameter expression="${eclipse.workspace}"
*/
private File outputDir;
public EclipsePlugin() public EclipsePlugin()
{ {
eclipseWriter = new EclipseWriter(); eclipseWriter = new EclipseWriter();
@ -88,6 +93,7 @@ public class EclipsePlugin
{ {
throw new MojoExecutionException( "There must be a POM in the current working directory for the Eclipse plugin to work." ); throw new MojoExecutionException( "There must be a POM in the current working directory for the Eclipse plugin to work." );
} }
if ( "pom".equals( project.getPackaging() ) ) if ( "pom".equals( project.getPackaging() ) )
{ {
getLog().info( "Don't generate Eclipse project for pom project" ); getLog().info( "Don't generate Eclipse project for pom project" );
@ -95,17 +101,38 @@ public class EclipsePlugin
return; return;
} }
if ( outputDir == null )
{
outputDir = project.getFile().getParentFile();
}
else
{
if ( !outputDir.isDirectory() )
{
throw new MojoExecutionException( "Not a directory: '" + outputDir + "'" );
}
outputDir = new File( outputDir, project.getArtifactId() );
if ( !outputDir.isDirectory() && !outputDir.mkdir() )
{
throw new MojoExecutionException( "Can't create directory '" + outputDir + "'" );
}
}
try try
{ {
eclipseWriter.setLocalRepositoryFile( new File ( localRepository.getBasedir() ) ); eclipseWriter.setLocalRepositoryFile( new File ( localRepository.getBasedir() ) );
eclipseWriter.setLog( getLog() );
if ( executedProject == null ) if ( executedProject == null )
{ {
// backwards compat with alpha-2 only // backwards compat with alpha-2 only
executedProject = project; executedProject = project;
} }
eclipseWriter.write( project, executedProject, reactorProjects ); eclipseWriter.write( outputDir, project, executedProject, reactorProjects );
} }
catch ( EclipsePluginException e ) catch ( EclipsePluginException e )
{ {

View File

@ -18,6 +18,7 @@ package org.apache.maven.plugin.eclipse;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource; import org.apache.maven.model.Resource;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
@ -27,6 +28,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -35,10 +37,13 @@ import java.util.Set;
/** /**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a> * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
* @version $Id$ * @version $Id$
*/ */
public class EclipseWriter public class EclipseWriter
{ {
private Log log;
private File localRepository; private File localRepository;
public void setLocalRepositoryFile( File localRepository ) public void setLocalRepositoryFile( File localRepository )
@ -46,11 +51,14 @@ public class EclipseWriter
this.localRepository = localRepository; this.localRepository = localRepository;
} }
public void write( MavenProject project, MavenProject executedProject, List reactorProjects ) public void setLog(Log log)
{
this.log = log;
}
public void write( File outputDir, MavenProject project, MavenProject executedProject, List reactorProjects )
throws EclipsePluginException throws EclipsePluginException
{ {
File basedir = project.getFile().getParentFile();
Map map = new HashMap(); Map map = new HashMap();
assertNotEmpty( project.getGroupId(), "groupId" ); assertNotEmpty( project.getGroupId(), "groupId" );
@ -59,18 +67,20 @@ public class EclipseWriter
map.put( "project.artifactId", project.getArtifactId() ); map.put( "project.artifactId", project.getArtifactId() );
writeEclipseProject( basedir, project, map ); File projectBaseDir = project.getFile().getParentFile();
writeEclipseClasspath( basedir, project, executedProject, map, reactorProjects ); List referencedProjects = writeEclipseClasspath( projectBaseDir, outputDir, project, executedProject, map, reactorProjects );
System.out.println( "Wrote Eclipse project for " + project.getArtifactId() + " to " + basedir.getAbsolutePath() ); writeEclipseProject( projectBaseDir, outputDir, project, executedProject, referencedProjects, map );
log.info( "Wrote Eclipse project for " + project.getArtifactId() + " to " + outputDir.getAbsolutePath() );
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// .project // .project
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
protected void writeEclipseProject( File basedir, MavenProject project, Map map ) protected void writeEclipseProject( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, List referencedProjects, Map map )
throws EclipsePluginException throws EclipsePluginException
{ {
FileWriter w; FileWriter w;
@ -111,11 +121,17 @@ public class EclipseWriter
writer.endElement(); writer.endElement();
// TODO: Add project dependencies here
// Should look in the reactor for other projects
writer.startElement( "projects" ); writer.startElement( "projects" );
for ( Iterator it = referencedProjects.iterator(); it.hasNext(); )
{
writer.startElement( "project" );
writer.writeText( ( (MavenProject) it.next() ).getArtifactId() );
writer.endElement();
}
writer.endElement(); // projects writer.endElement(); // projects
writer.startElement( "buildSpec" ); writer.startElement( "buildSpec" );
@ -146,6 +162,22 @@ public class EclipseWriter
writer.endElement(); // natures writer.endElement(); // natures
if ( ! projectBaseDir.equals( basedir ) )
{
writer.startElement( "linkedResources" );
addSourceLinks( writer, projectBaseDir, basedir, executedProject.getCompileSourceRoots() );
addResourceLinks( writer, projectBaseDir, basedir, executedProject.getBuild().getResources() );
addSourceLinks( writer, projectBaseDir, basedir, executedProject.getTestCompileSourceRoots() );
addResourceLinks( writer, projectBaseDir, basedir, executedProject.getBuild().getTestResources() );
writer.endElement(); // linedResources
}
writer.endElement(); // projectDescription writer.endElement(); // projectDescription
close( w ); close( w );
@ -155,7 +187,7 @@ public class EclipseWriter
// .classpath // .classpath
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
protected void writeEclipseClasspath( File basedir, MavenProject project, MavenProject executedProject, Map map, List reactorProjects ) protected List writeEclipseClasspath( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, Map map, List reactorProjects )
throws EclipsePluginException throws EclipsePluginException
{ {
FileWriter w; FileWriter w;
@ -177,11 +209,11 @@ public class EclipseWriter
// The source roots // The source roots
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
addSourceRoots( writer, project.getBasedir(), addSourceRoots( writer, projectBaseDir, basedir,
executedProject.getCompileSourceRoots(), executedProject.getCompileSourceRoots(),
null ); null );
addResources( writer, project.getBasedir(), addResources( writer, projectBaseDir, basedir,
project.getBuild().getResources(), project.getBuild().getResources(),
null ); null );
@ -189,11 +221,11 @@ public class EclipseWriter
// The test sources and resources // The test sources and resources
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
addSourceRoots( writer, project.getBasedir(), addSourceRoots( writer, projectBaseDir, basedir,
executedProject.getTestCompileSourceRoots(), executedProject.getTestCompileSourceRoots(),
project.getBuild().getTestOutputDirectory() ); project.getBuild().getTestOutputDirectory() );
addResources( writer, project.getBasedir(), addResources( writer, projectBaseDir, basedir,
project.getBuild().getTestResources(), project.getBuild().getTestResources(),
project.getBuild().getTestOutputDirectory() ); project.getBuild().getTestOutputDirectory() );
@ -205,7 +237,7 @@ public class EclipseWriter
writer.addAttribute( "kind", "output" ); writer.addAttribute( "kind", "output" );
writer.addAttribute( "path", toRelative( basedir, project.getBuild().getOutputDirectory() ) ); writer.addAttribute( "path", toRelative( projectBaseDir, project.getBuild().getOutputDirectory() ) );
writer.endElement(); writer.endElement();
@ -229,25 +261,34 @@ public class EclipseWriter
// The dependencies // The dependencies
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
List referencedProjects = new ArrayList();
Set artifacts = project.getArtifacts(); Set artifacts = project.getArtifacts();
for ( Iterator it = artifacts.iterator(); it.hasNext(); ) for ( Iterator it = artifacts.iterator(); it.hasNext(); )
{ {
Artifact artifact = (Artifact) it.next(); Artifact artifact = (Artifact) it.next();
addDependency( writer, artifact, reactorProjects ); MavenProject refProject = addDependency( writer, artifact, reactorProjects );
if ( refProject != null )
{
referencedProjects.add( refProject );
}
} }
writer.endElement(); writer.endElement();
close( w ); close( w );
return referencedProjects;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// //
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private void addSourceRoots( XMLWriter writer, File basedir, List sourceRoots, String output ) private void addSourceRoots( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots, String output )
{ {
for ( Iterator it = sourceRoots.iterator(); it.hasNext(); ) for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
{ {
@ -259,11 +300,15 @@ public class EclipseWriter
writer.addAttribute( "kind", "src" ); writer.addAttribute( "kind", "src" );
writer.addAttribute( "path", toRelative( basedir, sourceRoot ) ); sourceRoot = toRelative( projectBaseDir, sourceRoot );
if (!projectBaseDir.equals(basedir))
sourceRoot = sourceRoot.replaceAll("/", "-");
writer.addAttribute( "path", sourceRoot );
if ( output != null ) if ( output != null )
{ {
writer.addAttribute( "output", toRelative( basedir, output ) ); writer.addAttribute( "output", toRelative( projectBaseDir, output ) );
} }
writer.endElement(); writer.endElement();
@ -271,7 +316,7 @@ public class EclipseWriter
} }
} }
private void addResources( XMLWriter writer, File basedir, List resources, String output ) private void addResources( XMLWriter writer, File projectBaseDir, File basedir, List resources, String output )
{ {
for ( Iterator it = resources.iterator(); it.hasNext(); ) for ( Iterator it = resources.iterator(); it.hasNext(); )
{ {
@ -279,17 +324,17 @@ public class EclipseWriter
if ( resource.getIncludes().size() != 0 ) if ( resource.getIncludes().size() != 0 )
{ {
System.err.println( "This plugin currently doesn't support include patterns for resources. Adding the entire directory." ); log.warn( "This plugin currently doesn't support include patterns for resources. Adding the entire directory." );
} }
if ( resource.getExcludes().size() != 0 ) if ( resource.getExcludes().size() != 0 )
{ {
System.err.println( "This plugin currently doesn't support exclude patterns for resources. Adding the entire directory." ); log.warn( "This plugin currently doesn't support exclude patterns for resources. Adding the entire directory." );
} }
if ( !StringUtils.isEmpty( resource.getTargetPath() ) ) if ( !StringUtils.isEmpty( resource.getTargetPath() ) )
{ {
System.err.println( "This plugin currently doesn't support target paths for resources." ); log.error( "This plugin currently doesn't support target paths for resources." );
return; return;
} }
@ -305,36 +350,124 @@ public class EclipseWriter
writer.addAttribute( "kind", "src" ); writer.addAttribute( "kind", "src" );
writer.addAttribute( "path", toRelative( basedir, resource.getDirectory() ) ); String resourceDir = resource.getDirectory();
resourceDir = toRelative( projectBaseDir, resourceDir );
if (!projectBaseDir.equals(basedir))
resourceDir = resourceDir.replaceAll("/", "-");
writer.addAttribute( "path", resourceDir );
if ( output != null ) if ( output != null )
{ {
writer.addAttribute( "output", toRelative( basedir, output ) ); writer.addAttribute( "output", toRelative( projectBaseDir, output ) );
} }
writer.endElement(); writer.endElement();
} }
} }
private void addDependency( XMLWriter writer, Artifact artifact, List reactorProjects ) private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
{ {
String path = getProjectPath( reactorProjects, artifact ); for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
{
String sourceRoot = (String) it.next();
String kind = path == null ? "var" : "src"; if ( new File( sourceRoot ).isDirectory() )
{
writer.startElement( "link" );
// fall-through when no local project could be found in the reactor writer.startElement( "name" );
if ( path == null )
writer.writeText( toRelative( projectBaseDir, sourceRoot ).replaceAll("/", "-") );
writer.endElement(); // name
writer.startElement( "type" );
writer.writeText( "2" );
writer.endElement(); // type
writer.startElement( "location" );
writer.writeText( sourceRoot );
writer.endElement(); // location
writer.endElement(); // link
}
}
}
private void addResourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
{
for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
{
String resourceDir = ((Resource) it.next() ).getDirectory();
if ( new File( resourceDir ).isDirectory() )
{
writer.startElement( "link" );
writer.startElement( "name" );
writer.writeText( toRelative( projectBaseDir, resourceDir ).replaceAll("/", "-") );
writer.endElement(); // name
writer.startElement( "type" );
writer.writeText( "2" );
writer.endElement(); // type
writer.startElement( "location" );
writer.writeText( resourceDir );
writer.endElement(); // location
writer.endElement(); // link
}
}
}
/**
*
* @param writer
* @param artifact
* @param reactorProjects
* @return null or the reactorProject providing this dependency
*/
private MavenProject addDependency( XMLWriter writer, Artifact artifact, List reactorProjects )
{
MavenProject reactorProject = findReactorProject( reactorProjects, artifact );
String path = null;
String kind = null;
if (reactorProject != null)
{
path = "/" + reactorProject.getArtifactId();
kind = "src";
}
else
{ {
File artifactPath = artifact.getFile(); File artifactPath = artifact.getFile();
if ( artifactPath == null ) if ( artifactPath == null )
{ {
System.err.println( "The artifacts path was null. Artifact id: " + artifact.getId() ); log.error( "The artifacts path was null. Artifact id: " + artifact.getId() );
return; return null;
} }
path = "M2_REPO/" + toRelative( localRepository, artifactPath.getPath() ); path = "M2_REPO/" + toRelative( localRepository, artifactPath.getPath() );
kind = "var";
} }
writer.startElement( "classpathentry" ); writer.startElement( "classpathentry" );
@ -344,9 +477,18 @@ public class EclipseWriter
writer.addAttribute( "path", path ); writer.addAttribute( "path", path );
writer.endElement(); writer.endElement();
return reactorProject;
} }
private String getProjectPath( List reactorProjects, Artifact artifact ) /**
* Utility method that locates a project producing the given artifact.
*
* @param reactorProjects a list of projects to search.
* @param artifact the artifact a project should produce.
* @return null or the first project found producing the artifact.
*/
private static MavenProject findReactorProject( List reactorProjects, Artifact artifact )
{ {
if ( reactorProjects == null ) if ( reactorProjects == null )
{ {
@ -362,7 +504,7 @@ public class EclipseWriter
&& project.getVersion().equals( artifact.getVersion() ) && project.getVersion().equals( artifact.getVersion() )
) )
{ {
return "/" + project.getArtifactId(); return project;
} }
} }