View Javadoc
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  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          if ( Constants.PROJECT_PACKAGING_WAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
90          {
91              writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.web" ); //$NON-NLS-1$
92  
93              writer.startElement( ELT_VERSION );
94              String servletVersion;
95              if ( config.getJeeVersion() != null )
96              {
97                  servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion();
98              }
99              else
100             {
101                 servletVersion = JeeUtils.resolveServletVersion( config.getProject() );
102             }
103             writer.writeText( servletVersion );
104             writer.endElement();
105 
106             String contextRoot = config.getContextName();
107 
108             writer.startElement( ELT_PROPERTY );
109             writer.addAttribute( ATTR_NAME, "context-root" ); //$NON-NLS-1$
110             writer.addAttribute( ATTR_VALUE, contextRoot );
111             writer.endElement();
112         }
113         else if ( Constants.PROJECT_PACKAGING_EJB.equals( config.getPackaging() ) ) //$NON-NLS-1$
114         {
115             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ejb" ); //$NON-NLS-1$
116 
117             writer.startElement( ELT_VERSION );
118 
119             String ejbVersion;
120             if ( config.getJeeVersion() != null )
121             {
122                 ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion();
123             }
124             else
125             {
126                 ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() );
127             }
128             writer.writeText( ejbVersion );
129 
130             writer.endElement();
131 
132             writer.startElement( ELT_PROPERTY );
133             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
134             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
135                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
136             writer.endElement();
137 
138         }
139         else if ( Constants.PROJECT_PACKAGING_EAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
140         {
141             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.ear" ); //$NON-NLS-1$
142 
143             writer.startElement( ELT_VERSION );
144             String jeeVersion;
145             if ( config.getJeeVersion() != null )
146             {
147                 jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
148             }
149             else
150             {
151                 jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() );
152             }
153             writer.writeText( jeeVersion );
154             writer.endElement();
155         }
156         else
157         {
158             // jar
159             writer.addAttribute( ATTR_MODULE_TYPE_ID, "jst.utility" ); //$NON-NLS-1$
160 
161             writer.startElement( ELT_PROPERTY );
162             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
163             writer.addAttribute( ATTR_VALUE, "/" + //$NON-NLS-1$
164                 IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
165             writer.endElement();
166         }
167     }
168 
169     /**
170      * Adds dependency for Eclipse WTP project.
171      * 
172      * @param writer
173      * @param artifact
174      * @param localRepository
175      * @param basedir
176      * @throws MojoExecutionException
177      */
178     protected void addDependency( XMLWriter writer, IdeDependency dep, ArtifactRepository localRepository,
179                                   File basedir, String deployPath )
180         throws MojoExecutionException
181     {
182         String handle;
183         String dependentObject = null;
184         String archiveName;
185 
186         // ejb's and wars must always be toplevel
187         if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() )
188             || Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) )
189         {
190             deployPath = "/";
191         }
192 
193         if ( dep.isReferencedProject() )
194         {
195             // <dependent-module deploy-path="/WEB-INF/lib"
196             // handle="module:/resource/artifactid/artifactid">
197             // <dependency-type>uses</dependency-type>
198             // </dependent-module>
199 
200             handle = "module:/resource/" + dep.getEclipseProjectName() + "/" + dep.getEclipseProjectName(); //$NON-NLS-1$ //$NON-NLS-2$
201 
202             String archiveExtension = dep.getType();
203             if ( Constants.PROJECT_PACKAGING_EJB.equals( dep.getType() ) )
204             {
205                 dependentObject = "EjbModule_";
206                 // an EJB module is packed as a .jar file
207                 archiveExtension = Constants.PROJECT_PACKAGING_JAR;
208             }
209             else if ( Constants.PROJECT_PACKAGING_WAR.equals( dep.getType() ) )
210             {
211                 dependentObject = "WebModule_";
212             }
213             archiveName = dep.getEclipseProjectName() + "." + archiveExtension;
214         }
215         else
216         {
217             // <dependent-module deploy-path="/WEB-INF/lib"
218             // handle="module:/classpath/var/M2_REPO/cl/cl/2.1/cl-2.1.jar">
219             // <dependency-type>uses</dependency-type>
220             // </dependent-module>
221 
222             File artifactPath = dep.getFile();
223 
224             if ( artifactPath == null )
225             {
226                 log.error( Messages.getString( "EclipsePlugin.artifactpathisnull", dep.getId() ) ); //$NON-NLS-1$
227                 return;
228             }
229 
230             String fullPath = artifactPath.getPath();
231             File repoFile = new File( fullPath );
232 
233             if ( dep.isSystemScoped() )
234             {
235                 handle = "module:/classpath/lib/" //$NON-NLS-1$
236                     + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false );
237             }
238             else
239             {
240                 File localRepositoryFile = new File( localRepository.getBasedir() );
241                 String relativePath = IdeUtils.toRelativeAndFixSeparator( localRepositoryFile, repoFile, false );
242 
243                 if ( !new File( relativePath ).isAbsolute() )
244                 {
245                     handle = "module:/classpath/var/M2_REPO/" //$NON-NLS-1$
246                         + relativePath;
247                 }
248                 else
249                 {
250                     handle = "module:/classpath/lib/" //$NON-NLS-1$
251                         + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), repoFile, false );
252                 }
253             }
254             if ( Constants.PROJECT_PACKAGING_EAR.equals( this.config.getPackaging() ) && !"/".equals( deployPath ) )
255             {
256                 // This is a very ugly hack around a WTP bug! a delpoydir in the configuration file is duplicated.
257                 // a deploy dir like "lib" will be used as "lib/lib" the only workig workaround is to include a ..
258                 // in the archive name.
259                 archiveName = "../" + artifactPath.getName();
260             }
261             else
262             {
263                 archiveName = artifactPath.getName();
264             }
265         }
266 
267         writer.startElement( ELT_DEPENDENT_MODULE );
268 
269         writer.addAttribute( "archiveName", archiveName );
270 
271         writer.addAttribute( ATTR_DEPLOY_PATH, deployPath ); //$NON-NLS-1$
272         writer.addAttribute( ATTR_HANDLE, handle );
273 
274         if ( dependentObject != null && config.getWtpVersion() >= 2.0f )
275         {
276             writer.startElement( ELT_DEPENDENCY_OBJECT );
277             writer.writeText( dependentObject + System.identityHashCode( dep ) );
278             writer.endElement();
279         }
280 
281         writer.startElement( ELT_DEPENDENCY_TYPE );
282         writer.writeText( "uses" ); //$NON-NLS-1$
283         writer.endElement();
284 
285         writer.endElement();
286     }
287 
288     protected void writeWarOrEarResources( XMLWriter writer, MavenProject project, ArtifactRepository localRepository )
289         throws MojoExecutionException
290     {
291         // use /WEB-INF/lib for war projects and / or the configured defaultLibBundleDir for ear projects
292         String deployDir =
293             IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN, "defaultLibBundleDir",
294                                        "/" );
295 
296         if ( project.getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) )
297         {
298             deployDir = "/WEB-INF/lib";
299         }
300         // dependencies
301         for ( int j = 0; j < config.getDeps().length; j++ )
302         {
303             IdeDependency dep = config.getDeps()[j];
304             String type = dep.getType();
305 
306             // NB war is needed for ear projects, we suppose nobody adds a war dependency to a war/jar project
307             // exclude test and provided and system dependencies outside the project
308             if ( ( !dep.isTestDependency() && !dep.isProvided() && !dep.isSystemScopedOutsideProject( project ) )
309                 && ( Constants.PROJECT_PACKAGING_JAR.equals( type ) || Constants.PROJECT_PACKAGING_EJB.equals( type )
310                     || "ejb-client".equals( type ) || Constants.PROJECT_PACKAGING_WAR.equals( type ) ) ) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
311             {
312                 addDependency( writer, dep, localRepository, config.getProject().getBasedir(), deployDir );
313             }
314         }
315     }
316 
317 }