o Fix MNG-410 (and MNG-769, MNG-738). Added testcase project-3 to demonstrate this.

o Added testcase to check generation of eclipse files in
  different location; modified the testcase to support this,
  and deal with the absolute paths.

o Neatified the addSourceLinks/addResourceLinks: using
  a map to keep track of duplicate source folders has the side
  effect of only needing one of those methods; it should
  also be faster :)

o Tried to add support for includes/excludes, but eclipse only
  supports unique source directories; this is typically not the
  case in maven, filters are used to split one resourcedir
  up. Left the code in for future reference.
  TODO: find a way to merge includes/excludes. If only
  the dir where in/excludes are specified is unique, they
  could be written without problem. This requires a preprocessing
  pass.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@240143 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kenney Westerhof 2005-08-25 21:28:30 +00:00
parent 39a6096086
commit a98961eeed
17 changed files with 317 additions and 78 deletions

View File

@ -86,6 +86,11 @@ public class EclipsePlugin
this.localRepository = localRepository;
}
public void setOutputDir( File outputDir )
{
this.outputDir = outputDir;
}
public void execute()
throws MojoExecutionException
{
@ -105,7 +110,7 @@ public class EclipsePlugin
{
outputDir = project.getFile().getParentFile();
}
else
else if ( !outputDir.equals( project.getFile().getParentFile() ) )
{
if ( !outputDir.isDirectory() )
{

View File

@ -32,8 +32,8 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@ -70,12 +70,16 @@ public class EclipseWriter
assertNotEmpty( project.getArtifactId(), "artifactId" );
File projectBaseDir = project.getFile().getParentFile();
Collection referencedProjects = writeEclipseClasspath( projectBaseDir, outputDir, project, executedProject, reactorProjects );
writeEclipseProject( projectBaseDir, outputDir, project, executedProject, referencedProjects );
Map eclipseSourceRoots = new HashMap();
writeEclipseSettings( projectBaseDir, outputDir, project, executedProject );
Collection referencedProjects = writeEclipseClasspath(
projectBaseDir, outputDir, project, executedProject, reactorProjects, eclipseSourceRoots
);
writeEclipseProject( projectBaseDir, outputDir, project, executedProject, referencedProjects, eclipseSourceRoots );
writeEclipseSettings( projectBaseDir, outputDir, project, executedProject);
log.info( "Wrote Eclipse project for " + project.getArtifactId() + " to " + outputDir.getAbsolutePath() );
}
@ -144,7 +148,7 @@ public class EclipseWriter
// .project
// ----------------------------------------------------------------------
protected void writeEclipseProject( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, Collection referencedProjects )
protected void writeEclipseProject( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, Collection referencedProjects, Map eclipseSourceRoots )
throws EclipsePluginException
{
FileWriter w;
@ -232,14 +236,8 @@ public class EclipseWriter
writer.startElement( "linkedResources" );
addFileLink( writer, projectBaseDir, basedir, project.getFile() );
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() );
addSourceLinks( writer, projectBaseDir, basedir, eclipseSourceRoots );
writer.endElement(); // linkedResources
}
@ -253,7 +251,7 @@ public class EclipseWriter
// .classpath
// ----------------------------------------------------------------------
protected Collection writeEclipseClasspath( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, List reactorProjects )
protected Collection writeEclipseClasspath( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, List reactorProjects, Map eclipseSourceRoots )
throws EclipsePluginException
{
FileWriter w;
@ -277,11 +275,11 @@ public class EclipseWriter
addSourceRoots( writer, projectBaseDir, basedir,
executedProject.getCompileSourceRoots(),
null );
null, eclipseSourceRoots );
addResources( writer, projectBaseDir, basedir,
project.getBuild().getResources(),
null );
null, eclipseSourceRoots );
// ----------------------------------------------------------------------
// The test sources and resources
@ -289,11 +287,13 @@ public class EclipseWriter
addSourceRoots( writer, projectBaseDir, basedir,
executedProject.getTestCompileSourceRoots(),
project.getBuild().getTestOutputDirectory() );
project.getBuild().getTestOutputDirectory(),
eclipseSourceRoots );
addResources( writer, projectBaseDir, basedir,
project.getBuild().getTestResources(),
project.getBuild().getTestOutputDirectory() );
project.getBuild().getTestOutputDirectory(),
eclipseSourceRoots );
// ----------------------------------------------------------------------
// The default output
@ -349,7 +349,7 @@ public class EclipseWriter
//
// ----------------------------------------------------------------------
private void addSourceRoots( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots, String output )
private void addSourceRoots( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots, String output, Map addedSourceRoots )
{
for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
{
@ -357,17 +357,27 @@ public class EclipseWriter
if ( new File( sourceRoot ).isDirectory() )
{
// Don't add the same sourceroots twice. No include/exclude
// patterns possible in maven for (test|script|)source directories.
if ( addedSourceRoots.containsKey( sourceRoot ) )
{
continue;
}
writer.startElement( "classpathentry" );
writer.addAttribute( "kind", "src" );
sourceRoot = toRelative( projectBaseDir, sourceRoot );
String eclipseSourceRoot = toRelative( projectBaseDir, sourceRoot );
if (!projectBaseDir.equals(basedir))
{
sourceRoot = sourceRoot.replaceAll( "/", "-" );
eclipseSourceRoot = eclipseSourceRoot.replaceAll( "/", "-" );
}
writer.addAttribute( "path", sourceRoot );
addedSourceRoots.put( sourceRoot, eclipseSourceRoot );
writer.addAttribute( "path", eclipseSourceRoot );
if ( output != null )
{
@ -379,7 +389,7 @@ public class EclipseWriter
}
}
private void addResources( XMLWriter writer, File projectBaseDir, File basedir, List resources, String output )
private void addResources( XMLWriter writer, File projectBaseDir, File basedir, List resources, String output, Map addedSourceRoots )
{
for ( Iterator it = resources.iterator(); it.hasNext(); )
{
@ -397,9 +407,7 @@ public class EclipseWriter
if ( !StringUtils.isEmpty( resource.getTargetPath() ) )
{
log.error( "This plugin currently doesn't support target paths for resources." );
return;
output = resource.getTargetPath();
}
File resourceDirectory = new File( resource.getDirectory() );
@ -409,19 +417,50 @@ public class EclipseWriter
continue;
}
String resourceDir = resource.getDirectory();
// don't add the same sourceroot twice; eclipse can't handle
// that, even with mutual exclusive include/exclude patterns.
if ( addedSourceRoots.containsKey( resourceDir ) )
{
continue;
}
String eclipseResourceDir = toRelative( projectBaseDir, resourceDir );
if ( ! projectBaseDir.equals( basedir ) )
{
eclipseResourceDir = eclipseResourceDir.replaceAll( "/", "-" );
}
addedSourceRoots.put( resourceDir, eclipseResourceDir );
writer.startElement( "classpathentry" );
writer.addAttribute( "kind", "src" );
String resourceDir = resource.getDirectory();
resourceDir = toRelative( projectBaseDir, resourceDir );
if (!projectBaseDir.equals(basedir))
{
resourceDir = resourceDir.replaceAll( "/", "-" );
}
writer.addAttribute( "path", resourceDir );
writer.addAttribute( "path", eclipseResourceDir );
// Example of setting include/exclude patterns for future reference.
//
// TODO: figure out how to merge if the same dir is specified twice
// with different in/exclude patterns. We can't write them now,
// since only the the first one would be included.
//
// if ( resource.getIncludes().size() != 0 )
// {
// writer.addAttribute(
// "including", StringUtils.join( resource.getIncludes().iterator(), "|" )
// );
// }
//
// if ( resource.getExcludes().size() != 0 )
// {
// writer.addAttribute(
// "excluding", StringUtils.join( resource.getExcludes().iterator(), "|" )
// );
// }
if ( output != null )
{
writer.addAttribute( "output", toRelative( projectBaseDir, output ) );
@ -431,11 +470,17 @@ public class EclipseWriter
}
}
private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, Map sourceRoots )
{
for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
for ( Iterator it = sourceRoots.keySet().iterator(); it.hasNext(); )
{
String sourceRoot = (String) it.next();
String linkName = (String) sourceRoots.get( sourceRoot );
sourceRoot = sourceRoot.replaceAll("\\\\", "/");
log.debug( "Adding link '" + linkName + "' to '" + sourceRoot + "'" );
if ( new File( sourceRoot ).isDirectory() )
{
@ -443,7 +488,7 @@ public class EclipseWriter
writer.startElement( "name" );
writer.writeText( toRelative( projectBaseDir, sourceRoot ).replaceAll( "/", "-" ) );
writer.writeText( linkName );
writer.endElement(); // name
@ -455,40 +500,7 @@ public class EclipseWriter
writer.startElement( "location" );
writer.writeText( sourceRoot.replaceAll("\\\\", "/") );
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.replaceAll( "\\\\", "/" ) );
writer.writeText( sourceRoot );
writer.endElement(); // location

View File

@ -23,6 +23,7 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.StringUtils;
import java.io.BufferedReader;
import java.io.File;
@ -42,20 +43,32 @@ public class EclipsePluginTest
public void testProject1()
throws Exception
{
testProject( "project-1" );
testProject( "project-1", null );
}
public void testProject2()
throws Exception
{
testProject( "project-2" );
testProject( "project-2", null );
}
public void testProject3()
throws Exception
{
testProject( "project-3", null );
}
public void testProject4()
throws Exception
{
testProject( "project-4", getTestFile( "target/project-4-test/" ) );
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
private void testProject( String projectName )
private void testProject( String projectName, File outputDir )
throws Exception
{
File basedir = getTestFile( "src/test/projects/" + projectName );
@ -73,6 +86,24 @@ public class EclipsePluginTest
MavenProject project = builder.buildWithDependencies( new File( basedir, "project.xml" ), localRepository, null );
File projectOutputDir = basedir;
if ( outputDir == null )
{
outputDir = basedir;
}
else
{
outputDir.mkdirs();
projectOutputDir = new File( outputDir, project.getArtifactId() );
}
System.err.println("basedir: " + basedir+"\noutputdir: " + outputDir+"\nprojectOutputDir: " + projectOutputDir );
plugin.setOutputDir( outputDir );
for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
{
Artifact artifact = (Artifact) it.next();
@ -85,9 +116,9 @@ public class EclipsePluginTest
plugin.execute();
assertFileEquals( localRepository.getBasedir(), new File( basedir, "project" ), new File( basedir, ".project" ) );
assertFileEquals( localRepository.getBasedir(), new File( basedir, "project" ), new File( projectOutputDir, ".project" ) );
assertFileEquals( localRepository.getBasedir(), new File( basedir, "classpath" ), new File( basedir, ".classpath" ) );
assertFileEquals( localRepository.getBasedir(), new File( basedir, "classpath" ), new File( projectOutputDir, ".classpath" ) );
}
private void assertFileEquals( String mavenRepo, File expectedFile, File actualFile )
@ -101,6 +132,11 @@ public class EclipsePluginTest
{
String expected = expectedLines.get( i ).toString();
// replace some vars in the expected line, to account
// for absolute paths that are different on each installation.
expected = StringUtils.replace( expected, "${basedir}", basedir );
if ( actualLines.size() < i )
{
fail( "Too few lines in the actual file. Was " + actualLines.size() + ", expected: " + expectedLines.size() );

View File

@ -0,0 +1,8 @@
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources" output="target/main-resources"/>
<classpathentry kind="src" path="src/test/resources" output="target/test-output-dir"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" rootpath="JRE_SRCROOT" path="JRE_LIB" sourcepath="JRE_SRC"/>
<classpathentry kind="var" path="M2_REPO/junit/jars/junit-2.0.jar"/>
</classpath>

View File

@ -0,0 +1,17 @@
<projectDescription>
<name>maven-eclipse-plugin-test-project-3</name>
<comment>Tests creating just one sourceroot when test sourceroot and
main sourceroot are the same, and tests the same for
two resourceroots for the same directory but with different
include/excludes.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>eclipse</groupId>
<artifactId>maven-eclipse-plugin-test-project-3</artifactId>
<version>88.0</version>
<name>Maven</name>
<description>
Tests creating just one sourceroot when test sourceroot and
main sourceroot are the same, and tests the same for
two resourceroots for the same directory but with different
include/excludes.
</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/main/java</testSourceDirectory>
<testOutputDirectory>target/test-output-dir</testOutputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>target/main-resources</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,7 @@
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class DummyClass
{
}

View File

@ -0,0 +1,7 @@
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class DummyClass
{
}

View File

@ -0,0 +1,9 @@
<classpath>
<classpathentry kind="src" path="src-main-java"/>
<classpathentry kind="src" path="src-main-resources" output="target/main-resources"/>
<classpathentry kind="src" path="src-test-java" output="target/test-classes-dir"/>
<classpathentry kind="src" path="src-test-resources" output="target/test-classes-dir"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" rootpath="JRE_SRCROOT" path="JRE_LIB" sourcepath="JRE_SRC"/>
<classpathentry kind="var" path="M2_REPO/junit/jars/junit-2.0.jar"/>
</classpath>

View File

@ -0,0 +1,41 @@
<projectDescription>
<name>maven-eclipse-plugin-test-project-4</name>
<comment>Tests creating eclipse files in a different location</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<linkedResources>
<link>
<name>project.xml</name>
<type>1</type>
<location>${basedir}/src/test/projects/project-4/project.xml</location>
</link>
<link>
<name>src-main-java</name>
<type>2</type>
<location>${basedir}/src/test/projects/project-4/src/main/java</location>
</link>
<link>
<name>src-test-resources</name>
<type>2</type>
<location>${basedir}/src/test/projects/project-4/src/test/resources</location>
</link>
<link>
<name>src-test-java</name>
<type>2</type>
<location>${basedir}/src/test/projects/project-4/src/test/java</location>
</link>
<link>
<name>src-main-resources</name>
<type>2</type>
<location>${basedir}/src/test/projects/project-4/src/main/resources</location>
</link>
</linkedResources>
</projectDescription>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>eclipse</groupId>
<artifactId>maven-eclipse-plugin-test-project-4</artifactId>
<version>88.0</version>
<name>Maven</name>
<description>
Tests creating eclipse files in a different location
</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<testOutputDirectory>target/test-classes-dir</testOutputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>target/main-resources</targetPath>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,7 @@
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class DummyClass
{
}

View File

@ -0,0 +1,7 @@
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class DummyClass
{
}