catch ( IOException ex )
{
throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
}
XMLWriter writer = new PrettyPrintXMLWriter( w );
writer.startElement( ELT_CLASSPATH );
String defaultOutput =
IdeUtils.toRelativeAndFixSeparator( config.getProjectBaseDir(), config.getBuildOutputDirectory(), false );
// ----------------------------------------------------------------------
// Source roots and resources
// ----------------------------------------------------------------------
// List<EclipseSourceDir>
List specialSources = new ArrayList();
// Map<String,List<EclipseSourceDir>>
Map byOutputDir = new HashMap();
for ( int j = 0; j < config.getSourceDirs().length; j++ )
{
EclipseSourceDir dir = config.getSourceDirs()[j];
// List<EclipseSourceDir>
List byOutputDirs = (List) byOutputDir.get( dir.getOutput() );
if ( byOutputDirs == null )
{
// ArrayList<EclipseSourceDir>
byOutputDir.put( dir.getOutput() == null ? defaultOutput : dir.getOutput(), byOutputDirs =
new ArrayList() );
}
byOutputDirs.add( dir );
}
for ( int j = 0; j < config.getSourceDirs().length; j++ )
{
EclipseSourceDir dir = config.getSourceDirs()[j];
log.debug( "Processing " + ( dir.isResource() ? "re" : "" ) + "source " + dir.getPath() + ": output=" +
dir.getOutput() + "; default output=" + defaultOutput );
boolean isSpecial = false;
// handle resource with nested output folders
if ( dir.isResource() )
{
// Check if the output is a subdirectory of the default output,
// and if the default output has any sources that copy there.
if ( dir.getOutput() != null // resource output dir is set
&&
!dir.getOutput().equals( defaultOutput ) // output dir is not default target/classes
&& dir.getOutput().startsWith( defaultOutput ) // ... but is nested
&& byOutputDir.get( defaultOutput ) != null // ???
&& !( (List) byOutputDir.get( defaultOutput ) ).isEmpty() // ???
)
{
// do not specify as source since the output will be nested. Instead, mark
// it as a todo, and handle it with a custom build.xml file later.
log.debug( "Marking as special to prevent output folder nesting: " + dir.getPath() + " (output=" +
dir.getOutput() + ")" );
isSpecial = true;
specialSources.add( dir );
}
}
writer.startElement( ELT_CLASSPATHENTRY );
writer.addAttribute( ATTR_KIND, "src" ); //$NON-NLS-1$
writer.addAttribute( ATTR_PATH, dir.getPath() );
if ( !isSpecial && dir.getOutput() != null && !defaultOutput.equals( dir.getOutput() ) )
{
writer.addAttribute( ATTR_OUTPUT, dir.getOutput() );
}
if ( StringUtils.isNotEmpty( dir.getInclude() ) )
{
writer.addAttribute( ATTR_INCLUDING, dir.getInclude() );
}
String excludes = dir.getExclude();
if ( dir.isResource() )
{
// automatically exclude java files: eclipse doesn't have the concept of resource directory so it will
// try to compile any java file found in maven resource dirs
excludes = StringUtils.isEmpty( excludes ) ? "**/*.java" : excludes + "|**/*.java";
}
if ( StringUtils.isNotEmpty( excludes ) )
{
writer.addAttribute( ATTR_EXCLUDING, excludes );
}
writer.endElement();
}
/* --------------- Commented out by rfeng
// handle the special sources.
if ( !specialSources.isEmpty() )
{
log.info( "Creating maven-eclipse.xml Ant file to handle resources" );
try
{
Writer buildXmlWriter =
new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
"maven-eclipse.xml" ) ), "UTF-8" );
PrettyPrintXMLWriter buildXmlPrinter = new PrettyPrintXMLWriter( buildXmlWriter );
buildXmlPrinter.startElement( "project" );
buildXmlPrinter.addAttribute( "default", "copy-resources" );
buildXmlPrinter.startElement( "target" );
buildXmlPrinter.addAttribute( "name", "init" );
// initialize filtering tokens here
buildXmlPrinter.endElement();
buildXmlPrinter.startElement( "target" );
buildXmlPrinter.addAttribute( "name", "copy-resources" );
buildXmlPrinter.addAttribute( "depends", "init" );
for ( Iterator it = specialSources.iterator(); it.hasNext(); )
{
// TODO: merge source dirs on output path+filtering to reduce
// <copy> tags for speed.
EclipseSourceDir dir = (EclipseSourceDir) it.next();
buildXmlPrinter.startElement( "copy" );
buildXmlPrinter.addAttribute( "todir", dir.getOutput() );
buildXmlPrinter.addAttribute( "filtering", "" + dir.isFiltering() );
buildXmlPrinter.startElement( "fileset" );
buildXmlPrinter.addAttribute( "dir", dir.getPath() );
if ( dir.getInclude() != null )
{
buildXmlPrinter.addAttribute( "includes", dir.getInclude() );
}
if ( dir.getExclude() != null )
{
buildXmlPrinter.addAttribute( "excludes", dir.getExclude() );
}
buildXmlPrinter.endElement();
buildXmlPrinter.endElement();
}
buildXmlPrinter.endElement();
buildXmlPrinter.endElement();
IOUtil.close( buildXmlWriter );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Cannot create " + config.getEclipseProjectDirectory() +
"/maven-eclipse.xml", e );
}
log.info( "Creating external launcher file" );
// now create the launcher
new EclipseAntExternalLaunchConfigurationWriter().init( log, config, "Maven_Ant_Builder.launch",
"maven-eclipse.xml" ).write();
// finally add it to the project writer.
config.getBuildCommands().add(
new BuildCommand(
"org.eclipse.ui.externaltools.ExternalToolBuilder",
"LaunchConfigHandle",
"<project>/" +
EclipseLaunchConfigurationWriter.FILE_DOT_EXTERNAL_TOOL_BUILDERS +
"Maven_Ant_Builder.launch" ) );
}
*/
// ----------------------------------------------------------------------
// The default output
// ----------------------------------------------------------------------
writer.startElement( ELT_CLASSPATHENTRY );
writer.addAttribute( ATTR_KIND, ATTR_OUTPUT );
writer.addAttribute( ATTR_PATH, defaultOutput );
writer.endElement();
// ----------------------------------------------------------------------
// Container classpath entries
// ----------------------------------------------------------------------
for ( Iterator it = config.getClasspathContainers().iterator(); it.hasNext(); )
{
writer.startElement( ELT_CLASSPATHENTRY );
writer.addAttribute( ATTR_KIND, "con" ); //$NON-NLS-1$
writer.addAttribute( ATTR_PATH, (String) it.next() );
writer.endElement(); // name
}
// ----------------------------------------------------------------------
// The dependencies
// ----------------------------------------------------------------------
Set addedDependencies = new HashSet();
// TODO if (..magic property equals orderDependencies..)
IdeDependency[] depsToWrite = config.getDepsOrdered();
for ( int j = 0; j < depsToWrite.length; j++ )
{
IdeDependency dep = depsToWrite[j];
if ( dep.isAddedToClasspath() )
{
String depId =
dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getClassifier() + ":" + dep.getVersion();
/* avoid duplicates in the classpath for artifacts with different types (like ejbs) */
if ( !addedDependencies.contains( depId ) )
{
addDependency( writer, dep );
addedDependencies.add( depId );
}
}
}
writer.endElement();
IOUtil.close( w );
}