Coverage Report - org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponentWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseWtpComponentWriter
0%
0/56
0%
0/16
2.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.plugin.eclipse.writers.wtp;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStreamWriter;
 25  
 import java.io.Writer;
 26  
 
 27  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.eclipse.Constants;
 30  
 import org.apache.maven.plugin.eclipse.EclipseSourceDir;
 31  
 import org.apache.maven.plugin.eclipse.Messages;
 32  
 import org.apache.maven.plugin.ide.IdeUtils;
 33  
 import org.apache.maven.plugin.ide.JeeUtils;
 34  
 import org.codehaus.plexus.util.IOUtil;
 35  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 36  
 import org.codehaus.plexus.util.xml.XMLWriter;
 37  
 
 38  
 /**
 39  
  * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 40  
  * 
 41  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 42  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 43  
  * @version $Id: EclipseWtpComponentWriter.java 595639 2007-11-16 12:16:25Z aheritier $
 44  
  */
 45  0
 public class EclipseWtpComponentWriter
 46  
     extends AbstractWtpResourceWriter
 47  
 {
 48  
 
 49  
     /**
 50  
      * Context root attribute.
 51  
      */
 52  
     public static final String ATTR_CONTEXT_ROOT = "context-root"; //$NON-NLS-1$
 53  
 
 54  
     /**
 55  
      * The .settings folder for Web Tools Project 1.x release.
 56  
      */
 57  
     public static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
 58  
 
 59  
     /**
 60  
      * File name where the WTP component settings will be stored for our Eclipse Project.
 61  
      * 
 62  
      * @return <code>.component</code>
 63  
      */
 64  
     protected String getComponentFileName()
 65  
     {
 66  0
         return ".component"; //$NON-NLS-1$
 67  
     }
 68  
 
 69  
     /**
 70  
      * Version number added to component configuration.
 71  
      * 
 72  
      * @return <code>1.0</code>
 73  
      */
 74  
     protected String getProjectVersion()
 75  
     {
 76  0
         return null;
 77  
     }
 78  
 
 79  
     /**
 80  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 81  
      */
 82  
     public void write()
 83  
         throws MojoExecutionException
 84  
     {
 85  
 
 86  
         // create a .settings directory (if not existing)
 87  0
         File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
 88  0
         settingsDir.mkdirs();
 89  
 
 90  
         Writer w;
 91  
         try
 92  
         {
 93  0
             w =
 94  
                 new OutputStreamWriter( new FileOutputStream( new File( settingsDir, getComponentFileName() ) ),
 95  
                                         "UTF-8" );
 96  
         }
 97  0
         catch ( IOException ex )
 98  
         {
 99  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 100  0
         }
 101  
 
 102  
         // create a .component file and write out to it
 103  0
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 104  
 
 105  0
         writeModuleTypeComponent( writer, config.getPackaging(), config.getBuildOutputDirectory(),
 106  
                                   config.getSourceDirs(), config.getLocalRepository() );
 107  
 
 108  0
         IOUtil.close( w );
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Writes out the module type settings for a Web Tools Project to a component file.
 113  
      * 
 114  
      * @param writer
 115  
      * @param packaging
 116  
      * @param buildOutputDirectory
 117  
      * @param sourceDirs
 118  
      * @param localRepository
 119  
      * @throws MojoExecutionException
 120  
      */
 121  
     private void writeModuleTypeComponent( XMLWriter writer, String packaging, File buildOutputDirectory,
 122  
                                            EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository )
 123  
         throws MojoExecutionException
 124  
     {
 125  0
         writer.startElement( ELT_PROJECT_MODULES );
 126  0
         writer.addAttribute( ATTR_MODULE_ID, "moduleCoreId" ); //$NON-NLS-1$
 127  0
         if ( getProjectVersion() != null )
 128  
         {
 129  0
             writer.addAttribute( ATTR_PROJECT_VERSION, getProjectVersion() );
 130  
         }
 131  0
         writer.startElement( ELT_WB_MODULE );
 132  
 
 133  
         // we should use the eclipse project name as the deploy name.
 134  0
         writer.addAttribute( ATTR_DEPLOY_NAME, this.config.getEclipseProjectName() );
 135  
 
 136  
         // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
 137  0
         String target = "/"; //$NON-NLS-1$
 138  
 
 139  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 140  
         {
 141  0
             target = "/WEB-INF/classes"; //$NON-NLS-1$
 142  
 
 143  0
             File warSourceDirectory =
 144  
                 new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
 145  
                                                      "warSourceDirectory", //$NON-NLS-1$
 146  
                                                      config.getProject().getBasedir() + "/src/main/webapp" ) ); //$NON-NLS-1$
 147  
 
 148  0
             writeContextRoot( writer );
 149  
 
 150  0
             writer.startElement( ELT_WB_RESOURCE );
 151  0
             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
 152  0
             writer.addAttribute( ATTR_SOURCE_PATH,
 153  
                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
 154  
                                                                      warSourceDirectory, false ) );
 155  0
             writer.endElement();
 156  
 
 157  
             // @todo is this really needed?
 158  0
             writer.startElement( ELT_PROPERTY );
 159  0
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 160  0
             writer.addAttribute( ATTR_VALUE, "/" //$NON-NLS-1$
 161  
                 +
 162  
                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 163  0
             writer.endElement(); // property
 164  
 
 165  0
         }
 166  0
         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 167  
         {
 168  
 
 169  0
             String defaultApplicationXML =
 170  
                 config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application";
 171  
 
 172  0
             String earSourceDirectory =
 173  
                 IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
 174  
                                            "earSourceDirectory", //$NON-NLS-1$
 175  
                                            config.getProject().getBasedir() + defaultApplicationXML ); //$NON-NLS-1$
 176  0
             writer.startElement( ELT_WB_RESOURCE );
 177  0
             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
 178  0
             writer.addAttribute( ATTR_SOURCE_PATH,
 179  
                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
 180  
                                                                      new File( earSourceDirectory ), false ) );
 181  0
             writer.endElement();
 182  
         }
 183  
 
 184  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ||
 185  
             Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ //$NON-NLS-2$
 186  
         {
 187  
             // write out the dependencies.
 188  0
             writeWarOrEarResources( writer, config.getProject(), localRepository );
 189  
 
 190  
         }
 191  
 
 192  0
         for ( int j = 0; j < sourceDirs.length; j++ )
 193  
         {
 194  0
             EclipseSourceDir dir = sourceDirs[j];
 195  
             // test src/resources are not added to wtpmodules
 196  0
             if ( !dir.isTest() )
 197  
             {
 198  
                 // <wb-resource deploy-path="/" source-path="/src/java" />
 199  0
                 writer.startElement( ELT_WB_RESOURCE );
 200  0
                 writer.addAttribute( ATTR_DEPLOY_PATH, target );
 201  0
                 writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() );
 202  0
                 writer.endElement();
 203  
             }
 204  
         }
 205  
 
 206  0
         writer.endElement(); // wb-module
 207  0
         writer.endElement(); // project-modules
 208  0
     }
 209  
 
 210  
     /**
 211  
      * @param writer
 212  
      */
 213  
     protected void writeContextRoot( XMLWriter writer )
 214  
     {
 215  0
         writer.startElement( ELT_PROPERTY );
 216  0
         writer.addAttribute( ATTR_CONTEXT_ROOT, config.getContextName() );
 217  0
         writer.endElement(); // property
 218  0
     }
 219  
 
 220  
 }