1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
46  
47  
48  
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  
100 
101 
102     @Parameter
103     private String warContextRoot;
104 
105     
106 
107 
108 
109 
110 
111     @Parameter( property = "generatedResourceDirName", defaultValue = "target/generated-resources/rad6" )
112     private String generatedResourceDirName;
113 
114     
115 
116 
117     public String getWarContextRoot()
118     {
119         return warContextRoot;
120     }
121 
122     
123 
124 
125     public void setWarContextRoot( String warContextRoot )
126     {
127         this.warContextRoot = warContextRoot;
128     }
129 
130     
131 
132 
133 
134 
135 
136 
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 
158 
159 
160 
161 
162     private void addManifestResource( EclipseWriterConfig config )
163         throws MojoExecutionException
164     {
165         if ( isJavaProject() )
166         {
167             
168             
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 
202 
203 
204 
205 
206 
207 
208     private static String getWebContentBaseDirectory( EclipseWriterConfig config )
209         throws MojoExecutionException
210     {
211         
212         File warSourceDirectory =
213             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
214                                                  "warSourceDirectory", "src/main/webapp" ) );
215         
216         String webContentDir =
217             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
218 
219         
220         String result = config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;
221 
222         return result;
223     }
224 
225     
226 
227 
228 
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 
271 
272 
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 
304 
305 
306 
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 
336 
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 
352 
353     public String getProjectNameForArifact( Artifact artifact )
354     {
355         return artifact.getArtifactId();
356     }
357 }