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 static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
23 import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
24 import static org.apache.maven.artifact.Artifact.SCOPE_RUNTIME;
25 import static org.apache.maven.artifact.Artifact.SCOPE_SYSTEM;
26 import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
27
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.LinkedHashSet;
32 import java.util.List;
33 import java.util.Set;
34
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.model.Resource;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.project.MavenProject;
39 import org.codehaus.plexus.component.annotations.Component;
40 import org.codehaus.plexus.logging.AbstractLogEnabled;
41
42
43
44
45
46
47
48 @Component(role = ClasspathBuilder.class)
49 public class ClasspathBuilder
50 extends AbstractLogEnabled
51 {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public Collection<File> buildClasspathList( final MavenProject project, final String scope,
67 Set<Artifact> artifacts, boolean isGenerator )
68 throws ClasspathBuilderException
69 {
70 getLogger().debug( "establishing classpath list (scope = " + scope + ")" );
71
72 Set<File> items = new LinkedHashSet<File>();
73
74
75
76
77
78
79
80 if ( !isGenerator ) {
81 items.add( new File( project.getBuild().getOutputDirectory() ) );
82 }
83 addSources( items, project.getCompileSourceRoots() );
84 if ( isGenerator ) {
85 addResources( items, project.getResources() );
86 }
87
88
89
90 if ( scope.equals( SCOPE_TEST ) )
91 {
92 addSources( items, project.getTestCompileSourceRoots() );
93 addResources( items, project.getTestResources() );
94 items.add( new File( project.getBuild().getTestOutputDirectory() ) );
95
96
97 for ( Artifact artifact : artifacts )
98 {
99 items.add( artifact.getFile() );
100 }
101 }
102 else if ( scope.equals( SCOPE_COMPILE ) )
103 {
104
105 getLogger().debug( "candidate artifacts : " + artifacts.size() );
106 for ( Artifact artifact : artifacts )
107 {
108 String artifactScope = artifact.getScope();
109 if ( SCOPE_COMPILE.equals( artifactScope ) || SCOPE_PROVIDED.equals( artifactScope )
110 || SCOPE_SYSTEM.equals( artifactScope ) )
111 {
112 items.add( artifact.getFile() );
113 }
114 }
115 }
116 else if ( scope.equals( SCOPE_RUNTIME ) )
117 {
118
119
120 for ( Artifact artifact : artifacts )
121 {
122 getLogger().debug( "candidate artifact : " + artifact );
123 if ( !artifact.getScope().equals( SCOPE_TEST ) && artifact.getArtifactHandler().isAddedToClasspath() )
124 {
125 items.add( artifact.getFile() );
126 }
127 }
128 }
129 else
130 {
131 throw new ClasspathBuilderException( "unsupported scope " + scope );
132 }
133 return items;
134 }
135
136
137
138
139
140
141
142
143 public void addSourcesWithActiveProjects( final MavenProject project, final Collection<File> items,
144 final String scope )
145 {
146 final List<Artifact> scopeArtifacts = getScopeArtifacts( project, scope );
147
148 addSources( items, getSourceRoots( project, scope ) );
149
150 for ( Artifact artifact : scopeArtifacts )
151 {
152 String projectReferenceId =
153 getProjectReferenceId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
154 MavenProject refProject = project.getProjectReferences().get( projectReferenceId );
155 if ( refProject != null && "sources".equals( artifact.getClassifier() ) )
156 {
157 addSources( items, getSourceRoots( refProject, scope ) );
158 }
159 }
160 }
161
162
163
164
165
166
167
168
169 public void addResourcesWithActiveProjects( final MavenProject project, final Collection<File> items,
170 final String scope )
171 {
172 final List<Artifact> scopeArtifacts = getScopeArtifacts( project, scope );
173
174 addResources( items, getResources( project, scope ) );
175
176 for ( Artifact artifact : scopeArtifacts )
177 {
178 String projectReferenceId =
179 getProjectReferenceId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
180 MavenProject refProject = (MavenProject) project.getProjectReferences().get( projectReferenceId );
181 if ( refProject != null )
182 {
183 addResources( items, getResources( refProject, scope ) );
184 }
185 }
186 }
187
188
189
190
191
192
193
194
195 private List<Artifact> getScopeArtifacts( final MavenProject project, final String scope )
196 {
197 if ( SCOPE_COMPILE.equals( scope ) )
198 {
199 return project.getCompileArtifacts();
200 }
201 if ( SCOPE_RUNTIME.equals( scope ) )
202 {
203 return project.getRuntimeArtifacts();
204 }
205 else if ( SCOPE_TEST.equals( scope ) )
206 {
207 return project.getTestArtifacts();
208 }
209 else
210 {
211 throw new RuntimeException( "Not allowed scope " + scope );
212 }
213 }
214
215
216
217
218
219
220
221
222 private List<String> getSourceRoots( final MavenProject project, final String scope )
223 {
224 if ( SCOPE_COMPILE.equals( scope ) || SCOPE_RUNTIME.equals( scope ) )
225 {
226 return project.getCompileSourceRoots();
227 }
228 else if ( SCOPE_TEST.equals( scope ) )
229 {
230 List<String> sourceRoots = new ArrayList<String>();
231 sourceRoots.addAll( project.getTestCompileSourceRoots() );
232 sourceRoots.addAll( project.getCompileSourceRoots() );
233 return sourceRoots;
234 }
235 else
236 {
237 throw new RuntimeException( "Not allowed scope " + scope );
238 }
239 }
240
241
242
243
244
245
246
247
248 private List<Resource> getResources( final MavenProject project, final String scope )
249 {
250 if ( SCOPE_COMPILE.equals( scope ) || SCOPE_RUNTIME.equals( scope ) )
251 {
252 return project.getResources();
253 }
254 else if ( SCOPE_TEST.equals( scope ) )
255 {
256 List<Resource> resources = new ArrayList<Resource>();
257 resources.addAll( project.getTestResources() );
258 resources.addAll( project.getResources() );
259 return resources;
260 }
261 else
262 {
263 throw new RuntimeException( "Not allowed scope " + scope );
264 }
265 }
266
267
268
269
270
271
272
273 private void addSources( final Collection<File> items, final Collection<String> sourceRoots )
274 {
275 for ( String path : sourceRoots )
276 {
277 items.add( new File( path ) );
278 }
279 }
280
281
282
283
284
285
286
287 private void addResources( final Collection<File> items, final Collection<Resource> resources )
288 {
289 for ( Resource resource : resources )
290 {
291 items.add( new File( resource.getDirectory() ) );
292 }
293 }
294
295
296
297
298
299
300
301
302
303 private String getProjectReferenceId( final String groupId, final String artifactId, final String version )
304 {
305 return groupId + ":" + artifactId + ":" + version;
306 }
307 }