Index: src/main/java/org/apache/maven/plugin/eclipse/writers/wtp/EclipseWtpFacetsWriter.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/writers/wtp/EclipseWtpFacetsWriter.java (revision 831059) +++ src/main/java/org/apache/maven/plugin/eclipse/writers/wtp/EclipseWtpFacetsWriter.java (working copy) @@ -1,20 +1,16 @@ /* - * 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. + * 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.writers.wtp; @@ -23,17 +19,24 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Map.Entry; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.eclipse.Constants; import org.apache.maven.plugin.eclipse.Messages; +import org.apache.maven.plugin.eclipse.writers.EclipseWriter; +import org.apache.maven.plugin.eclipse.writers.EclipseWriterConfig; import org.apache.maven.plugin.ide.IdeUtils; import org.apache.maven.plugin.ide.JeeUtils; +import org.apache.maven.plugin.logging.Log; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomWriter; /** * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it. @@ -70,6 +73,8 @@ private static final String ELT_FACETED_PROJECT = "faceted-project"; //$NON-NLS-1$ + private static final String ELT_RUNTIME = "runtime"; //$NON-NLS-1$ + /** * The .settings folder for Web Tools Project 1.x release. */ @@ -81,143 +86,223 @@ private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$ /** - * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() + * facets to be written */ - public void write() - throws MojoExecutionException - { - - // create a .settings directory (if not existing) - File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS ); - settingsDir.mkdirs(); + private Xpp3Dom facetedProject; - Writer w; + /** + * @see org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter#init(org.apache.maven.plugin.logging.Log, org.apache.maven.plugin.eclipse.writers.EclipseWriterConfig) + */ + public EclipseWriter init( Log log, EclipseWriterConfig config ) + { + super.init( log, config ); String packaging = config.getPackaging(); + Map facets = new HashMap( config.getProjectFacets() ); - // Write out facet core xml - try - { - w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" ); - } - catch ( IOException ex ) - { - throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$ - } - XMLWriter writer = new PrettyPrintXMLWriter( w ); - writeModuleTypeFacetCore( writer, packaging ); - IOUtil.close( w ); - } + facetedProject = new Xpp3Dom( ELT_FACETED_PROJECT ); - /** - * Writes out the facet info for a faceted-project based on the packaging. - * - * @param writer - * @param packaging - */ - private void writeModuleTypeFacetCore( XMLWriter writer, String packaging ) - { - writer.startElement( ELT_FACETED_PROJECT ); if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ { - writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed - writeFacetFixedElement( writer, FACET_JST_WEB ); // fixed - writeFacetInstalledElement( writer, FACET_JST_WEB, JeeUtils.resolveServletVersion( config.getProject() ) ); // installed - writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed + // fixed elements + facetedProject.addChild( createFixedFacetElement( FACET_JST_JAVA ) ); + facetedProject.addChild( createFixedFacetElement( FACET_JST_WEB ) ); + + // installed elements + if ( facets.containsKey( FACET_JST_JAVA ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, (String) facets + .get( FACET_JST_JAVA ) ) ); + facets.remove( FACET_JST_JAVA ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, IdeUtils + .resolveJavaVersion( config.getProject() ) ) ); + } + + if ( facets.containsKey( FACET_JST_WEB ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_WEB, (String) facets + .get( FACET_JST_WEB ) ) ); + facets.remove( FACET_JST_WEB ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_WEB, JeeUtils + .resolveServletVersion( config.getProject() ) ) ); + } } - else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ + else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) { - writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed - writeFacetFixedElement( writer, FACET_JST_EJB ); // fixed - writeFacetInstalledElement( writer, FACET_JST_EJB, JeeUtils.resolveEjbVersion( config.getProject() ) ); // installed - writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed + // fixed elements + facetedProject.addChild( createFixedFacetElement( FACET_JST_JAVA ) ); + facetedProject.addChild( createFixedFacetElement( FACET_JST_EJB ) ); + + // installed elements + if ( facets.containsKey( FACET_JST_JAVA ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, (String) facets + .get( FACET_JST_JAVA ) ) ); + facets.remove( FACET_JST_JAVA ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, IdeUtils + .resolveJavaVersion( config.getProject() ) ) ); + } + + if ( facets.containsKey( FACET_JST_EJB ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_EJB, (String) facets + .get( FACET_JST_EJB ) ) ); + facets.remove( FACET_JST_EJB ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_EJB, JeeUtils.resolveEjbVersion( config + .getProject() ) ) ); + } } - else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ + else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) { - if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null ) + // fixed elements + facetedProject.addChild( createFixedFacetElement( FACET_JST_EAR ) ); + + if ( facets.containsKey( FACET_JST_EAR ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_EAR, (String) facets + .get( FACET_JST_EAR ) ) ); + facets.remove( FACET_JST_EAR ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_EAR, JeeUtils.resolveJeeVersion( config + .getProject() ) ) ); + } + + if ( config.getWorkspaceConfiguration().getWebsphereVersion() != null ) { - writer.startElement( "runtime" ); - writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() ); - writer.endElement(); // runtime + facetedProject.addChild( createRuntimeElement( config.getWorkspaceConfiguration() + .getDefaultDeployServerName() ) ); - writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR, - this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed - writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR, - this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed + facetedProject.addChild( createInstalledFacetElement( FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR, config + .getWorkspaceConfiguration().getWebsphereVersion() ) ); + facetedProject.addChild( createInstalledFacetElement( FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR, config + .getWorkspaceConfiguration().getWebsphereVersion() ) ); } - writeFacetFixedElement( writer, FACET_JST_EAR ); // fixed - writeFacetInstalledElement( writer, FACET_JST_EAR, JeeUtils.resolveJeeVersion( config.getProject() ) ); // installed } else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ { - writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed - writeFacetFixedElement( writer, FACET_JST_UTILITY ); // fixed - writeFacetInstalledElement( writer, FACET_JST_UTILITY, "1.0" ); //$NON-NLS-1$ - writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed - // installed + facetedProject.addChild( createFixedFacetElement( FACET_JST_JAVA ) ); + facetedProject.addChild( createFixedFacetElement( FACET_JST_UTILITY ) ); + + // installed elements + if ( facets.containsKey( FACET_JST_JAVA ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, (String) facets + .get( FACET_JST_JAVA ) ) ); + facets.remove( FACET_JST_JAVA ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_JAVA, IdeUtils + .resolveJavaVersion( config.getProject() ) ) ); + } + + if ( facets.containsKey( FACET_JST_UTILITY ) ) + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_UTILITY, (String) facets + .get( FACET_JST_UTILITY ) ) ); + facets.remove( FACET_JST_UTILITY ); + } + else + { + facetedProject.addChild( createInstalledFacetElement( FACET_JST_UTILITY, "1.0" ) ); + } } - writeAdditionalProjectFacets( writer ); + // additional installed elements + for ( Iterator iter = facets.keySet().iterator(); iter.hasNext(); ) + { + String facet = (String) iter.next(); + String version = (String) facets.get( facet ); + facetedProject.addChild( createInstalledFacetElement( facet, version ) ); + } - writer.endElement(); // faceted-project + return this; } /** - * Writes facet fixed element with attribute facet set to the value of argument - * facetName. + * Creates and returns an XML element with the name fixed. * - * @param writer - * @param facetName + * @param name the facet name + * @return the XML element */ - private void writeFacetFixedElement( XMLWriter writer, String facetName ) + private Xpp3Dom createFixedFacetElement( String name ) { - writer.startElement( ELT_FIXED ); - writer.addAttribute( ATTR_FACET, facetName ); - writer.endElement(); + Xpp3Dom element = new Xpp3Dom( ELT_FIXED ); + element.setAttribute( ATTR_FACET, name ); + return element; } /** - * Writes a facet installed element with attribute facet set to the value of argument - * facetName, and attribute version set to the value of argument facetVersion - * . + * Creates and returns an XML element with the name installed. * - * @param writer - * @param facetName - * @param facetVersion + * @param name the facet name + * @param version the facet version + * @return the XML element */ - private void writeFacetInstalledElement( XMLWriter writer, String facetName, String facetVersion ) + private Xpp3Dom createInstalledFacetElement( String name, String version ) { - writer.startElement( ELT_INSTALLED ); - writer.addAttribute( ATTR_FACET, facetName ); - writer.addAttribute( ATTR_VERSION, facetVersion ); - writer.endElement(); + Xpp3Dom element = new Xpp3Dom( ELT_INSTALLED ); + element.setAttribute( ATTR_FACET, name ); + element.setAttribute( ATTR_VERSION, version ); + return element; } /** - * Writes out any additional project facets specified in the plugin configuration + * Creates and returns an XML element with the name runtime. * - * @param writer - * @param packaging + * @param name the runtime + * @return the XML element + */ + private Xpp3Dom createRuntimeElement( String name ) + { + Xpp3Dom element = new Xpp3Dom( ELT_RUNTIME ); + element.setAttribute( ATTR_NAME, name ); + return element; + } + + /** + * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write() */ - private void writeAdditionalProjectFacets( XMLWriter writer ) + public void write() + throws MojoExecutionException { - if ( config.getProjectFacets() == null ) + + // create a .settings directory (if not existing) + File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS ); + settingsDir.mkdirs(); + + Writer w; + + // Write out facet core xml + try { - return; + w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" ); } - - Iterator facetIterator = config.getProjectFacets().entrySet().iterator(); - while ( facetIterator.hasNext() ) + catch ( IOException ex ) { - Entry facetEntry = (Entry) facetIterator.next(); - - writer.startElement( ELT_INSTALLED ); - writer.addAttribute( ATTR_FACET, (String) facetEntry.getKey() ); - writer.addAttribute( ATTR_VERSION, (String) facetEntry.getValue() ); - writer.endElement(); // installed + throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$ } + XMLWriter writer = new PrettyPrintXMLWriter( w ); + + Xpp3DomWriter.write( writer, facetedProject ); + + IOUtil.close( w ); } } Index: src/test/java/org/apache/maven/plugin/eclipse/writers/EclipseWtpFacetsWriterTest.java =================================================================== --- src/test/java/org/apache/maven/plugin/eclipse/writers/EclipseWtpFacetsWriterTest.java (revision 0) +++ src/test/java/org/apache/maven/plugin/eclipse/writers/EclipseWtpFacetsWriterTest.java (revision 0) @@ -0,0 +1,143 @@ +/* + * 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.writers; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.maven.model.Build; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig; +import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpFacetsWriter; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.project.MavenProject; +import org.apache.maven.shared.tools.easymock.TestFileManager; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.jdom.xpath.XPath; + +/** + * Facet writer test. + * + * @author Daniel Rohe + */ +public class EclipseWtpFacetsWriterTest + extends TestCase +{ + + private TestFileManager fileManager = new TestFileManager( "EclipseWtpFacetsWriter.unitTest.", "" ); + + protected void tearDown() + throws IOException + { + fileManager.cleanUp(); + } + + private Dependency createDependency( String groupId, String artifactId, String version, String classifier, + String scope ) + { + Dependency dependency = new Dependency(); + dependency.setGroupId( groupId ); + dependency.setArtifactId( artifactId ); + dependency.setVersion( version ); + if ( scope != null ) + { + dependency.setScope( scope ); + } + if ( classifier != null ) + { + dependency.setClassifier( classifier ); + } + return dependency; + } + + public void testWriteFacetMECLIPSE449() + throws MojoExecutionException, IOException, JDOMException + { + TestEclipseWriterConfig config = new TestEclipseWriterConfig(); + + config.setWtpVersion( 1.5f ); + config.setEclipseProjectName( "test-project" ); + + File basedir = fileManager.createTempDir(); + File pom = new File( basedir, "pom.xml" ); + pom.createNewFile(); + + MavenProject project = new MavenProject(); + project.setFile( pom ); + project.getDependencies().add( createDependency( "javax.servlet", "servlet-api", "2.5", null, null ) ); + project.getModel().setBuild( new Build() ); + project.getBuildPlugins(); + + config.setProject( project ); + config.setProjectBaseDir( basedir ); + + config.setEclipseProjectDirectory( basedir ); + config.setPackaging( "war" ); + + Map facets = new HashMap(); + facets.put( "jst.web", "2.4" ); + config.setProjectFacets( facets ); + + // add an ejb3 and ejb packaged dependency + + EclipseWtpFacetsWriter lWriter = new EclipseWtpFacetsWriter(); + + Log log = new TestLog(); + + lWriter.init( log, config ); + + lWriter.write(); + + // now check extension of archivenames to be jar + SAXBuilder builder = new SAXBuilder( false ); + + Document doc = builder.build( new File( basedir, ".settings/org.eclipse.wst.common.project.facet.core.xml" ) ); + + XPath archiveNames = XPath.newInstance( "//faceted-project/installed[@facet = \"jst.web\"]" ); + + List nodes = archiveNames.selectNodes( doc ); + assertEquals( "Must be 1 installed element", 1, nodes.size() ); + for ( Iterator it = nodes.iterator(); it.hasNext(); ) + { + Element element = (Element) it.next(); + + String facet = element.getAttributeValue( "facet" ); + String version = element.getAttributeValue( "version" ); + + assertEquals( "jst.web", facet ); + assertEquals( "2.4", version ); + } + } + + private static final class TestLog + extends SystemStreamLog + { + public boolean isDebugEnabled() + { + return true; + } + } +}