From e6aedd7d5d07e2a6f46de0c9a49efea7a1de7b27 Mon Sep 17 00:00:00 2001 From: Fabrizio Giustina Date: Thu, 10 Nov 2005 21:27:12 +0000 Subject: [PATCH] Fixing a couple of issues actually reported against the m1/m2 plugin: MPECLIPSE-102 Running 'maven:eclipse' turns CheckStyle off for the project. MPECLIPSE-106 Eclipse loses Sysdeo "Is Tomcat Project" setting after mvn eclipse:eclipse existing .project files are parsed and additional natures/builders are preserved. Running eclipse:clean will remove existing settings and start from scratch git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@332376 13f79535-47bb-0310-9956-ffa450edef68 --- .../eclipse/EclipseClasspathWriter.java | 8 +- .../plugin/eclipse/EclipseProjectWriter.java | 92 +++++++++++++++++-- .../maven/plugin/eclipse/messages.properties | 2 + 3 files changed, 89 insertions(+), 13 deletions(-) diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java index cbb3e55745..8b1eb7f0eb 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseClasspathWriter.java @@ -197,9 +197,11 @@ public class EclipseClasspathWriter throw new MojoExecutionException( message, e ); } - log.info( Messages.getString( "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$ - new Object[] { artifact.getArtifactId(), path } ) ); - + if ( log.isDebugEnabled() ) + { + log.debug( Messages.getString( "EclipsePlugin.artifactissystemscoped", //$NON-NLS-1$ + new Object[] { artifact.getArtifactId(), path } ) ); + } log.info( Messages.getString( "EclipseClasspathWriter.sourcesnotavailable", //$NON-NLS-1$ artifact.getArtifactId() ) ); diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseProjectWriter.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseProjectWriter.java index f218720aa1..e112d8e80a 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseProjectWriter.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/EclipseProjectWriter.java @@ -16,6 +16,15 @@ package org.apache.maven.plugin.eclipse; * limitations under the License. */ +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Resource; import org.apache.maven.plugin.MojoExecutionException; @@ -24,12 +33,9 @@ import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.IOUtil; 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; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** * Writes eclipse .project file. @@ -50,19 +56,85 @@ public class EclipseProjectWriter } protected void write( File projectBaseDir, File basedir, MavenProject project, MavenProject executedProject, - List reactorArtifacts, List projectnatures, List buildCommands ) + List reactorArtifacts, List addedProjectnatures, List addedBuildCommands ) throws MojoExecutionException { + + Set projectnatures = new LinkedHashSet(); + Set buildCommands = new LinkedHashSet(); + + File dotProject = new File( basedir, ".project" ); + + if ( dotProject.exists() ) + { + + log.info( Messages.getString( "EclipsePlugin.keepexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$ + + // parse existing file in order to keep manually-added entries + FileReader reader = null; + try + { + reader = new FileReader( dotProject ); + Xpp3Dom dom = Xpp3DomBuilder.build( reader ); + + Xpp3Dom naturesElement = dom.getChild( "natures" ); + if ( naturesElement != null ) + { + Xpp3Dom[] existingNatures = naturesElement.getChildren( "nature" ); + for ( int j = 0; j < existingNatures.length; j++ ) + { + // adds all the existing natures + projectnatures.add( existingNatures[j].getValue() ); + } + } + + Xpp3Dom buildSpec = dom.getChild( "buildSpec" ); + if ( buildSpec != null ) + { + Xpp3Dom[] existingBuildCommands = buildSpec.getChildren( "buildCommand" ); + for ( int j = 0; j < existingBuildCommands.length; j++ ) + { + Xpp3Dom buildCommandName = existingBuildCommands[j].getChild( "name" ); + if ( buildCommandName != null ) + { + buildCommands.add( buildCommandName.getValue() ); + } + } + } + } + catch ( XmlPullParserException e ) + { + log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$ + } + catch ( IOException e ) + { + log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$ + } + finally + { + IOUtil.close( reader ); + } + } + + // adds new entries after the existing ones + for ( Iterator iter = addedProjectnatures.iterator(); iter.hasNext(); ) + { + projectnatures.add( iter.next() ); + } + for ( Iterator iter = addedBuildCommands.iterator(); iter.hasNext(); ) + { + buildCommands.add( iter.next() ); + } + FileWriter w; try { - w = new FileWriter( new File( basedir, ".project" ) ); //$NON-NLS-1$ + w = new FileWriter( dotProject ); //$NON-NLS-1$ } 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 ); diff --git a/maven-plugins/maven-eclipse-plugin/src/main/resources/org/apache/maven/plugin/eclipse/messages.properties b/maven-plugins/maven-eclipse-plugin/src/main/resources/org/apache/maven/plugin/eclipse/messages.properties index f972709fa3..d766d91435 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/resources/org/apache/maven/plugin/eclipse/messages.properties +++ b/maven-plugins/maven-eclipse-plugin/src/main/resources/org/apache/maven/plugin/eclipse/messages.properties @@ -6,6 +6,8 @@ EclipsePlugin.erroropeningfile=Exception while opening file. EclipsePlugin.cantcanonicalize=Can''t canonicalize system path: {0} EclipsePlugin.cantwritetofile=Unable to write to file: {0} EclipsePlugin.cantreadfile=Unable to read file: {0} +EclipsePlugin.keepexisting=File {0} already exists.\n Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed. +EclipsePlugin.cantparseexisting=Unable to parse existing file: {0}. Settings will not be preserved. EclipsePlugin.cantresolvesources=Cannot resolve source artifact. Artifact id: {0} (Message: {1}) EclipsePlugin.errorresolvingsources=Error resolving source artifact. Artifact id: {0} (Message: {1}) EclipsePlugin.wrote=Wrote Eclipse project for "{0}" to {1}.