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