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