Coverage Report - org.apache.maven.plugin.eclipse.writers.AbstractWtpResourceWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractWtpResourceWriter
30% 
61% 
3.286
 
 1  
 /**
 2  
  *
 3  
  */
 4  
 package org.apache.maven.plugin.eclipse.writers;
 5  
 
 6  
 import java.io.File;
 7  
 import java.util.Iterator;
 8  
 
 9  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 10  
 import org.apache.maven.model.Dependency;
 11  
 import org.apache.maven.plugin.MojoExecutionException;
 12  
 import org.apache.maven.plugin.eclipse.Messages;
 13  
 import org.apache.maven.plugin.ide.IdeDependency;
 14  
 import org.apache.maven.plugin.ide.IdeUtils;
 15  
 import org.apache.maven.project.MavenProject;
 16  
 import org.codehaus.plexus.util.StringUtils;
 17  
 import org.codehaus.plexus.util.xml.XMLWriter;
 18  
 
 19  
 /**
 20  
  * Base class to hold common constants used by extending classes.
 21  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 22  
  * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
 23  
  */
 24  5
 public abstract class AbstractWtpResourceWriter
 25  
     extends AbstractEclipseWriter
 26  
 {
 27  
 
 28  
     private static final String ELT_DEPENDENCY_TYPE = "dependency-type"; //$NON-NLS-1$
 29  
 
 30  
     private static final String ATTR_HANDLE = "handle"; //$NON-NLS-1$
 31  
 
 32  
     private static final String ELT_DEPENDENT_MODULE = "dependent-module"; //$NON-NLS-1$
 33  
 
 34  
     protected static final String ATTR_VALUE = "value"; //$NON-NLS-1$
 35  
 
 36  
     protected static final String ATTR_NAME = "name"; //$NON-NLS-1$
 37  
 
 38  
     protected static final String ELT_PROPERTY = "property"; //$NON-NLS-1$
 39  
 
 40  
     protected static final String ELT_VERSION = "version"; //$NON-NLS-1$
 41  
 
 42  
     protected static final String ATTR_MODULE_TYPE_ID = "module-type-id"; //$NON-NLS-1$
 43  
 
 44  
     protected static final String ATTR_SOURCE_PATH = "source-path"; //$NON-NLS-1$
 45  
 
 46  
     protected static final String ATTR_DEPLOY_PATH = "deploy-path"; //$NON-NLS-1$
 47  
 
 48  
     protected static final String ELT_WB_RESOURCE = "wb-resource"; //$NON-NLS-1$
 49  
 
 50  
     protected static final String ELT_MODULE_TYPE = "module-type"; //$NON-NLS-1$
 51  
 
 52  
     protected static final String ATTR_DEPLOY_NAME = "deploy-name"; //$NON-NLS-1$
 53  
 
 54  
     protected static final String ELT_WB_MODULE = "wb-module"; //$NON-NLS-1$
 55  
 
 56  
     protected static final String ATTR_MODULE_ID = "id"; //$NON-NLS-1$
 57  
 
 58  
     protected static final String ATTR_PROJECT_VERSION = "project-version"; //$NON-NLS-1$
 59  
 
 60  
     protected static final String ELT_PROJECT_MODULES = "project-modules"; //$NON-NLS-1$
 61  
 
 62  
     protected static final String ARTIFACT_MAVEN_WAR_PLUGIN = "maven-war-plugin"; //$NON-NLS-1$
 63  
 
 64  
     /**
 65  
      * @param project
 66  
      * @param writer
 67  
      * @throws MojoExecutionException
 68  
      */
 69  
     protected void writeModuleTypeAccordingToPackaging( MavenProject project, XMLWriter writer, File buildOutputDirectory )
 70  
         throws MojoExecutionException
 71  
     {
 72  1
         if ( "war".equals( project.getPackaging() ) ) //$NON-NLS-1$
 73  
         {
 74  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.web" ); //$NON-NLS-1$
 75  
 
 76  0
             writer.startElement( ELT_VERSION );
 77  
 
 78  0
             writer.writeText( resolveServletVersion() );
 79  0
             writer.endElement();
 80  
 
 81  
             // use finalName as context root only if it has been explicitely set
 82  0
             String contextRoot = project.getArtifactId();
 83  0
             String finalName = project.getBuild().getFinalName();
 84  0
             if ( !finalName.equals( project.getArtifactId() + "-" + project.getVersion() ) ) //$NON-NLS-1$
 85  
             {
 86  0
                 contextRoot = finalName;
 87  
             }
 88  
 
 89  0
             writer.startElement( ELT_PROPERTY );
 90  0
             writer.addAttribute( ATTR_NAME, "context-root" ); //$NON-NLS-1$
 91  0
             writer.addAttribute( ATTR_VALUE, contextRoot );
 92  0
             writer.endElement();
 93  0
         }
 94  1
         else if ( "ejb".equals( config.getProject().getPackaging() ) ) //$NON-NLS-1$
 95  
         {
 96  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ejb" ); //$NON-NLS-1$
 97  
 
 98  0
             writer.startElement( ELT_VERSION );
 99  0
             writer.writeText( resolveEjbVersion() );
 100  
 
 101  0
             writer.endElement();
 102  
 
 103  0
             writer.startElement( ELT_PROPERTY );
 104  0
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 105  0
             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
 106  
                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 107  0
             writer.endElement();
 108  
 
 109  0
         }
 110  1
         else if ( "ear".equals( config.getProject().getPackaging() ) ) //$NON-NLS-1$
 111  
         {
 112  0
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ear" ); //$NON-NLS-1$
 113  
 
 114  0
             writer.startElement( ELT_VERSION );
 115  0
             writer.writeText( resolveJ2eeVersion() );
 116  0
             writer.endElement();
 117  0
         }
 118  
         else
 119  
         {
 120  
             // jar
 121  1
             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.utility" ); //$NON-NLS-1$
 122  
 
 123  1
             writer.startElement( ELT_PROPERTY );
 124  1
             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
 125  1
             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
 126  
                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
 127  1
             writer.endElement();
 128  
         }
 129  1
     }
 130  
 
 131  
     /**
 132  
      * Adds dependency for Eclipse WTP project.
 133  
      *
 134  
      * @param writer
 135  
      * @param artifact
 136  
      * @param localRepository
 137  
      * @param basedir
 138  
      * @throws MojoExecutionException
 139  
      */
 140  
     protected void addDependency( XMLWriter writer, IdeDependency dep, ArtifactRepository localRepository, File basedir )
 141  
         throws MojoExecutionException
 142  
     {
 143  
         String handle;
 144  
 
 145  0
         if ( dep.isReferencedProject() )
 146  
         {
 147  
             // <dependent-module deploy-path="/WEB-INF/lib"
 148  
             // handle="module:/resource/artifactid/artifactid">
 149  
             // <dependency-type>uses</dependency-type>
 150  
             // </dependent-module>
 151  
 
 152  0
             handle = "module:/resource/" + dep.getArtifactId() + "/" + dep.getArtifactId(); //$NON-NLS-1$ //$NON-NLS-2$
 153  0
         }
 154  
         else
 155  
         {
 156  
             // <dependent-module deploy-path="/WEB-INF/lib"
 157  
             // handle="module:/classpath/var/M2_REPO/cl/cl/2.1/cl-2.1.jar">
 158  
             // <dependency-type>uses</dependency-type>
 159  
             // </dependent-module>
 160  
 
 161  0
             File artifactPath = dep.getFile();
 162  
 
 163  0
             if ( artifactPath == null )
 164  
             {
 165  0
                 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); //$NON-NLS-1$
 166  0
                 return;
 167  
             }
 168  
 
 169  0
             String fullPath = artifactPath.getPath();
 170  0
             File repoFile = new File( fullPath );
 171  
 
 172  0
             if ( dep.isSystemScoped() )
 173  
             {
 174  0
                 handle = "module:/classpath/lib/" //$NON-NLS-1$
 175  
                     + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false );
 176  0
             }
 177  
             else
 178  
             {
 179  0
                 File localRepositoryFile = new File( localRepository.getBasedir() );
 180  
 
 181  0
                 handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$
 182  
                     + IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, repoFile, false );
 183  
             }
 184  
         }
 185  
 
 186  0
         writer.startElement( ELT_DEPENDENT_MODULE );
 187  
 
 188  0
         writer.addAttribute( ATTR_DEPLOY_PATH, "/WEB-INF/lib" ); //$NON-NLS-1$
 189  0
         writer.addAttribute( ATTR_HANDLE, handle );
 190  
 
 191  0
         writer.startElement( ELT_DEPENDENCY_TYPE );
 192  0
         writer.writeText( "uses" ); //$NON-NLS-1$
 193  0
         writer.endElement();
 194  
 
 195  0
         writer.endElement();
 196  0
     }
 197  
 
 198  
     protected void writeWarOrEarResources( XMLWriter writer, MavenProject project, ArtifactRepository localRepository )
 199  
         throws MojoExecutionException
 200  
     {
 201  
 
 202  
         // dependencies
 203  2
         for ( int j = 0; j < config.getDeps().length; j++ )
 204  
         {
 205  0
             IdeDependency dep = config.getDeps()[j];
 206  0
             String type = dep.getType();
 207  
 
 208  
             // NB war is needed for ear projects, we suppose nobody adds a war dependency to a war/jar project
 209  
             // exclude test and provided deps
 210  0
             if ( ( !dep.isTestDependency() && !dep.isProvided() )
 211  
                 && ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) || "war".equals( type ) ) ) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 212  
             {
 213  0
                 addDependency( writer, dep, localRepository, config.getProject().getBasedir() );
 214  
             }
 215  
         }
 216  2
     }
 217  
 
 218  
     protected String resolveServletVersion()
 219  
     {
 220  1
         String[] artifactNames = new String[] { "servlet-api", "servletapi", "geronimo-spec-servlet" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 221  
 
 222  1
         String version = IdeUtils.getDependencyVersion( artifactNames, config.getProject().getArtifacts(), 3 );
 223  1
         if ( version == null )
 224  
         {
 225  
             // none of the above specified matched, try geronimo-spec-j2ee
 226  1
             artifactNames = new String[] { "geronimo-spec-j2ee" }; //$NON-NLS-1$
 227  1
             version = IdeUtils.getDependencyVersion( artifactNames, config.getProject().getArtifacts(), 3 );
 228  1
             if ( version != null )
 229  
             {
 230  0
                 String j2eeMinorVersion = StringUtils.substring( version, 2, 3 );
 231  0
                 version = "2." + j2eeMinorVersion; //$NON-NLS-1$
 232  
             }
 233  
         }
 234  1
         return version == null ? "2.4" : version; //$NON-NLS-1$
 235  
     }
 236  
 
 237  
     protected String resolveEjbVersion()
 238  
     {
 239  0
         String version = null;
 240  
         // @todo this is the default, find real ejb version from dependencies
 241  
 
 242  0
         return version == null ? "2.1" : version; //$NON-NLS-1$
 243  
     }
 244  
 
 245  
     protected String resolveJ2eeVersion()
 246  
     {
 247  
         // Take a guess as to what version of J2EE they're using; assume 1.3
 248  
         // See: http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html
 249  1
         String version = "1.3";
 250  1
         for ( Iterator it = config.getProject().getDependencies().iterator(); it.hasNext(); )
 251  
         {
 252  0
             Dependency d = (Dependency) it.next();
 253  0
             if ( "javax.j2ee".equals( d.getGroupId() ) && "j2ee".equals( d.getArtifactId() ) )
 254  
             {
 255  0
                 version = d.getVersion();
 256  0
                 break;
 257  
             }
 258  0
         }
 259  1
         return version;
 260  
     }
 261  
 
 262  
     protected String resolveJavaVersion()
 263  
     {
 264  2
         String version = IdeUtils.getPluginSetting( config.getProject(), "maven-compiler-plugin", "target", null ); //$NON-NLS-1$ //$NON-NLS-2$
 265  2
         if ( version == null )
 266  
         {
 267  2
             IdeUtils.getPluginSetting( config.getProject(), "maven-compiler-plugin", "source", null ); //$NON-NLS-1$ //$NON-NLS-2$
 268  
         }
 269  
 
 270  2
         if ( "1.5".equals( version ) || "5".equals( version ) ) //$NON-NLS-1$ //$NON-NLS-2$
 271  
         {
 272  0
             version = "5.0";// see MECLIPSE-47 eclipse only accept 5.0 as a valid version //$NON-NLS-1$
 273  
         }
 274  
 
 275  2
         return version == null ? "1.4" : version; //$NON-NLS-1$
 276  
     }
 277  
 
 278  
 }