Index: src/test/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterTest.java =================================================================== --- src/test/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterTest.java (revision 0) +++ src/test/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterTest.java (revision 0) @@ -0,0 +1,39 @@ +package org.apache.maven.plugin.eclipse.writers.workspace; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import java.io.File; + +import junit.framework.TestCase; + +public class EclipseCodeFormatterTest + extends TestCase +{ + private static File basedir = new File( System.getProperty( "basedir", "." ) ); + + public void testLoadFormatter() + throws Exception + { + File testStyleFile = new File( basedir, "src/test/resources/projects/workspace-02/code-styles.xml" ); + + EclipseCodeFormatterProfile formatter = new EclipseCodeFormatterProfile(); + formatter.init( testStyleFile.toURL(), null ); + + assertEquals( "format-1", formatter.getProfileName() ); + + //see integration test on workspace-02 for content validation + } +} Index: src/test/java/org/apache/maven/plugin/eclipse/AbstractEclipsePluginTestCase.java =================================================================== --- src/test/java/org/apache/maven/plugin/eclipse/AbstractEclipsePluginTestCase.java (revision 554657) +++ src/test/java/org/apache/maven/plugin/eclipse/AbstractEclipsePluginTestCase.java (working copy) @@ -32,6 +32,7 @@ import junit.framework.AssertionFailedError; +import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter; import org.apache.maven.plugin.ide.IdeUtils; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.invoker.InvocationRequest; @@ -249,6 +250,81 @@ } + /** + * Execute the eclipse:configure-workspace goal on a test project and verify generated files. + * @param projectName project directory + * @throws Exception any exception generated during test + */ + protected void testWorkspace( String projectName ) + throws Exception + { + testWorkspace( projectName, new Properties(), "configure-workspace" ); + } + + /** + * Execute the eclipse:configure-workspace goal on a test project and verify generated files. + * @param projectName project directory + * @throws Exception any exception generated during test + */ + protected void testWorkspace( String projectName, String goal ) + throws Exception + { + testWorkspace( projectName, new Properties(), goal ); + } + /** + * Execute the eclipse:configure-workspace goal on a test project and verify generated files. + * @param projectName project directory + * @param properties additional properties + * @param cleanGoal TODO + * @param genGoal TODO + * @throws Exception any exception generated during test + */ + protected void testWorkspace( String projectName, Properties properties, String genGoal ) + throws Exception + { + File basedir = getOutputDirectory( projectName ); + + File pom = new File( basedir, "pom.xml" ); + + String pluginSpec = getPluginCLISpecification(); + + List goals = new ArrayList(); + + goals.add( pluginSpec + genGoal ); + + executeMaven( pom, properties, goals ); + + MavenProject project = readProject( pom ); + + String outputDirPath = IdeUtils.getPluginSetting( project, "maven-eclipse-plugin", "outputDir", null ); + File outputDir; + File projectOutputDir = basedir; + + if ( outputDirPath == null ) + { + outputDir = basedir; + } + else + { + outputDir = new File( basedir, outputDirPath ); + outputDir.mkdirs(); + projectOutputDir = new File( outputDir, project.getArtifactId() ); + } + + compareDirectoryContent( basedir, projectOutputDir, EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR + "/" ); + + } + + protected File getOutputDirectory( String projectName ) + { + return getTestFile( "target/test-classes/projects/" + projectName ); + } + + protected File getTestWorkspaceWorkDirectory( String projectName ) + { + return new File( this.getOutputDirectory( projectName ), ".metadata" ); + } + protected void executeMaven( File pom, Properties properties, List goals ) throws TestToolsException, ExecutionFailedException { Index: src/test/java/org/apache/maven/plugin/eclipse/EclipseWorkspaceTest.java =================================================================== --- src/test/java/org/apache/maven/plugin/eclipse/EclipseWorkspaceTest.java (revision 0) +++ src/test/java/org/apache/maven/plugin/eclipse/EclipseWorkspaceTest.java (revision 0) @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.maven.plugin.eclipse; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Properties; + +import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +/** + * @version $Id: $ + */ +public class EclipseWorkspaceTest + extends AbstractEclipsePluginTestCase +{ + + private static final String ECLIPSE_JDT_CORE_PREFS_PATH = EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR + + "/" + EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE; + + private static final String ECLIPSE_JDT_UI_PREFS_PATH = EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR + + "/" + EclipseWorkspaceWriter.ECLIPSE_JDT_UI_PREFS_FILE; + + protected void setUp() + throws Exception + { + super.setUp(); + } + + public void testWorkspace01() + throws Exception + { + String projectName = "workspace-01"; + + FileUtils.deleteDirectory( this.getTestWorkspaceWorkDirectory( "add-maven-repo" ) ); + testWorkspace( projectName, "add-maven-repo" ); + + this.validateM2REPOVar( projectName ); + + File eclipseJDTUIPrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_UI_PREFS_PATH ); + + assertFalse( eclipseJDTUIPrefsFile.exists() ); + } + + public void testWorkspace02() + throws Exception + { + // In this test we purposely do not include expected/.metatdata/.plugins/org.eclipse.core.runtime.settings/org.eclipse.jdt.core.prefs + // The content of that file is heavily depended on the location of the test + + String projectName = "workspace-02"; + + FileUtils.deleteDirectory( this.getTestWorkspaceWorkDirectory( projectName ) ); + testWorkspace( projectName ); + + this.validateM2REPOVar( projectName ); + + File eclipseJDTUIPrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_UI_PREFS_PATH ); + + assertTrue( eclipseJDTUIPrefsFile.exists() ); + + + } + + private void validateM2REPOVar( String projectName ) + throws Exception + { + File eclipseJDTCorePrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_CORE_PREFS_PATH ); + + assertTrue( "Test if workspace properties exists", eclipseJDTCorePrefsFile.exists() ); + + Properties props = new Properties(); + props.load( new FileInputStream( eclipseJDTCorePrefsFile ) ); + + String M2_REPO = props.getProperty( EclipseWorkspaceWriter.CLASSPATH_VARIABLE_M2_REPO ); + + assertNotNull( "Test M2_REPO has a value", M2_REPO ); + + String localRepo = PlexusTestCase.getBasedir() + "/target/test-classes/m2repo"; + + assertEquals( "Test M2_REPO value", localRepo.replace( '\\', '/' ), M2_REPO.replace( '\\', '/' ) ); + + } + +} Index: src/test/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojoTest.java =================================================================== --- src/test/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojoTest.java (revision 554657) +++ src/test/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojoTest.java (working copy) @@ -18,6 +18,7 @@ */ package org.apache.maven.plugin.eclipse; +import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.codehaus.plexus.PlexusTestCase; @@ -43,16 +44,16 @@ mojo.execute(); - File workDir = new File( mojo.getWorkspace(), AddMavenRepoMojo.DIR_ECLIPSE_CORE_RUNTIME_SETTINGS ); + File workDir = new File( mojo.getWorkspace(), EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR ); - File eclipseJDTCorePrefsFile = new File( workDir, AddMavenRepoMojo.FILE_ECLIPSE_JDT_CORE_PREFS ); + File eclipseJDTCorePrefsFile = new File( workDir, EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE ); assertTrue( "Test if workspace properties exists", eclipseJDTCorePrefsFile.exists() ); Properties props = new Properties(); props.load( new FileInputStream( eclipseJDTCorePrefsFile ) ); - String M2_REPO = props.getProperty( AddMavenRepoMojo.CLASSPATH_VARIABLE_M2_REPO ); + String M2_REPO = props.getProperty( EclipseWorkspaceWriter.CLASSPATH_VARIABLE_M2_REPO ); assertNotNull( "Test M2_REPO has a value", M2_REPO ); Index: src/test/resources/projects/workspace-01/pom.xml =================================================================== --- src/test/resources/projects/workspace-01/pom.xml (revision 0) +++ src/test/resources/projects/workspace-01/pom.xml (revision 0) @@ -0,0 +1,22 @@ + + + 4.0.0 + eclipse + maven-eclipse-plugin-test-worksapce-1 + 99.0 + Maven + pom + + + + org.apache.maven.plugins + maven-eclipse-plugin + test + + ${basedir} + + + + + Index: src/test/resources/projects/workspace-02/expected/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs =================================================================== --- src/test/resources/projects/workspace-02/expected/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs (revision 0) +++ src/test/resources/projects/workspace-02/expected/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs (revision 0) @@ -0,0 +1,3 @@ +#Mon Jul 09 15:32:24 PDT 2007 +org.eclipse.jdt.ui.formatterprofiles=\n\n\n\n\n\n\n\n\n\n\n +formatter_profile=_format-1 Index: src/test/resources/projects/workspace-02/code-styles.xml =================================================================== --- src/test/resources/projects/workspace-02/code-styles.xml (revision 0) +++ src/test/resources/projects/workspace-02/code-styles.xml (revision 0) @@ -0,0 +1,11 @@ + + + + + + + + + + + Index: src/test/resources/projects/workspace-02/pom.xml =================================================================== --- src/test/resources/projects/workspace-02/pom.xml (revision 0) +++ src/test/resources/projects/workspace-02/pom.xml (revision 0) @@ -0,0 +1,23 @@ + + + 4.0.0 + eclipse + maven-eclipse-plugin-test-worksapce-2 + 99.0 + Maven + pom + + + + org.apache.maven.plugins + maven-eclipse-plugin + test + + ${basedir} + file:///${basedir}/code-styles.xml + + + + + Index: src/main/java/org/apache/maven/plugin/eclipse/AbstractWorkspaceMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/AbstractWorkspaceMojo.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/AbstractWorkspaceMojo.java (revision 0) @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugin.eclipse; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.plugin.AbstractMojo; + +/** + * + * @requiresProject true + */ +public abstract class AbstractWorkspaceMojo + extends AbstractMojo +{ + + /** + * Directory location of the Eclipse workspace. + * + * @parameter expression="${eclipse.workspace}" + * @required + */ + private String workspace; + + /** + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + private ArtifactRepository localRepository; + + public ArtifactRepository getLocalRepository() + { + return localRepository; + } + + public void setLocalRepository( ArtifactRepository localRepository ) + { + this.localRepository = localRepository; + } + + public String getWorkspace() + { + return workspace; + } + + public void setWorkspace( String workspace ) + { + this.workspace = workspace; + } +} Index: src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseWorkspaceWriter.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseWorkspaceWriter.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseWorkspaceWriter.java (revision 0) @@ -0,0 +1,156 @@ +package org.apache.maven.plugin.eclipse.writers.workspace; + +import java.io.File; +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.plugin.MojoExecutionException; +import org.apache.maven.plugin.eclipse.Messages; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.IOUtil; + +public class EclipseWorkspaceWriter + implements WorkspaceWriter +{ + + /** + * Path under Eclipse workspace where Eclipse Plugin metadata/config is + * stored. + */ + public static final String ECLIPSE_PLUGINS_METADATA_DIR = ".metadata/.plugins"; //$NON-NLS-1$ + + /** + * Path under {@value #ECLIPSE_PLUGINS_METADATA_DIR } folder where Eclipse + * Workspace Runtime settings are stored. + */ + public static final String ECLIPSE_CORE_RUNTIME_SETTINGS_DIR = ECLIPSE_PLUGINS_METADATA_DIR + + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$ + + /** + * File that stores the Eclipse JDT Core preferences. + */ + public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$ + + /** + * Property constant under which Variable 'M2_REPO' is setup. + */ + public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$ + + /** + * File that stores the Eclipse JDT UI preferences. + */ + public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs"; //$NON-NLS-1$ + + private WorkspaceConfiguration config; + + private Log logger; + + private File workDir; + + public WorkspaceWriter init( Log logger, WorkspaceConfiguration config ) + { + this.logger = logger; + this.config = config; + + workDir = new File( config.getWorkspaceDirectory(), ECLIPSE_CORE_RUNTIME_SETTINGS_DIR ); + workDir.mkdirs(); + + return this; + } + + public void write() + throws MojoExecutionException + { + this.writeLocalRepositoryConfiguration(); + + if ( config.getCodeStylesURL() != null ) + { + this.writeCodeStyleConfiguration(); + } + } + + private void writeCodeStyleConfiguration() + throws MojoExecutionException + { + File f = new File( workDir, ECLIPSE_JDT_UI_PREFS_FILE ); + + Properties props = loadProperties( f ); + + EclipseCodeFormatterProfile codeFormatter = new EclipseCodeFormatterProfile() + .init( config.getCodeStylesURL(), config.getActiveStyleProfileName() ); + + if ( codeFormatter.getProfileName() != null ) + { + logger.info( "Set active code style profile name: " + codeFormatter.getProfileName() ); + props.setProperty( "formatter_profile", "_" + codeFormatter.getProfileName() ); + } + + props.setProperty( "org.eclipse.jdt.ui.formatterprofiles", codeFormatter.getContent() ); + + storeProperties( props, f ); + } + + private void writeLocalRepositoryConfiguration() + throws MojoExecutionException + { + File f = new File( workDir, ECLIPSE_JDT_CORE_PREFS_FILE ); + + Properties props = loadProperties( f ); + + props.put( CLASSPATH_VARIABLE_M2_REPO, config.getLocalRepository().getBasedir() ); //$NON-NLS-1$ //$NON-NLS-2$ + + storeProperties( props, f ); + } + + private static Properties loadProperties( File f ) + throws MojoExecutionException + { + Properties props = new Properties(); + + // 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$ + } + } + + return props; + } + + private static void storeProperties( Properties props, File f ) + throws MojoExecutionException + { + OutputStream os = null; + + try + { + os = new FileOutputStream( f ); + props.store( os, null ); + } + catch ( IOException ioe ) + { + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$ + f.getAbsolutePath() ) ); + } + finally + { + IOUtil.close( os ); + } + } +} Index: src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterProfile.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterProfile.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/EclipseCodeFormatterProfile.java (revision 0) @@ -0,0 +1,126 @@ +package org.apache.maven.plugin.eclipse.writers.workspace; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.net.URL; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.eclipse.Messages; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +/** + * an Eclipse code style file + * + * @author dtran + * + */ + +public class EclipseCodeFormatterProfile +{ + private static final String ELT_PROFILES = "profiles"; + + private static final String ELT_PROFILE = "profile"; + + /** + * String presentation of the formatter with EOLs are escaped so that it can + * be embedded in a property value + */ + private String content; + + private String profileName; + + public EclipseCodeFormatterProfile init( URL url, String profileName ) + throws MojoExecutionException + { + + this.profileName = profileName; + + if ( this.profileName == null ) + { + loadDefaultProfileName( url ); + } + + this.convertFormatterToString( url ); + + + return this; + } + + private void loadDefaultProfileName( URL url ) + throws MojoExecutionException + { + Reader reader = null; + try + { + reader = new InputStreamReader( url.openStream() ); + Xpp3Dom dom = Xpp3DomBuilder.build( reader ); + + Xpp3Dom[] existingProfiles = dom.getChildren( ELT_PROFILE ); + if ( existingProfiles.length != 0 ) + { + Xpp3Dom firstProfile = existingProfiles[0]; + this.profileName = firstProfile.getAttribute( "name" ); + } + } + catch ( XmlPullParserException e ) + { + throw new MojoExecutionException ( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$ + } + catch ( IOException e ) + { + throw new MojoExecutionException ( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); //$NON-NLS-1$ + } + finally + { + IOUtil.close( reader ); + } + } + + private void convertFormatterToString( URL url ) + throws MojoExecutionException + { + InputStream is = null; + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + try + { + is = url.openStream(); + + IOUtil.copy( is, os ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile", url.toString() ), e ); //$NON-NLS-1$ + } + finally + { + IOUtil.close( is ); + } + + content = os.toString(); + + + } + + public String getContent() + { + return this.content; + } + + public String getProfileName() + { + return this.profileName; + } + +} Index: src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceConfiguration.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceConfiguration.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceConfiguration.java (revision 0) @@ -0,0 +1,59 @@ +package org.apache.maven.plugin.eclipse.writers.workspace; + +import java.io.File; +import java.net.URL; + +import org.apache.maven.artifact.repository.ArtifactRepository; + + +public class WorkspaceConfiguration +{ + private File workspaceDirectory; + + private URL codeStylesURL; + + private String activeCodeStyleProfileName; + + private ArtifactRepository localRepository; + + public File getWorkspaceDirectory() + { + return this.workspaceDirectory; + } + + public void setWorkspaceDirectory( File dir ) + { + this.workspaceDirectory = dir; + } + + public URL getCodeStylesURL() + { + return this.codeStylesURL; + } + + public void setCodeStylesURL( URL url ) + { + this.codeStylesURL = url; + } + + public String getActiveStyleProfileName() + { + return this.activeCodeStyleProfileName; + } + + public void setActiveStyleProfileName( String name ) + { + this.activeCodeStyleProfileName = name; + } + + public ArtifactRepository getLocalRepository() + { + return localRepository; + } + + public void setLocalRepository( ArtifactRepository localRepository ) + { + this.localRepository = localRepository; + } + +} Index: src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceWriter.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceWriter.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/writers/workspace/WorkspaceWriter.java (revision 0) @@ -0,0 +1,41 @@ +package org.apache.maven.plugin.eclipse.writers.workspace; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; + +/** + * @author Dan T. Tran + * @version $Id:$ + */ + +public interface WorkspaceWriter +{ + /** + * Init this writer. + * @param log mojo logger. + * @param config writer configuration. + * @return the writer instance + */ + WorkspaceWriter init( Log log, WorkspaceConfiguration config ); + + /** + * Main method that should be implemented by the writer to do the work. + * @throws MojoExecutionException + */ + void write() throws MojoExecutionException; +} Index: src/main/java/org/apache/maven/plugin/eclipse/ConfigureWorkspaceMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/ConfigureWorkspaceMojo.java (revision 0) +++ src/main/java/org/apache/maven/plugin/eclipse/ConfigureWorkspaceMojo.java (revision 0) @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.maven.plugin.eclipse; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter; +import org.apache.maven.plugin.eclipse.writers.workspace.WorkspaceConfiguration; + +/** + * Configures The following Eclipse Workspace features: + * + * + * @goal configure-workspace + * @requiresProject false + */ +public class ConfigureWorkspaceMojo + extends AbstractWorkspaceMojo +{ + /** + * Point to a URL containing code styles content. + * + * @parameter expression="${eclipse.workspaceCodeStylesURL}" + * + */ + private String workspaceCodeStylesURL; + + /** + * Name of a profile in workspaceCodeStylesURL to activate. + * Default is the first profile name in the code style file in workspaceCodeStylesURL + * + * @parameter expression="${eclipse.workspaceActiveCodeStyleProfileName}" + */ + private String workspaceActiveCodeStyleProfileName; + + public void execute() + throws MojoExecutionException + { + WorkspaceConfiguration config = new WorkspaceConfiguration(); + config.setWorkspaceDirectory( new File( this.getWorkspace() ) ); + config.setLocalRepository( this.getLocalRepository() ); + + if ( this.workspaceCodeStylesURL != null ) + { + try + { + config.setCodeStylesURL( new URL( workspaceCodeStylesURL ) ); + } + catch ( MalformedURLException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + + config.setActiveStyleProfileName( workspaceActiveCodeStyleProfileName ); + + } + + new EclipseWorkspaceWriter().init( this.getLog(), config ).write(); + } + +} Index: src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java (revision 554657) +++ src/main/java/org/apache/maven/plugin/eclipse/AddMavenRepoMojo.java (working copy) @@ -19,131 +19,28 @@ package org.apache.maven.plugin.eclipse; import java.io.File; -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; +import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter; +import org.apache.maven.plugin.eclipse.writers.workspace.WorkspaceConfiguration; /** * Adds the classpath variable MAVEN_REPO to Eclipse. * * @goal add-maven-repo * @requiresProject false + * @deprecated Use configure-workspace goal instead. */ public class AddMavenRepoMojo - extends AbstractMojo + extends AbstractWorkspaceMojo { - - /** - * Path under Eclipse workspace where Eclipse Plugin metadata/config is - * stored. - */ - public static final String DIR_ECLIPSE_PLUGINS_METADATA = ".metadata/.plugins"; //$NON-NLS-1$ - - /** - * Path under {@value #DIR_ECLIPSE_PLUGINS_METADATA } folder where Eclipse - * Workspace Runtime settings are stored. - */ - public static final String DIR_ECLIPSE_CORE_RUNTIME_SETTINGS = DIR_ECLIPSE_PLUGINS_METADATA - + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$ - - /** - * File that stores the Eclipse JDT Core preferences. - */ - public static final String FILE_ECLIPSE_JDT_CORE_PREFS = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$ - - /** - * Property constant under which Variable 'M2_REPO' is setup. - */ - public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$ - - /** - * Location of the Eclipse workspace that holds your - * configuration and source. On Windows, this will be the - * workspace directory under your eclipse installation. For - * example, if you installed eclipse into c:\eclipse, the - * workspace is c:\eclipse\workspace. - * - * @parameter expression="${eclipse.workspace}" - * @required - */ - private String workspace; - - /** - * @parameter expression="${localRepository}" - * @required - * @readonly - */ - private ArtifactRepository localRepository; - public void execute() throws MojoExecutionException { + WorkspaceConfiguration config = new WorkspaceConfiguration(); + config.setWorkspaceDirectory( new File( this.getWorkspace() ) ); + config.setLocalRepository( this.getLocalRepository() ); - File workDir = new File( workspace, DIR_ECLIPSE_CORE_RUNTIME_SETTINGS ); - workDir.mkdirs(); - - Properties props = new Properties(); - - File f = new File( workDir, FILE_ECLIPSE_JDT_CORE_PREFS ); - - // 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( CLASSPATH_VARIABLE_M2_REPO, localRepository.getBasedir() ); //$NON-NLS-1$ //$NON-NLS-2$ - - try - { - OutputStream os = new FileOutputStream( f ); - props.store( os, null ); - os.close(); - } - catch ( IOException ioe ) - { - throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$ - f.getAbsolutePath() ) ); - } + new EclipseWorkspaceWriter().init( this.getLog(), config ).write(); } - public ArtifactRepository getLocalRepository() - { - return localRepository; - } - - public void setLocalRepository( ArtifactRepository localRepository ) - { - this.localRepository = localRepository; - } - - public String getWorkspace() - { - return workspace; - } - - public void setWorkspace( String workspace ) - { - this.workspace = workspace; - } } Index: src/site/apt/index.apt =================================================================== --- src/site/apt/index.apt (revision 554657) +++ src/site/apt/index.apt (working copy) @@ -18,8 +18,12 @@ * {{{add-maven-repo-mojo.html}eclipse:add-maven-repo}} is used to add the classpath variable MAVEN_REPO to Eclipse which points to your local - repository. + repository. This goal is deprecated, please use eclipse:configure-workspace instead + * {{{configure-workspace-mojo.html}eclipse:configure-workspace}} is used to add the + classpath variable MAVEN_REPO to Eclipse which points to your local repository and + optional to configure other workspace features. + * {{{clean-mojo.html}eclipse:clean}} is used to delete the files used by the Eclipse IDE. @@ -49,3 +53,6 @@ * {{{examples/provide-project-natures-and-build-commands.html}Provide Project Natures and Build Commands}} * {{{examples/additional-facets.html}Adding additional facets}} + + * {{{examples/load-code-styles.html}Load Code Style file}} + \ No newline at end of file Index: src/site/apt/examples/load-code-styles.apt =================================================================== --- src/site/apt/examples/load-code-styles.apt (revision 0) +++ src/site/apt/examples/load-code-styles.apt (revision 0) @@ -0,0 +1,48 @@ + ------ + Loading Code Styles File + ------ + Dan T. Tran + ------ + 13 June 2007 + ------ + + +Load Coding Styles into Workspace + + External coding style file can be loaded into your Eclipse workspace via a URL. + + The following example shows how set Maven code style format to a workspace + under your current directory. + ++----- + +mvn eclipse:eclipse -Declipse.workspace=. + -Declipse.workspaceCodeStyleURL=http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-eclipse-plugin/src/optional/eclipse-config/maven-styles.xml + ++----- + + or in your pom.xml: + ++----- + + [...] + + [...] + + [...] + + org.apache.maven.plugins + maven-eclipse-plugin + + ${basedir} + http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-eclipse-plugin/src/optional/eclipse-config/maven-styles.xml + + + [...] + + [...] + + [...] + ++----- +