Coverage Report - org.apache.maven.plugin.eclipse.writers.wtp.AbstractWtpResourceWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractWtpResourceWriter
37%
30/81
40%
19/48
6.333
 
 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  
 
 23  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.eclipse.Constants;
 26  
 import org.apache.maven.plugin.eclipse.Messages;
 27  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 28  
 import org.apache.maven.plugin.ide.IdeDependency;
 29  
 import org.apache.maven.plugin.ide.IdeUtils;
 30  
 import org.apache.maven.plugin.ide.JeeUtils;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.codehaus.plexus.util.xml.XMLWriter;
 33  
 
 34  
 /**
 35  
  * Base class to hold common constants used by extending classes.
 36  
  * 
 37  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 38  
  * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
 39  
  */
 40  1
 public abstract class AbstractWtpResourceWriter
 41  
     extends AbstractEclipseWriter
 42  
 {
 43  
 
 44  
     private static final String ELT_DEPENDENCY_OBJECT = "dependent-object"; //$NON-NLS-1$
 45  
 
 46  
     private static final String ELT_DEPENDENCY_TYPE = "dependency-type"; //$NON-NLS-1$
 47  
 
 48  
     private static final String ATTR_HANDLE = "handle"; //$NON-NLS-1$
 49  
 
 50  
     private static final String ELT_DEPENDENT_MODULE = "dependent-module"; //$NON-NLS-1$
 51  
 
 52  
     protected static final String ATTR_VALUE = "value"; //$NON-NLS-1$
 53  
 
 54  
     protected static final String ATTR_NAME = "name"; //$NON-NLS-1$
 55  
 
 56  
     protected static final String ELT_PROPERTY = "property"; //$NON-NLS-1$
 57  
 
 58  
     protected static final String ELT_VERSION = "version"; //$NON-NLS-1$
 59  
 
 60  
     protected static final String ATTR_MODULE_TYPE_ID = "module-type-id"; //$NON-NLS-1$
 61  
 
 62  
     protected static final String ATTR_SOURCE_PATH = "source-path"; //$NON-NLS-1$
 63  
 
 64  
     protected static final String ATTR_DEPLOY_PATH = "deploy-path"; //$NON-NLS-1$
 65  
 
 66  
     protected static final String ELT_WB_RESOURCE = "wb-resource"; //$NON-NLS-1$
 67  
 
 68  
     protected static final String ELT_MODULE_TYPE = "module-type"; //$NON-NLS-1$
 69  
 
 70  
     protected static final String ATTR_DEPLOY_NAME = "deploy-name"; //$NON-NLS-1$
 71  
 
 72  
     protected static final String ELT_WB_MODULE = "wb-module"; //$NON-NLS-1$
 73  
 
 74  
     protected static final String ATTR_MODULE_ID = "id"; //$NON-NLS-1$
 75  
 
 76  
     protected static final String ATTR_PROJECT_VERSION = "project-version"; //$NON-NLS-1$
 77  
 
 78  
     protected static final String ELT_PROJECT_MODULES = "project-modules"; //$NON-NLS-1$
 79  
 
 80  
     /**
 81  
      * @param project
 82  
      * @param writer
 83  
      * @throws MojoExecutionException
 84  
      */
 85  
     protected void writeModuleTypeAccordingToPackaging( MavenProject project, XMLWriter writer,
 86  
                                                         File buildOutputDirectory )
 87  
         throws MojoExecutionException
 88  
     {
 89  0
         if ( Constants.PROJECT_PACKAGING_WAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
 90  
         {
 91  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.web" ); //$NON-NLS-1$
 92  
 
 93  0
             writer.startElement( ELT_VERSION );
 94  
 
 95  0
             writer.writeText( JeeUtils.resolveServletVersion( config.getProject() ) );
 96  0
             writer.endElement();
 97  
 
 98  0
             String contextRoot = config.getContextName();
 99  
 
 100  0
             writer.startElement( ELT_PROPERTY );
 101  0
             writer.addAttribute( ATTR_NAME, "context-root" ); //$NON-NLS-1$
 102  0
             writer.addAttribute( ATTR_VALUE, contextRoot );
 103  0
             writer.endElement();
 104  
         }
 105  0
         else if ( Constants.PROJECT_PACKAGING_EJB.equals( config.getPackaging() ) ) //$NON-NLS-1$
 106  
         {
 107  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ejb" ); //$NON-NLS-1$
 108  
 
 109  0
             writer.startElement( ELT_VERSION );
 110  0
             writer.writeText( JeeUtils.resolveEjbVersion( config.getProject() ) );
 111  
 
 112  0
             writer.endElement();
 113  
 
 114  0
             writer.startElement( ELT_PROPERTY );
 115  0
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 116  0
             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
 117  
                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 118  0
             writer.endElement();
 119  
 
 120  
         }
 121  0
         else if ( Constants.PROJECT_PACKAGING_EAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
 122  
         {
 123  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ear" ); //$NON-NLS-1$
 124  
 
 125  0
             writer.startElement( ELT_VERSION );
 126  0
             writer.writeText( JeeUtils.resolveJeeVersion( config.getProject() ) );
 127  0
             writer.endElement();
 128  
         }
 129  
         else
 130  
         {
 131  
             // jar
 132  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.utility" ); //$NON-NLS-1$
 133  
 
 134  0
             writer.startElement( ELT_PROPERTY );
 135  0
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 136  0
             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
 137  
                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 138  0
             writer.endElement();
 139  
         }
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Adds dependency for Eclipse WTP project.
 144  
      * 
 145  
      * @param writer
 146  
      * @param artifact
 147  
      * @param localRepository
 148  
      * @param basedir
 149  
      * @throws MojoExecutionException
 150  
      */
 151  
     protected void addDependency( XMLWriter writer, IdeDependency dep, ArtifactRepository localRepository,
 152  
                                   File basedir, String deployPath )
 153  
         throws MojoExecutionException
 154  
     {
 155  
         String handle;
 156  2
         String dependentObject = null;
 157  
         String archiveName;
 158  
 
 159  
         // ejb's and wars must always be toplevel
 160  2
         if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() )
 161  
             || Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) )
 162  
         {
 163  1
             deployPath = "/";
 164  
         }
 165  
 
 166  2
         if ( dep.isReferencedProject() )
 167  
         {
 168  
             // <dependent-module deploy-path="/WEB-INF/lib"
 169  
             // handle="module:/resource/artifactid/artifactid">
 170  
             // <dependency-type>uses</dependency-type>
 171  
             // </dependent-module>
 172  
 
 173  2
             handle = "module:/resource/" + dep.getEclipseProjectName() + "/" + dep.getEclipseProjectName(); //$NON-NLS-1$ //$NON-NLS-2$
 174  
              
 175  2
             String archiveExtension = dep.getType();
 176  2
             if ( Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) )
 177  
             {
 178  1
                 dependentObject = "EjbModule_";
 179  
                 // an EJB module is packed as a .jar file
 180  1
                 archiveExtension = Constants.PROJECT_PACKAGING_JAR;
 181  
             }
 182  1
             else if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() ) )
 183  
             {
 184  0
                 dependentObject = "WebModule_";
 185  
             }
 186  2
             archiveName = dep.getEclipseProjectName() + "." + archiveExtension;
 187  
         }
 188  
         else
 189  
         {
 190  
             // <dependent-module deploy-path="/WEB-INF/lib"
 191  
             // handle="module:/classpath/var/M2_REPO/cl/cl/2.1/cl-2.1.jar">
 192  
             // <dependency-type>uses</dependency-type>
 193  
             // </dependent-module>
 194  
 
 195  0
             File artifactPath = dep.getFile();
 196  
 
 197  0
             if ( artifactPath == null )
 198  
             {
 199  0
                 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); //$NON-NLS-1$
 200  0
                 return;
 201  
             }
 202  
 
 203  0
             String fullPath = artifactPath.getPath();
 204  0
             File repoFile = new File( fullPath );
 205  
 
 206  0
             if ( dep.isSystemScoped() )
 207  
             {
 208  0
                 handle = "module:/classpath/lib/" //$NON-NLS-1$
 209  
                     + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false );
 210  
             }
 211  
             else
 212  
             {
 213  0
                 File localRepositoryFile = new File( localRepository.getBasedir() );
 214  0
                 String relativePath = IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, repoFile, false );
 215  
 
 216  0
                 if ( !new File( relativePath ).isAbsolute() )
 217  
                 {
 218  0
                     handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$
 219  
                         + relativePath;
 220  
                 }
 221  
                 else
 222  
                 {
 223  0
                     handle = "module:/classpath/lib/" //$NON-NLS-1$
 224  
                         + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false );
 225  
                 }
 226  
             }
 227  0
             if ( Constants.PROJECT_PACKAGING_EAR.equals( this.config.getPackaging() ) && !"/".equals( deployPath ) )
 228  
             {
 229  
                 // This is a very ugly hack around a WTP bug! a delpoydir in the configuration file is duplicated.
 230  
                 // a deploy dir like "lib" will be used as "lib/lib" the only workig workaround is to include a ..
 231  
                 // in the archive name.
 232  0
                 archiveName = "../" + artifactPath.getName();
 233  
             }
 234  
             else
 235  
             {
 236  0
                 archiveName = artifactPath.getName();
 237  
             }
 238  
         }
 239  
 
 240  2
         writer.startElement( ELT_DEPENDENT_MODULE );
 241  
 
 242  2
         writer.addAttribute( "archiveName", archiveName );
 243  
 
 244  2
         writer.addAttribute( ATTR_DEPLOY_PATH, deployPath ); //$NON-NLS-1$
 245  2
         writer.addAttribute( ATTR_HANDLE, handle );
 246  
 
 247  2
         if ( dependentObject != null && config.getWtpVersion() >= 2.0f )
 248  
         {
 249  0
             writer.startElement( ELT_DEPENDENCY_OBJECT );
 250  0
             writer.writeText( dependentObject + System.identityHashCode( dep ) );
 251  0
             writer.endElement();
 252  
         }
 253  
 
 254  2
         writer.startElement( ELT_DEPENDENCY_TYPE );
 255  2
         writer.writeText( "uses" ); //$NON-NLS-1$
 256  2
         writer.endElement();
 257  
 
 258  2
         writer.endElement();
 259  2
     }
 260  
 
 261  
     protected void writeWarOrEarResources( XMLWriter writer, MavenProject project, ArtifactRepository localRepository )
 262  
         throws MojoExecutionException
 263  
     {
 264  
         // use /WEB-INF/lib for war projects and / or the configured defaultLibBundleDir for ear projects
 265  1
         String deployDir =
 266  
             IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN, "defaultLibBundleDir",
 267  
                                        "/" );
 268  
 
 269  1
         if ( project.getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) )
 270  
         {
 271  0
             deployDir = "/WEB-INF/lib";
 272  
         }
 273  
         // dependencies
 274  3
         for ( int j = 0; j < config.getDepsOrdered().length; j++ )
 275  
         {
 276  2
             IdeDependency dep = config.getDepsOrdered()[j];
 277  2
             String type = dep.getType();
 278  
 
 279  
             // NB war is needed for ear projects, we suppose nobody adds a war dependency to a war/jar project
 280  
             // exclude test and provided and system dependencies outside the project
 281  2
             if ( ( !dep.isTestDependency() && !dep.isProvided() && !dep.isSystemScopedOutsideProject( project ) )
 282  
                 && ( Constants.PROJECT_PACKAGING_JAR.equals( type ) || Constants.PROJECT_PACKAGING_EJB.equals( type )
 283  
                     || "ejb-client".equals( type ) || Constants.PROJECT_PACKAGING_WAR.equals( type ) ) ) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 284  
             {
 285  2
                 addDependency( writer, dep, localRepository, config.getProject().getBasedir(), deployDir );
 286  
             }
 287  
         }
 288  1
     }
 289  
 
 290  
 }