Coverage Report - org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponentWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseWtpComponentWriter
0%
0/62
0%
0/18
2.8
 
 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  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 38  
 
 39  
 /**
 40  
  * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 41  
  * 
 42  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 43  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 44  
  * @version $Id: EclipseWtpComponentWriter.java 720443 2008-11-25 08:48:22Z rinku $
 45  
  */
 46  0
 public class EclipseWtpComponentWriter
 47  
     extends AbstractWtpResourceWriter
 48  
 {
 49  
 
 50  
     /**
 51  
      * Context root attribute.
 52  
      */
 53  
     public static final String ATTR_CONTEXT_ROOT = "context-root"; //$NON-NLS-1$
 54  
 
 55  
     /**
 56  
      * The .settings folder for Web Tools Project 1.x release.
 57  
      */
 58  
     public static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
 59  
 
 60  
     /**
 61  
      * File name where the WTP component settings will be stored for our Eclipse Project.
 62  
      * 
 63  
      * @return <code>.component</code>
 64  
      */
 65  
     protected String getComponentFileName()
 66  
     {
 67  0
         return ".component"; //$NON-NLS-1$
 68  
     }
 69  
 
 70  
     /**
 71  
      * Version number added to component configuration.
 72  
      * 
 73  
      * @return <code>1.0</code>
 74  
      */
 75  
     protected String getProjectVersion()
 76  
     {
 77  0
         return null;
 78  
     }
 79  
 
 80  
     /**
 81  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 82  
      */
 83  
     public void write()
 84  
         throws MojoExecutionException
 85  
     {
 86  
 
 87  
         // create a .settings directory (if not existing)
 88  0
         File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
 89  0
         settingsDir.mkdirs();
 90  
 
 91  
         Writer w;
 92  
         try
 93  
         {
 94  0
             w =
 95  
                 new OutputStreamWriter( new FileOutputStream( new File( settingsDir, getComponentFileName() ) ),
 96  
                                         "UTF-8" );
 97  
         }
 98  0
         catch ( IOException ex )
 99  
         {
 100  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 101  0
         }
 102  
 
 103  
         // create a .component file and write out to it
 104  0
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 105  
 
 106  0
         writeModuleTypeComponent( writer, config.getPackaging(), config.getBuildOutputDirectory(),
 107  
                                   config.getSourceDirs(), config.getLocalRepository() );
 108  
 
 109  0
         IOUtil.close( w );
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Writes out the module type settings for a Web Tools Project to a component file.
 114  
      * 
 115  
      * @param writer
 116  
      * @param packaging
 117  
      * @param buildOutputDirectory
 118  
      * @param sourceDirs
 119  
      * @param localRepository
 120  
      * @throws MojoExecutionException
 121  
      */
 122  
     private void writeModuleTypeComponent( XMLWriter writer, String packaging, File buildOutputDirectory,
 123  
                                            EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository )
 124  
         throws MojoExecutionException
 125  
     {
 126  0
         writer.startElement( ELT_PROJECT_MODULES );
 127  0
         writer.addAttribute( ATTR_MODULE_ID, "moduleCoreId" ); //$NON-NLS-1$
 128  0
         if ( getProjectVersion() != null )
 129  
         {
 130  0
             writer.addAttribute( ATTR_PROJECT_VERSION, getProjectVersion() );
 131  
         }
 132  0
         writer.startElement( ELT_WB_MODULE );
 133  
 
 134  
         // we should use the eclipse project name as the deploy name.
 135  0
         writer.addAttribute( ATTR_DEPLOY_NAME, this.config.getEclipseProjectName() );
 136  
 
 137  
         // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
 138  0
         String target = "/"; //$NON-NLS-1$
 139  
 
 140  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 141  
         {
 142  0
             target = "/WEB-INF/classes"; //$NON-NLS-1$
 143  
 
 144  0
             File warSourceDirectory =
 145  
                 new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
 146  
                                                      "warSourceDirectory", //$NON-NLS-1$
 147  
                                                      config.getProject().getBasedir() + "/src/main/webapp" ) ); //$NON-NLS-1$
 148  
 
 149  0
             writeContextRoot( writer );
 150  
 
 151  0
             writer.startElement( ELT_WB_RESOURCE );
 152  0
             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
 153  0
             writer.addAttribute( ATTR_SOURCE_PATH,
 154  
                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
 155  
                                                                      warSourceDirectory, false ) );
 156  0
             writer.endElement();
 157  
             
 158  
             // add web resources over the top of the war source directory
 159  0
             Xpp3Dom[] webResources = IdeUtils.getPluginConfigurationDom( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
 160  
                     new String[] { "webResources", "resource" } );
 161  0
             for ( int index = 0; index < webResources.length; index++ )
 162  
             {
 163  0
                 File webResourceDirectory = new File( webResources[index].getChild( "directory" ).getValue() );
 164  0
                     writer.startElement( ELT_WB_RESOURCE );
 165  0
                 writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
 166  0
                 writer.addAttribute( ATTR_SOURCE_PATH,
 167  
                                      IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
 168  
                                                      webResourceDirectory, false ) );
 169  0
                 writer.endElement();
 170  
             }
 171  
 
 172  
             // @todo is this really needed?
 173  0
             writer.startElement( ELT_PROPERTY );
 174  0
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 175  0
             writer.addAttribute( ATTR_VALUE, "/" //$NON-NLS-1$
 176  
                 + IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 177  0
             writer.endElement(); // property
 178  
 
 179  
         }
 180  0
         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 181  
         {
 182  
 
 183  0
             String defaultApplicationXML =
 184  
                 config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application";
 185  
 
 186  0
             String earSourceDirectory =
 187  
                 IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
 188  
                                            "earSourceDirectory", //$NON-NLS-1$
 189  
                                            config.getProject().getBasedir() + defaultApplicationXML ); //$NON-NLS-1$
 190  0
             writer.startElement( ELT_WB_RESOURCE );
 191  0
             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
 192  0
             writer.addAttribute( ATTR_SOURCE_PATH,
 193  
                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
 194  
                                                                      new File( earSourceDirectory ), false ) );
 195  0
             writer.endElement();
 196  
         }
 197  
 
 198  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging )
 199  
             || Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$ //$NON-NLS-2$
 200  
         {
 201  
             // write out the dependencies.
 202  0
             writeWarOrEarResources( writer, config.getProject(), localRepository );
 203  
 
 204  
         }
 205  
 
 206  0
         for ( int j = 0; j < sourceDirs.length; j++ )
 207  
         {
 208  0
             EclipseSourceDir dir = sourceDirs[j];
 209  
             // test src/resources are not added to wtpmodules
 210  0
             if ( !dir.isTest() )
 211  
             {
 212  
                 // <wb-resource deploy-path="/" source-path="/src/java" />
 213  0
                 writer.startElement( ELT_WB_RESOURCE );
 214  0
                 writer.addAttribute( ATTR_DEPLOY_PATH, target );
 215  0
                 writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() );
 216  0
                 writer.endElement();
 217  
             }
 218  
         }
 219  
 
 220  0
         writer.endElement(); // wb-module
 221  0
         writer.endElement(); // project-modules
 222  0
     }
 223  
 
 224  
     /**
 225  
      * @param writer
 226  
      */
 227  
     protected void writeContextRoot( XMLWriter writer )
 228  
     {
 229  0
         writer.startElement( ELT_PROPERTY );
 230  0
         writer.addAttribute( ATTR_CONTEXT_ROOT, config.getContextName() );
 231  0
         writer.endElement(); // property
 232  0
     }
 233  
 
 234  
 }