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  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.eclipse.writers.EclipseWriterConfig;
29  import org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseHibernateWriter;
30  import org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseMetadataWriter;
31  import org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseSpringBeansWriter;
32  import org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseStrutsDataWriter;
33  import org.apache.maven.plugin.ide.IdeUtils;
34  import org.apache.maven.plugin.ide.JeeUtils;
35  import org.apache.maven.plugins.annotations.Execute;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  
40  /**
41   * Generates MyEclipse configuration files
42   *
43   * @author <a href="mailto:olivier.jacob@gmail.com">Olivier Jacob</a>
44   * @since 2.5
45   */
46  @Mojo( name = "myeclipse" )
47  @Execute( phase = LifecyclePhase.GENERATE_RESOURCES )
48  public class MyEclipsePlugin
49      extends EclipsePlugin
50  {
51      /* MyEclipse project natures */
52      private static final String MYECLIPSE_EAR_NATURE = "com.genuitec.eclipse.j2eedt.core.earnature";
53  
54      private static final String MYECLIPSE_WEB_NATURE = "com.genuitec.eclipse.j2eedt.core.webnature";
55  
56      private static final String MYECLISPE_SPRING_NATURE = "com.genuitec.eclipse.springframework.springnature";
57  
58      private static final String MYECLIPSE_STRUTS_NATURE =
59          "com.genuitec.eclipse.cross.easystruts.eclipse.easystrutsnature";
60  
61      private static final String MYECLIPSE_HIBERNATE_NATURE = "com.genuitec.eclipse.hibernate.hibernatenature";
62  
63      /* MyEclipse builders */
64      private static final String MYECLIPSE_DEPLOYMENT_DESCRIPTOR_VALIDATOR_BUILDER =
65          "com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator";
66  
67      private static final String MYECLIPSE_WEB_CLASSPATH_BUILDER =
68          "com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder";
69  
70      private static final String MYECLIPSE_J2EE_PROJECT_VALIDATOR_BUILDER =
71          "com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator";
72  
73      private static final String MYECLIPSE_SPRING_BUILDER = "com.genuitec.eclipse.springframework.springbuilder";
74  
75      private static final String MYECLIPSE_HIBERNATE_BUILDER = "com.genuitec.eclipse.hibernate.HibernateBuilder";
76  
77      private static final String MYECLIPSE_J2EE_14_CLASSPATH_CONTAINER =
78          "com.genuitec.eclipse.j2eedt.core.J2EE14_CONTAINER";
79  
80      private static final String MYECLIPSE_J2EE_13_CLASSPATH_CONTAINER =
81          "com.genuitec.eclipse.j2eedt.core.J2EE13_CONTAINER";
82  
83      private static final String MYECLIPSE_DEFAULT_HIBERNATE_CFG_XML = "src/main/resources/applicationContext.xml";
84  
85      /**
86       * Spring configuration placeholder
87       * <p/>
88       * 
89       * <pre>
90       *   &lt;spring&gt;
91       *     &lt;version&gt;1.0/2.0&lt;/version&gt;
92       *     &lt;file-pattern&gt;applicationContext-*.xml&lt;/file-pattern&gt;
93       *     &lt;basedir&gt;src/main/resources&lt;/basedir&gt;
94       *   &lt;/spring&gt;
95       * </pre>
96       */
97      @Parameter
98      private Map spring;
99  
100     /**
101      * Hibernate configuration placeholder
102      * <p/>
103      * 
104      * <pre>
105      *   &lt;hibernate&gt;
106      *     &lt;config-file&gt;src/main/resources/applicationContext-persistence.xml&lt;/config-file&gt;
107      *     &lt;session-factory-id&gt;mySessionFactory&lt;/session-factory-id&gt;
108      *   &lt;/hibernate&gt;
109      * </pre>
110      */
111     @Parameter
112     private Map hibernate;
113 
114     /**
115      * Allow declaration of struts properties for MyEclipse
116      * <p/>
117      * 
118      * <pre>
119      *   &lt;struts&gt;
120      *     &lt;version&gt;1.2.9&lt;/version&gt;
121      *     &lt;servlet-name&gt;action&lt;/servlet-name&gt;
122      *     &lt;pattern&gt;*.do&lt;/pattern&gt;
123      *     &lt;base-package&gt;1.2.9&lt;/base-package&gt;
124      *   &lt;/struts&gt;
125      * </pre>
126      */
127     @Parameter
128     private Map struts;
129 
130     /**
131      * {@inheritDoc}
132      */
133     protected void writeConfigurationExtras( EclipseWriterConfig config )
134         throws MojoExecutionException
135     {
136         super.writeConfigurationExtras( config );
137         if ( isJavaProject() )
138         {
139             // If the project is a Web Project, make it compile in WEB-INF/classes
140             if ( Constants.PROJECT_PACKAGING_WAR.equals( project.getPackaging() ) )
141             {
142                 String warSourceDirectory =
143                     IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
144                                                "warSourceDirectory",//$NON-NLS-1$
145                                                "/src/main/webapp" ); //$NON-NLS-1$
146 
147                 EclipseSourceDir[] sourceDirs = config.getSourceDirs();
148                 for (EclipseSourceDir sourceDir : sourceDirs) {
149                     if (!sourceDir.isTest()) {
150                         sourceDir.setOutput(warSourceDirectory + "/WEB-INF/classes");
151                     }
152                 }
153             }
154         }
155 
156         // the MyEclipse part ...
157 
158         new MyEclipseMetadataWriter().init( getLog(), config ).write();
159 
160         if ( getStruts() != null )
161         {
162             new MyEclipseStrutsDataWriter( getStruts() ).init( getLog(), config ).write();
163         }
164         if ( getSpring() != null )
165         {
166             new MyEclipseSpringBeansWriter( getSpring() ).init( getLog(), config ).write();
167         }
168         if ( getHibernate() != null )
169         {
170             new MyEclipseHibernateWriter( getHibernate() ).init( getLog(), config ).write();
171         }
172     }
173 
174     /**
175      * Override the default builders with the builders used by MyEclipse
176      * 
177      * @param packaging packaging-type (jar,war,ejb,ear)
178      */
179     protected void fillDefaultBuilders( String packaging )
180     {
181         List commands = new ArrayList();
182 
183         super.fillDefaultBuilders( packaging );
184 
185         if ( Constants.PROJECT_PACKAGING_EAR.equals( packaging ) )
186         {
187             if ( getLog().isDebugEnabled() )
188             {
189                 getLog().debug( "EAR packaging does not need specific builders" );
190             }
191         }
192         else if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
193         {
194             commands.add( MYECLIPSE_DEPLOYMENT_DESCRIPTOR_VALIDATOR_BUILDER );
195             commands.add( MYECLIPSE_J2EE_PROJECT_VALIDATOR_BUILDER );
196             commands.add( MYECLIPSE_WEB_CLASSPATH_BUILDER );
197 
198             // WST Validation Builder : may be added by super.fillDefaultBuilders so check before adding it
199             if ( !getBuildcommands().contains( new BuildCommand( BUILDER_WST_VALIDATION ) ) )
200             {
201                 commands.add( BUILDER_WST_VALIDATION );
202             }
203         }
204         else if ( Constants.PROJECT_PACKAGING_EJB.equals( packaging ) )
205         {
206             if ( getLog().isInfoEnabled() )
207             {
208                 getLog().info( "EJB packaging is not implemented yet" );
209             }
210         }
211         else if ( isJavaProject() )
212         {
213             if ( getLog().isDebugEnabled() )
214             {
215                 getLog().debug( "JAR packaging does not need specific builders" );
216             }
217         }
218 
219         if ( getSpring() != null )
220         {
221             commands.add( MYECLIPSE_SPRING_BUILDER );
222         }
223         if ( getHibernate() != null )
224         {
225             commands.add( MYECLIPSE_HIBERNATE_BUILDER );
226         }
227 
228         convertBuildCommandList( commands );
229         getBuildcommands().addAll( commands );
230     }
231 
232     /**
233      * Override the default natures with the natures used by MyEclipse
234      * 
235      * @param packaging packaging-type (jar,war,ejb,ear)
236      */
237     protected void fillDefaultNatures( String packaging )
238     {
239         List natures = new ArrayList();
240 
241         super.fillDefaultNatures( packaging );
242 
243         if ( Constants.PROJECT_PACKAGING_EAR.equals( packaging ) )
244         {
245             natures.add( MYECLIPSE_EAR_NATURE );
246         }
247         else if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
248         {
249             natures.add( MYECLIPSE_WEB_NATURE );
250         }
251         else if ( Constants.PROJECT_PACKAGING_EJB.equals( packaging ) )
252         {
253             if ( getLog().isInfoEnabled() )
254             {
255                 getLog().info( "EJB packaging is not implemented yet" );
256             }
257         }
258         else if ( isJavaProject() )
259         {
260             if ( getLog().isDebugEnabled() )
261             {
262                 getLog().debug( "JAR projects does not need specific natures" );
263             }
264         }
265 
266         // Spring
267         if ( getSpring() != null )
268         {
269             natures.add( MYECLISPE_SPRING_NATURE );
270         }
271         // Struts
272         if ( getStruts() != null )
273         {
274             natures.add( MYECLIPSE_STRUTS_NATURE );
275         }
276 
277         // Hibernate
278         if ( getHibernate() != null )
279         {
280             natures.add( MYECLIPSE_HIBERNATE_NATURE );
281         }
282 
283         getProjectnatures().addAll( natures );
284     }
285 
286     protected void fillDefaultClasspathContainers( String packaging )
287     {
288         super.fillDefaultClasspathContainers( packaging );
289 
290         if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
291         {
292             String j2eeVersion;
293             if ( this.jeeversion != null )
294             {
295                 j2eeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( this.jeeversion ).getJeeVersion();
296             }
297             else
298             {
299                 j2eeVersion =
300                     JeeUtils.getJeeDescriptorFromServletVersion( JeeUtils.resolveServletVersion( project ) ).getJeeVersion();
301             }
302 
303             if ( "1.3".equals( j2eeVersion ) )
304             {
305                 getClasspathContainers().add( MYECLIPSE_J2EE_13_CLASSPATH_CONTAINER );
306             }
307             else if ( "1.4".equals( j2eeVersion ) )
308             {
309                 getClasspathContainers().add( MYECLIPSE_J2EE_14_CLASSPATH_CONTAINER );
310             }
311         }
312     }
313 
314     public Map getSpring()
315     {
316         return spring;
317     }
318 
319     public void setSpring( Map spring )
320     {
321         this.spring = spring;
322     }
323 
324     public Map getHibernate()
325     {
326         return hibernate;
327     }
328 
329     public void setHibernate( Map hibernate )
330     {
331         this.hibernate = hibernate;
332     }
333 
334     public Map getStruts()
335     {
336         return struts;
337     }
338 
339     public void setStruts( Map struts )
340     {
341         this.struts = struts;
342     }
343 
344 }