View Javadoc

1   package org.codehaus.mojo.springide;
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.io.FileReader;
24  import java.io.FileWriter;
25  import java.io.FilenameFilter;
26  import java.io.IOException;
27  import java.io.Writer;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.commons.io.FileUtils;
35  import org.apache.maven.model.Resource;
36  import org.apache.maven.plugin.AbstractMojo;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.MojoFailureException;
39  import org.apache.maven.project.MavenProject;
40  import org.codehaus.plexus.util.DirectoryScanner;
41  import org.codehaus.plexus.util.xml.Xpp3Dom;
42  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
43  import org.codehaus.plexus.util.xml.Xpp3DomWriter;
44  
45  import freemarker.template.Configuration;
46  import freemarker.template.Template;
47  import freemarker.template.TemplateException;
48  
49  /**
50   * @author <a href="mailto:nicolas@apache.org">nicolas de loof</a>
51   * @goal generate
52   */
53  public class SpringIDEMojo
54      extends AbstractMojo
55  {
56      /**
57       * @parameter default-value="xml"
58       * @alias configExtensions
59       */
60      private String configSuffixes;
61  
62      /**
63       * @parameter default-value="**\/applicationContext*.xml, META-INF/spring/**\/*.xml"
64       */
65      private String includes;
66  
67      /**
68       * @parameter
69       */
70      private String excludes;
71  
72      /**
73       * @parameter default-value="true"
74       */
75      private boolean allowBeanDefinitionOverriding;
76  
77      /**
78       * @parameter default-value="true"
79       */
80      private boolean enableImports;
81  
82      /**
83       * @parameter default-value="true"
84       */
85      private boolean incomplete;
86  
87      /**
88       * @parameter default-value="${project.artifactId}"
89       */
90      private String name;
91  
92      /**
93       * The project whose project files to create.
94       *
95       * @parameter expression="${project}"
96       * @required
97       */
98      private MavenProject project;
99  
100     /**
101      * {@inheritDoc}
102      *
103      * @see org.apache.maven.plugin.Mojo#execute()
104      */
105     public void execute()
106         throws MojoExecutionException, MojoFailureException
107     {
108         if ( "pom".equals( project.getPackaging() ) )
109         {
110             return;
111         }
112 
113         List configs = new LinkedList();
114 
115         String src = project.getBuild().getSourceDirectory();
116         scan( configs, new File( src ) );
117 
118         for ( Iterator iterator = project.getBuild().getResources().iterator(); iterator.hasNext(); )
119         {
120             Resource resource = (Resource) iterator.next();
121             scan( configs, new File( resource.getDirectory() ) );
122         }
123 
124         String[] generated = new File( project.getBuild().getOutputDirectory() ).list( new FilenameFilter()
125         {
126             public boolean accept( File dir, String name )
127             {
128                 return name.startsWith( "generated-" );
129             }
130         } );
131 
132         for ( int i = 0; i < generated.length; i++ )
133         {
134             scan( configs, new File( generated[i] ) );
135         }
136 
137         if ( configs.size() > 0 )
138         {
139             forceSpringNature();
140             Map context = new HashMap();
141             context.put( "configs", configs );
142             context.put( "configSuffixes", configSuffixes.split( "," ) );
143             context.put( "allowBeanDefinitionOverriding", String.valueOf( allowBeanDefinitionOverriding ) );
144             context.put( "incomplete", String.valueOf( incomplete ) );
145             context.put( "enableImports", String.valueOf( enableImports ) );
146             context.put( "name", name );
147             getLog().info( "create SpringIDE configuration for " + name );
148 
149             File dotSpringBeans = new File( project.getBasedir(), ".springBeans" );
150             applyTemplate( context, dotSpringBeans, "springBeans.fm" );
151 
152             File prefs = new File( project.getBasedir(), ".settings/org.springframework.ide.eclipse.core.prefs" );
153             applyTemplate( context, prefs, "prefs.fm" );
154         }
155         else
156         {
157             getLog().info( "No spring context file found in project" );
158         }
159     }
160 
161     private void scan( List configs, File src )
162     {
163         if ( !src.exists() )
164         {
165             return;
166         }
167         DirectoryScanner scanner = new DirectoryScanner();
168         scanner.setBasedir( src );
169         scanner.setIncludes( includes.split( "," ) );
170         if (excludes != null)
171         {
172             scanner.setExcludes( excludes.split( "," ) );
173         }
174         scanner.scan();
175         String[] files = scanner.getIncludedFiles();
176         for ( int i = 0; i < files.length; i++ )
177         {
178             configs.add( src.getAbsolutePath().substring( project.getBasedir().getAbsolutePath().length() ) + "/" + files[i] );
179         }
180         return;
181     }
182 
183     protected void applyTemplate( Map context, File dotSpringBean, String template )
184         throws MojoExecutionException
185     {
186         Configuration cfg = new Configuration();
187         cfg.setClassForTemplateLoading( getClass(), "" );
188 
189         try
190         {
191             Writer configWriter = new FileWriter( dotSpringBean );
192             Template tpl = cfg.getTemplate( template );
193             tpl.process( context, configWriter );
194             configWriter.flush();
195             configWriter.close();
196             getLog().debug( "Write SpringIDE configuration to: " + dotSpringBean.getAbsolutePath() );
197         }
198         catch ( IOException ioe )
199         {
200             throw new MojoExecutionException( "Unable to write SpringIDE configuration file", ioe );
201         }
202         catch ( TemplateException te )
203         {
204             throw new MojoExecutionException( "Unable to merge freemarker template", te );
205         }
206     }
207 
208     protected void forceSpringNature()
209     {
210         try
211         {
212             File dotProject = new File( project.getBasedir(), ".project" );
213             String content = FileUtils.readFileToString( dotProject, null );
214             if ( content.indexOf( "<nature>org.springframework.ide.eclipse.core.springnature</nature>" ) < 0 )
215             {
216                 getLog().info( "Add spring nature to the eclipse .project file" );
217                 try
218                 {
219                     Xpp3Dom dom = Xpp3DomBuilder.build( new FileReader( dotProject ) );
220                     Xpp3Dom nature = new Xpp3Dom( "nature" );
221                     nature.setValue( "org.springframework.ide.eclipse.core.springnature" );
222                     dom.getChild( "natures" ).addChild( nature );
223                     FileWriter writer = new FileWriter( dotProject );
224                     Xpp3DomWriter.write( writer, dom );
225                     writer.close();
226                 }
227                 catch ( Exception e )
228                 {
229                     getLog().warn( "Failed to add missing tomcat nature to the eclipse .project file", e );
230                 }
231             }
232         }
233         catch ( IOException e )
234         {
235             getLog().info( "Failed to retrieve the Eclipse .project file" );
236         }
237     }
238 
239 }