diff --git a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java index 591d05ea3f..ed48cbd7f8 100644 --- a/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java +++ b/maven-plugins/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java @@ -16,10 +16,14 @@ package org.apache.maven.plugin.eclipse; * limitations under the License. */ - import java.io.File; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.util.Properties; + import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -29,7 +33,8 @@ import org.apache.maven.plugin.MojoExecutionException; * * @goal add-maven-repo */ -public class AddMavenRepoMojo extends AbstractMojo +public class AddMavenRepoMojo + extends AbstractMojo { /** * Location of the Eclipse workspace that holds your configuration and source. @@ -42,7 +47,7 @@ public class AddMavenRepoMojo extends AbstractMojo * @required */ private String workspace; - + /** * @parameter expression="${localRepository}" * @required @@ -50,28 +55,48 @@ public class AddMavenRepoMojo extends AbstractMojo */ private ArtifactRepository localRepository; - public void execute() throws MojoExecutionException + public void execute() + throws MojoExecutionException { - - File workDir = new File( workspace, ".metadata/.plugins/org.eclipse.core.runtime/.settings" ); - + + File workDir = new File( workspace, ".metadata/.plugins/org.eclipse.core.runtime/.settings" ); //$NON-NLS-1$ workDir.mkdirs(); - - File f = new File( workDir.getAbsolutePath(), "org.eclipse.jdt.core.prefs" ); - + + Properties props = new Properties(); + + File f = new File( workDir.getAbsolutePath(), "org.eclipse.jdt.core.prefs" ); //$NON-NLS-1$ + + // preserve old settings + if ( f.exists() ) + { + try + { + props.load( new FileInputStream( f ) ); + } + catch ( FileNotFoundException e ) + { + throw new MojoExecutionException( Messages + .getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$ + } + catch ( IOException e ) + { + throw new MojoExecutionException( Messages + .getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$ + } + } + + props.put( "\norg.eclipse.jdt.core.classpathVariable.M2_REPO", localRepository.getBasedir() ); //$NON-NLS-1$ + try { - FileWriter fWriter = new FileWriter( f ); - - fWriter.write( "\norg.eclipse.jdt.core.classpathVariable.M2_REPO=" + localRepository.getBasedir() ); - - fWriter.flush(); - - fWriter.close(); + OutputStream os = new FileOutputStream( f ); + props.store( os, null ); + os.close(); } - catch( IOException ioe ) + catch ( IOException ioe ) { - throw new MojoExecutionException( "Unable to write to file: " + f.getAbsolutePath() ); + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$ + f.getAbsolutePath() ) ); } } } 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 93d8a917a2..a95c7690cf 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 @@ -4,6 +4,8 @@ EclipsePlugin.notadir=Not a directory: "{0}" EclipsePlugin.cantcreatedir=Can''t create directory "{0}" 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.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}.