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;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.model.Resource;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.eclipse.writers.EclipseWriterConfig;
28  import org.apache.maven.plugin.eclipse.writers.rad.RadApplicationXMLWriter;
29  import org.apache.maven.plugin.eclipse.writers.rad.RadEjbClasspathWriter;
30  import org.apache.maven.plugin.eclipse.writers.rad.RadJ2EEWriter;
31  import org.apache.maven.plugin.eclipse.writers.rad.RadLibCopier;
32  import org.apache.maven.plugin.eclipse.writers.rad.RadManifestWriter;
33  import org.apache.maven.plugin.eclipse.writers.rad.RadWebSettingsWriter;
34  import org.apache.maven.plugin.eclipse.writers.rad.RadWebsiteConfigWriter;
35  import org.apache.maven.plugin.ide.IdeDependency;
36  import org.apache.maven.plugin.ide.IdeUtils;
37  import org.apache.maven.plugin.ide.JeeUtils;
38  import org.apache.maven.plugins.annotations.Execute;
39  import org.apache.maven.plugins.annotations.LifecyclePhase;
40  import org.apache.maven.plugins.annotations.Mojo;
41  import org.apache.maven.plugins.annotations.Parameter;
42  import org.apache.maven.project.MavenProject;
43  
44  /**
45   * Generates the rad-6 configuration files.
46   *
47   * @author Richard van Nieuwenhoven (patch submission)
48   * @author jdcasey
49   */
50  @Mojo( name = "rad" )
51  @Execute( phase = LifecyclePhase.GENERATE_RESOURCES )
52  public class RadPlugin
53      extends EclipsePlugin
54  {
55  
56      private static final String COM_IBM_ETOOLS_J2EE_UI_LIB_DIR_BUILDER = "com.ibm.etools.j2ee.ui.LibDirBuilder";
57  
58      private static final String COM_IBM_ETOOLS_SITEEDIT_SITE_NAV_BUILDER = "com.ibm.etools.siteedit.SiteNavBuilder";
59  
60      private static final String COM_IBM_ETOOLS_SITEEDIT_SITE_UPDATE_BUILDER =
61          "com.ibm.etools.siteedit.SiteUpdateBuilder";
62  
63      private static final String COM_IBM_ETOOLS_SITEEDIT_WEB_SITE_NATURE = "com.ibm.etools.siteedit.WebSiteNature";
64  
65      private static final String COM_IBM_ETOOLS_VALIDATION_VALIDATIONBUILDER =
66          "com.ibm.etools.validation.validationbuilder";
67  
68      private static final String COM_IBM_ETOOLS_WEBPAGE_TEMPLATE_TEMPLATEBUILDER =
69          "com.ibm.etools.webpage.template.templatebuilder";
70  
71      private static final String COM_IBM_ETOOLS_WEBPAGE_TEMPLATE_TEMPLATENATURE =
72          "com.ibm.etools.webpage.template.templatenature";
73  
74      private static final String COM_IBM_ETOOLS_WEBTOOLS_ADDITIONS_JSPCOMPILATIONBUILDER =
75          "com.ibm.etools.webtools.additions.jspcompilationbuilder";
76  
77      private static final String COM_IBM_ETOOLS_WEBTOOLS_ADDITIONS_LINKSBUILDER =
78          "com.ibm.etools.webtools.additions.linksbuilder";
79  
80      private static final String COM_IBM_SSE_MODEL_STRUCTUREDBUILDER = "com.ibm.sse.model.structuredbuilder";
81  
82      private static final String COM_IBM_WTP_EJB_EJBNATURE = "com.ibm.wtp.ejb.EJBNature";
83  
84      private static final String COM_IBM_WTP_J2EE_EARNATURE = "com.ibm.wtp.j2ee.EARNature";
85  
86      private static final String COM_IBM_WTP_J2EE_LIB_COPY_BUILDER = "com.ibm.wtp.j2ee.LibCopyBuilder";
87  
88      private static final String COM_IBM_WTP_MIGRATION_MIGRATION_BUILDER = "com.ibm.wtp.migration.MigrationBuilder";
89  
90      private static final String COM_IBM_WTP_WEB_WEB_NATURE = "com.ibm.wtp.web.WebNature";
91  
92      private static final String NO_GENERATED_RESOURCE_DIRNAME = "none";
93  
94      private static final String ORG_ECLIPSE_JDT_CORE_JAVABUILDER = "org.eclipse.jdt.core.javabuilder";
95  
96      private static final String ORG_ECLIPSE_JDT_CORE_JAVANATURE = "org.eclipse.jdt.core.javanature";
97  
98      /**
99       * The context root of the webapplication. This parameter is only used when the current project is a war project,
100      * else it will be ignored.
101      */
102     @Parameter
103     private String warContextRoot;
104 
105     /**
106      * Use this to specify a different generated resources folder than target/generated-resources/rad6. Set to "none" to
107      * skip this folder generation.
108      *
109      * @since 2.4
110      */
111     @Parameter( property = "generatedResourceDirName", defaultValue = "target/generated-resources/rad6" )
112     private String generatedResourceDirName;
113 
114     /**
115      * @return Returns the warContextRoot.
116      */
117     public String getWarContextRoot()
118     {
119         return warContextRoot;
120     }
121 
122     /**
123      * @param warContextRoot The warContextRoot to set.
124      */
125     public void setWarContextRoot( String warContextRoot )
126     {
127         this.warContextRoot = warContextRoot;
128     }
129 
130     /**
131      * write all rad6 configuration files. <br/>
132      * <b> NOTE: This could change the config! </b>
133      * 
134      * @see EclipsePlugin#writeConfiguration()
135      * @param deps resolved dependencies to handle
136      * @throws MojoExecutionException if the config files could not be written.
137      */
138     protected void writeConfigurationExtras( EclipseWriterConfig config )
139         throws MojoExecutionException
140     {
141         super.writeConfigurationExtras( config );
142 
143         new RadJ2EEWriter().init( getLog(), config ).write();
144 
145         new RadWebSettingsWriter( this.warContextRoot ).init( getLog(), config ).write();
146 
147         new RadWebsiteConfigWriter().init( getLog(), config ).write();
148 
149         new RadApplicationXMLWriter().init( getLog(), config ).write();
150 
151         new RadLibCopier().init( getLog(), config ).write();
152 
153         new RadEjbClasspathWriter().init( getLog(), config ).write();
154     }
155 
156     /**
157      * make room for a Manifest file. use a generated resource for JARS and for WARS use the manifest in the
158      * webapp/meta-inf directory.
159      * 
160      * @throws MojoExecutionException
161      */
162     private void addManifestResource( EclipseWriterConfig config )
163         throws MojoExecutionException
164     {
165         if ( isJavaProject() )
166         {
167             // special case must be done first because it can add stuff to the classpath that will be
168             // written by the superclass
169             new RadManifestWriter().init( getLog(), config ).write();
170         }
171 
172         if ( isJavaProject() && !Constants.PROJECT_PACKAGING_EAR.equals( packaging )
173             && !Constants.PROJECT_PACKAGING_WAR.equals( packaging )
174             && !Constants.PROJECT_PACKAGING_EJB.equals( packaging )
175             && !NO_GENERATED_RESOURCE_DIRNAME.equals( this.generatedResourceDirName ) )
176         {
177 
178             String generatedResourceDir =
179                 this.project.getBasedir().getAbsolutePath() + File.separatorChar + this.generatedResourceDirName;
180 
181             String metainfDir = generatedResourceDir + File.separatorChar + "META-INF";
182 
183             new File( metainfDir ).mkdirs();
184 
185             final Resource resource = new Resource();
186 
187             getLog().debug( "Adding " + this.generatedResourceDirName + " to resources" );
188 
189             resource.setDirectory( generatedResourceDir );
190 
191             this.executedProject.addResource( resource );
192         }
193 
194         if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
195         {
196             new File( getWebContentBaseDirectory( config ) + File.separatorChar + "META-INF" ).mkdirs();
197         }
198     }
199 
200     /**
201      * Returns absolute path to the web content directory based on configuration of the war plugin or default one
202      * otherwise.
203      * 
204      * @param project
205      * @return absolute directory path as String
206      * @throws MojoExecutionException
207      */
208     private static String getWebContentBaseDirectory( EclipseWriterConfig config )
209         throws MojoExecutionException
210     {
211         // getting true location of web source dir from config
212         File warSourceDirectory =
213             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
214                                                  "warSourceDirectory", "src/main/webapp" ) );
215         // getting real and correct path to the web source dir
216         String webContentDir =
217             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
218 
219         // getting the path to meta-inf base dir
220         String result = config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;
221 
222         return result;
223     }
224 
225     /**
226      * overwite the default builders with the builders required by RAD6.
227      * 
228      * @param packaging packaging-type (jar,war,ejb,ear)
229      */
230     protected void fillDefaultBuilders( String packaging )
231     {
232         super.fillDefaultBuilders( packaging );
233 
234         ArrayList buildcommands = new ArrayList();
235         if ( Constants.PROJECT_PACKAGING_EAR.equals( packaging ) )
236         {
237             buildcommands.add( COM_IBM_ETOOLS_VALIDATION_VALIDATIONBUILDER );
238             buildcommands.add( COM_IBM_SSE_MODEL_STRUCTUREDBUILDER );
239         }
240         else if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
241         {
242             buildcommands.add( COM_IBM_WTP_MIGRATION_MIGRATION_BUILDER );
243             buildcommands.add( ORG_ECLIPSE_JDT_CORE_JAVABUILDER );
244             buildcommands.add( COM_IBM_ETOOLS_J2EE_UI_LIB_DIR_BUILDER );
245             buildcommands.add( COM_IBM_ETOOLS_WEBTOOLS_ADDITIONS_LINKSBUILDER );
246             buildcommands.add( COM_IBM_ETOOLS_WEBPAGE_TEMPLATE_TEMPLATEBUILDER );
247             buildcommands.add( COM_IBM_ETOOLS_SITEEDIT_SITE_NAV_BUILDER );
248             buildcommands.add( COM_IBM_ETOOLS_SITEEDIT_SITE_UPDATE_BUILDER );
249             buildcommands.add( COM_IBM_ETOOLS_VALIDATION_VALIDATIONBUILDER );
250             buildcommands.add( COM_IBM_WTP_J2EE_LIB_COPY_BUILDER );
251             buildcommands.add( COM_IBM_ETOOLS_WEBTOOLS_ADDITIONS_JSPCOMPILATIONBUILDER );
252             buildcommands.add( COM_IBM_SSE_MODEL_STRUCTUREDBUILDER );
253         }
254         else if ( Constants.PROJECT_PACKAGING_EJB.equals( packaging ) )
255         {
256             buildcommands.add( ORG_ECLIPSE_JDT_CORE_JAVABUILDER );
257             buildcommands.add( COM_IBM_ETOOLS_VALIDATION_VALIDATIONBUILDER );
258             buildcommands.add( COM_IBM_WTP_J2EE_LIB_COPY_BUILDER );
259             buildcommands.add( COM_IBM_SSE_MODEL_STRUCTUREDBUILDER );
260         }
261         else if ( isJavaProject() )
262         {
263             buildcommands.add( ORG_ECLIPSE_JDT_CORE_JAVABUILDER );
264             buildcommands.add( COM_IBM_SSE_MODEL_STRUCTUREDBUILDER );
265         }
266         setBuildcommands( buildcommands );
267     }
268 
269     /**
270      * overwite the default natures with the natures required by RAD6.
271      * 
272      * @param packaging packaging-type (jar,war,ejb,ear)
273      */
274     protected void fillDefaultNatures( String packaging )
275     {
276         super.fillDefaultNatures( packaging );
277 
278         ArrayList projectnatures = new ArrayList();
279         if ( Constants.PROJECT_PACKAGING_EAR.equals( packaging ) )
280         {
281             projectnatures.add( COM_IBM_WTP_J2EE_EARNATURE );
282         }
283         else if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
284         {
285             projectnatures.add( COM_IBM_WTP_WEB_WEB_NATURE );
286             projectnatures.add( ORG_ECLIPSE_JDT_CORE_JAVANATURE );
287             projectnatures.add( COM_IBM_ETOOLS_SITEEDIT_WEB_SITE_NATURE );
288             projectnatures.add( COM_IBM_ETOOLS_WEBPAGE_TEMPLATE_TEMPLATENATURE );
289         }
290         else if ( Constants.PROJECT_PACKAGING_EJB.equals( packaging ) )
291         {
292             projectnatures.add( COM_IBM_WTP_EJB_EJBNATURE );
293             projectnatures.add( ORG_ECLIPSE_JDT_CORE_JAVANATURE );
294         }
295         else if ( isJavaProject() )
296         {
297             projectnatures.add( ORG_ECLIPSE_JDT_CORE_JAVANATURE );
298         }
299         setProjectnatures( projectnatures );
300     }
301 
302     /**
303      * Utility method that locates a project producing the given artifact.
304      * 
305      * @param artifact the artifact a project should produce.
306      * @return <code>true</code> if the artifact is produced by a reactor projectart.
307      */
308     protected boolean isAvailableAsAReactorProject( Artifact artifact )
309     {
310         if ( this.reactorProjects != null
311             && ( Constants.PROJECT_PACKAGING_JAR.equals( artifact.getType() )
312                 || Constants.PROJECT_PACKAGING_EJB.equals( artifact.getType() ) || Constants.PROJECT_PACKAGING_WAR.equals( artifact.getType() ) ) )
313         {
314             for (Object reactorProject1 : this.reactorProjects) {
315                 MavenProject reactorProject = (MavenProject) reactorProject1;
316 
317                 if (reactorProject.getGroupId().equals(artifact.getGroupId())
318                         && reactorProject.getArtifactId().equals(artifact.getArtifactId())) {
319                     if (reactorProject.getVersion().equals(artifact.getVersion())) {
320                         return true;
321                     } else {
322                         getLog().info(
323                                 "Artifact "
324                                         + artifact.getId()
325                                         + " already available as a reactor project, but with different version. Expected: "
326                                         + artifact.getVersion() + ", found: " + reactorProject.getVersion());
327                     }
328                 }
329             }
330         }
331         return false;
332     }
333 
334     /**
335      * WARNING: The manifest resources added here will not have the benefit of the dependencies of the project, since
336      * that's not provided in the setup() apis...
337      */
338     protected void setupExtras()
339         throws MojoExecutionException
340     {
341         super.setupExtras();
342 
343         IdeDependency[] deps = doDependencyResolution();
344 
345         EclipseWriterConfig config = createEclipseWriterConfig( deps );
346 
347         addManifestResource( config );
348     }
349 
350     /**
351      * {@inheritDoc}
352      */
353     public String getProjectNameForArifact( Artifact artifact )
354     {
355         return artifact.getArtifactId();
356     }
357 }