All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpFacetsWriter Maven / Gradle / Ivy

Go to download

The Eclipse Plugin is used to generate Eclipse IDE files (.project, .classpath and the .settings folder) from a POM.

There is a newer version: 3.2.0
Show newest version
package org.apache.maven.plugin.eclipse.writers.wtp;

/*
 * 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 java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
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.ide.IdeUtils;
import org.apache.maven.plugin.ide.JeeUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
import org.codehaus.plexus.util.xml.XMLWriter;

/**
 * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 * 
 * @author Rahul Thakur
 * @author Fabrizio Giustina
 * @version $Id$
 */
public class EclipseWtpFacetsWriter
    extends AbstractWtpResourceWriter
{

    private static final String FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR = "com.ibm.websphere.coexistence.ear";

    private static final String FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR = "com.ibm.websphere.extended.ear";

    private static final String FACET_JST_EAR = "jst.ear"; 

    private static final String FACET_JST_UTILITY = "jst.utility";

    private static final String FACET_JST_EJB = "jst.ejb";

    private static final String FACET_JST_WEB = "jst.web";

    private static final String FACET_JST_JAVA = "jst.java";

    private static final String ATTR_VERSION = "version";

    private static final String ELT_INSTALLED = "installed";

    private static final String ATTR_FACET = "facet";

    private static final String ELT_FIXED = "fixed";

    private static final String ELT_FACETED_PROJECT = "faceted-project";

    /**
     * The .settings folder for Web Tools Project 1.x release.
     */
    private static final String DIR_WTP_SETTINGS = ".settings";

    /**
     * File name where Eclipse Project's Facet configuration will be stored.
     */
    private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml";

    /**
     * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
     */
    public void write()
        throws MojoExecutionException
    {

        // create a .settings directory (if not existing)
        File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
        settingsDir.mkdirs();

        Writer w;

        String packaging = config.getPackaging();

        // 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 );
        }
        XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
        writeModuleTypeFacetCore( writer, packaging );
        IOUtil.close( w );
    }

    /**
     * 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 ) )
        {
            writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
            writeFacetFixedElement( writer, FACET_JST_WEB ); // fixed
            String servletVersion;
            if ( config.getJeeVersion() != null )
            {
                servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion();
            }
            else
            {
                servletVersion = JeeUtils.resolveServletVersion( config.getProject() );
            }
            // installed
            writeFacetInstalledElement( writer, FACET_JST_WEB, servletVersion );
            // installed
            writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); 
        }
        else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
        {
            writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
            writeFacetFixedElement( writer, FACET_JST_EJB ); // fixed

            String ejbVersion;
            if ( config.getJeeVersion() != null )
            {
                ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion();
            }
            else
            {
                ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() );
            }
            // installed
            writeFacetInstalledElement( writer, FACET_JST_EJB, ejbVersion ); 
            // installed
            writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); 
        }
        else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) 
        {
            if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null )
            {
                writer.startElement( "runtime" );
                writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() );
                writer.endElement(); // runtime

                // installed
                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() ); 

            }
            writeFacetFixedElement( writer, FACET_JST_EAR ); // fixed
            String jeeVersion;
            if ( config.getJeeVersion() != null )
            {
                jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
            }
            else
            {
                jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() );
            }
            writeFacetInstalledElement( writer, FACET_JST_EAR, jeeVersion ); // installed

        }
        else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
        {
            writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
            writeFacetFixedElement( writer, FACET_JST_UTILITY ); // fixed
            // installed
            writeFacetInstalledElement( writer, FACET_JST_UTILITY, "1.0" ); 
            // installed
            writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); 
        }

        writeAdditionalProjectFacets( writer );

        writer.endElement(); // faceted-project
    }

    /**
     * Writes facet fixed element with attribute facet set to the value of argument
     * facetName.
     * 
     * @param writer
     * @param facetName
     */
    private void writeFacetFixedElement( XMLWriter writer, String facetName )
    {
        writer.startElement( ELT_FIXED );
        writer.addAttribute( ATTR_FACET, facetName );
        writer.endElement();
    }

    /**
     * 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
     * .
     * 
     * @param writer
     * @param facetName
     * @param facetVersion
     */
    private void writeFacetInstalledElement( XMLWriter writer, String facetName, String facetVersion )
    {
        writer.startElement( ELT_INSTALLED );
        writer.addAttribute( ATTR_FACET, facetName );
        writer.addAttribute( ATTR_VERSION, facetVersion );
        writer.endElement();
    }

    /**
     * Writes out any additional project facets specified in the plugin configuration
     * 
     * @param writer
     * @param packaging
     */
    private void writeAdditionalProjectFacets( XMLWriter writer )
    {
        if ( config.getProjectFacets() == null )
        {
            return;
        }

        for ( Object o : config.getProjectFacets().entrySet() )
        {
            Entry facetEntry = (Entry) o;

            writer.startElement( ELT_INSTALLED );
            writer.addAttribute( ATTR_FACET, (String) facetEntry.getKey() );
            writer.addAttribute( ATTR_VERSION, (String) facetEntry.getValue() );
            writer.endElement(); // installed
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy