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