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 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   * Util to consolidate classpath manipulation stuff in one place.
44   * 
45   * @author ccollins
46   * @version $Id$
47   */
48  @Component(role = ClasspathBuilder.class)
49  public class ClasspathBuilder
50      extends AbstractLogEnabled
51  {
52  
53      /**
54       * Build classpath list using either gwtHome (if present) or using *project* dependencies. Note that this is ONLY
55       * used for the script/cmd writers (so the scopes are not for the compiler, or war plugins, etc). This is required
56       * so that the script writers can get the dependencies they need regardless of the Maven scopes (still want to use
57       * the Maven scopes for everything else Maven, but for GWT-Maven we need to access deps differently - directly at
58       * times).
59       *
60       * @param project The maven project the Mojo is running for
61       * @param artifacts the project artifacts (all scopes)
62       * @param scope artifact scope to use
63       * @param isGenerator whether to use processed resources and compiled classes (false), or raw resources (true).
64       * @return file collection for classpath
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          // Note : Don't call addSourceWithActiveProject as a GWT dependency MUST be a valid GWT library module :
75          // * include java sources in the JAR as resources
76          // * define a gwt.xml module file to declare the required inherits
77          // addSourceWithActiveProject would make some java sources available to GWT compiler that should not be accessible in
78          // a non-reactor build, making the build less deterministic and encouraging bad design.
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          // Use our own ClasspathElements fitering, as for RUNTIME we need to include PROVIDED artifacts,
88          // that is not the default Maven policy, as RUNTIME is used here to build the GWTShell execution classpath
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              // Add all project dependencies in classpath
97              for ( Artifact artifact : artifacts )
98              {
99                  items.add( artifact.getFile() );
100             }
101         }
102         else if ( scope.equals( SCOPE_COMPILE ) )
103         {
104             // Add all project dependencies in classpath
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             // Add all dependencies BUT "TEST" as we need PROVIDED ones to setup the execution
119             // GWTShell that is NOT a full JEE server
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      * Add all sources and resources also with active (maven reactor active) referenced project sources and resources.
138      *
139      * @param project
140      * @param items
141      * @param scope
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      * Add all sources and resources also with active (maven reactor active) referenced project sources and resources.
164      *
165      * @param project
166      * @param items
167      * @param scope
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      * Get artifacts for specific scope.
190      *
191      * @param project
192      * @param scope
193      * @return
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      * Get source roots for specific scope.
217      *
218      * @param project
219      * @param scope
220      * @return
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      * Get resources for specific scope.
243      *
244      * @param project
245      * @param scope
246      * @return
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      * Add source path and resource paths of the project to the list of classpath items.
269      *
270      * @param items Classpath items.
271      * @param sourceRoots
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      * Add source path and resource paths of the project to the list of classpath items.
283      *
284      * @param items Classpath items.
285      * @param resources
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      * Cut from MavenProject.java
297      *
298      * @param groupId
299      * @param artifactId
300      * @param version
301      * @return
302      */
303     private String getProjectReferenceId( final String groupId, final String artifactId, final String version )
304     {
305         return groupId + ":" + artifactId + ":" + version;
306     }
307 }