View Javadoc
1   package org.codehaus.mojo.gwt;
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 org.apache.maven.model.Resource;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.codehaus.mojo.gwt.utils.DefaultGwtModuleReader;
25  import org.codehaus.mojo.gwt.utils.GwtModuleReaderException;
26  import org.codehaus.plexus.util.DirectoryScanner;
27  
28  import java.io.File;
29  import java.util.Collection;
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  /**
34   * Collect GWT java source code and module descriptor to be added as resources. Common
35   * functionality for different implementations GwtResourcesMojo and GwtSourcesJarMojo
36   * 
37   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
38   * @author <a href="mailto:vlads@pyx4j.com">Vlad Skarzhevskyy</a>
39   * 
40   */
41  abstract class GwtResourcesBaseMojo
42      extends AbstractGwtModuleMojo
43  {
44  
45      protected class ResourceFile
46      {
47  
48          File basedir;
49  
50          String fileRelativeName;
51  
52          public ResourceFile( File basedir, String fileRelativeName )
53          {
54              super();
55              this.basedir = basedir;
56              this.fileRelativeName = fileRelativeName;
57          }
58  
59      }
60  
61      /**
62       * Collect GWT java source code and module descriptor to be added as resources.
63       */
64      protected Collection<ResourceFile> getAllResourceFiles()
65          throws MojoExecutionException
66      {
67          try
68          {
69              Set<ResourceFile> sourcesAndResources = new HashSet<ResourceFile>();
70              Set<String> sourcesAndResourcesPath = new HashSet<String>();
71              sourcesAndResourcesPath.addAll( getProject().getCompileSourceRoots() );
72              for ( Resource resource : getProject().getResources() )
73              {
74                  sourcesAndResourcesPath.add( resource.getDirectory() );
75              }
76  
77              for ( String name : getModules() )
78              {
79                  GwtModule module = readModule( name );
80  
81                  sourcesAndResources.add( getDescriptor( module, sourcesAndResourcesPath ) );
82                  int count = 1;
83  
84                  for ( String source : module.getSources() )
85                  {
86                      getLog().debug( "GWT sources from " + name + '.' + source );
87                      Collection<ResourceFile> files = getAsResources( module, source, sourcesAndResourcesPath,
88                                                                       "**/*.java" );
89                      sourcesAndResources.addAll( files );
90                      count += files.size();
91  
92                      Collection<ResourceFile> uifiles = getAsResources( module, source, sourcesAndResourcesPath,
93                              "**/*.ui.xml" );
94                      sourcesAndResources.addAll( uifiles );
95                      count += uifiles.size();
96                  }
97                  for ( String source : module.getSuperSources() )
98                  {
99                      getLog().debug( "GWT super-sources from " + name + '.' + source );
100                     Collection<ResourceFile> files = getAsResources( module, source, sourcesAndResourcesPath,
101                                                                      "**/*.java" );
102                     sourcesAndResources.addAll( files );
103                     count += files.size();
104 
105                     Collection<ResourceFile> uifiles = getAsResources( module, source, sourcesAndResourcesPath,
106                             "**/*.ui.xml" );
107                     sourcesAndResources.addAll( uifiles );
108                     count += uifiles.size();
109                 }
110                 getLog().info( count + " source files from GWT module " + name );
111             }
112             return sourcesAndResources;
113         }
114         catch ( GwtModuleReaderException e )
115         {
116             throw new MojoExecutionException( e.getMessage(), e );
117         }
118     }
119 
120     /**
121      * @param source
122      * @param include TODO
123      * @param name
124      */
125     private Collection<ResourceFile> getAsResources( GwtModule module, String source, Set<String> paths, String include )
126     {
127         String pattern = module.getPackage().replace( '.', '/' );
128 
129         Set<ResourceFile> sourcesAndResources = new HashSet<ResourceFile>();
130 
131         for ( String path : paths )
132         {
133             File basedir = new File( path );
134             // the default "src/main/resource" may not phisicaly exist in project
135             if ( !basedir.exists() )
136             {
137                 continue;
138             }
139             DirectoryScanner scanner = new DirectoryScanner();
140             scanner.setBasedir( basedir );
141             scanner.setIncludes( new String[] { pattern + '/' + source + '/' + include } );
142             scanner.scan();
143             String[] includedFiles = scanner.getIncludedFiles();
144             for ( String included : includedFiles )
145             {
146                 sourcesAndResources.add( new ResourceFile( basedir, included ) );
147             }
148         }
149 
150         return sourcesAndResources;
151     }
152 
153     private ResourceFile getDescriptor( GwtModule module, Set<String> paths )
154         throws MojoExecutionException
155     {
156         String moduleFilePath = module.getName().replace( '.', '/' ) + DefaultGwtModuleReader.GWT_MODULE_EXTENSION;
157         for ( String path : paths )
158         {
159             File basedir = new File( path );
160             File descriptor = new File( basedir, moduleFilePath );
161             if ( descriptor.exists() )
162             {
163                 return new ResourceFile( basedir, moduleFilePath );
164             }
165         }
166         throw new MojoExecutionException( "Failed to retrieve GWT descriptor in project sources " + moduleFilePath );
167     }
168 
169 }