1 package org.codehaus.mojo.gwt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37
38
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
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
122
123
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
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 }