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