View Javadoc
1   package org.apache.maven;
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.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.inject.Inject;
33  import javax.inject.Named;
34  
35  import org.apache.maven.artifact.ArtifactUtils;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.project.MavenProject;
38  import org.eclipse.aether.artifact.Artifact;
39  import org.eclipse.aether.repository.WorkspaceReader;
40  import org.eclipse.aether.repository.WorkspaceRepository;
41  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
42  
43  /**
44   * An implementation of a workspace reader that knows how to search the Maven reactor for artifacts, either
45   * as packaged jar if it has been built, or only compile output directory if packaging hasn't happened yet.
46   * 
47   * @author Jason van Zyl
48   */
49  @Named( ReactorReader.HINT )
50  @SessionScoped
51  class ReactorReader
52      implements WorkspaceReader
53  {
54      public static final String HINT = "reactor";
55      
56      private static final Collection<String> COMPILE_PHASE_TYPES = Arrays.asList( "jar", "ejb-client" );
57  
58      private Map<String, MavenProject> projectsByGAV;
59  
60      private Map<String, List<MavenProject>> projectsByGA;
61  
62      private WorkspaceRepository repository;
63  
64      @Inject
65      public ReactorReader( MavenSession session )
66      {
67          projectsByGAV = session.getProjectMap();
68  
69          projectsByGA = new HashMap<String, List<MavenProject>>( projectsByGAV.size() * 2 );
70          for ( MavenProject project : projectsByGAV.values() )
71          {
72              String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
73  
74              List<MavenProject> projects = projectsByGA.get( key );
75  
76              if ( projects == null )
77              {
78                  projects = new ArrayList<MavenProject>( 1 );
79                  projectsByGA.put( key, projects );
80              }
81  
82              projects.add( project );
83          }
84  
85          repository = new WorkspaceRepository( "reactor", new HashSet<String>( projectsByGAV.keySet() ) );
86      }
87  
88      //
89      // Public API
90      //
91  
92      public WorkspaceRepository getRepository()
93      {
94          return repository;
95      }
96  
97      public File findArtifact( Artifact artifact )
98      {
99          String projectKey = ArtifactUtils.key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
100 
101         MavenProject project = projectsByGAV.get( projectKey );
102 
103         if ( project != null )
104         {
105             File file = find( project, artifact );
106             if ( file == null && project != project.getExecutionProject() )
107             {
108                 file = find( project.getExecutionProject(), artifact );
109             }
110             return file;
111         }
112 
113         return null;
114     }
115 
116     public List<String> findVersions( Artifact artifact )
117     {
118         String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
119 
120         List<MavenProject> projects = projectsByGA.get( key );
121         if ( projects == null || projects.isEmpty() )
122         {
123             return Collections.emptyList();
124         }
125 
126         List<String> versions = new ArrayList<String>();
127 
128         for ( MavenProject project : projects )
129         {
130             if ( find( project, artifact ) != null )
131             {
132                 versions.add( project.getVersion() );
133             }
134         }
135 
136         return Collections.unmodifiableList( versions );
137     }
138 
139     //
140     // Implementation
141     //
142 
143     private File find( MavenProject project, Artifact artifact )
144     {
145         if ( "pom".equals( artifact.getExtension() ) )
146         {
147             return project.getFile();
148         }
149 
150         Artifact projectArtifact = findMatchingArtifact( project, artifact );
151 
152         if ( hasArtifactFileFromPackagePhase( projectArtifact ) )
153         {
154             return projectArtifact.getFile();
155         }
156         else if ( !hasBeenPackaged( project ) )
157         {
158             // fallback to loose class files only if artifacts haven't been packaged yet
159             // and only for plain old jars. Not war files, not ear files, not anything else.
160 
161             if ( isTestArtifact( artifact ) )
162             {
163                 if ( project.hasLifecyclePhase( "test-compile" ) )
164                 {
165                     return new File( project.getBuild().getTestOutputDirectory() );
166                 }
167             }
168             else
169             {
170                 String type = artifact.getProperty( "type", "" );
171                 if ( project.hasLifecyclePhase( "compile" ) && COMPILE_PHASE_TYPES.contains( type ) )
172                 {
173                     return new File( project.getBuild().getOutputDirectory() );
174                 }
175             }
176         }
177 
178         // The fall-through indicates that the artifact cannot be found;
179         // for instance if package produced nothing or classifier problems.
180         return null;
181     }
182 
183     private boolean hasArtifactFileFromPackagePhase( Artifact projectArtifact )
184     {
185         return projectArtifact != null && projectArtifact.getFile() != null && projectArtifact.getFile().exists();
186     }
187 
188     private boolean hasBeenPackaged( MavenProject project )
189     {
190         return project.hasLifecyclePhase( "package" ) || project.hasLifecyclePhase( "install" )
191             || project.hasLifecyclePhase( "deploy" );
192     }
193 
194     /**
195      * Tries to resolve the specified artifact from the artifacts of the given project.
196      * 
197      * @param project The project to try to resolve the artifact from, must not be <code>null</code>.
198      * @param requestedArtifact The artifact to resolve, must not be <code>null</code>.
199      * @return The matching artifact from the project or <code>null</code> if not found. Note that this
200      */
201     private Artifact findMatchingArtifact( MavenProject project, Artifact requestedArtifact )
202     {
203         String requestedRepositoryConflictId = ArtifactIdUtils.toVersionlessId( requestedArtifact );
204 
205         Artifact mainArtifact = RepositoryUtils.toArtifact( project.getArtifact() );
206         if ( requestedRepositoryConflictId.equals( ArtifactIdUtils.toVersionlessId( mainArtifact ) ) )
207         {
208             return mainArtifact;
209         }
210 
211         for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) )
212         {
213             if ( attachedArtifactComparison( requestedArtifact, attachedArtifact ) )
214             {
215                 return attachedArtifact;
216             }
217         }
218 
219         return null;
220     }
221 
222     private boolean attachedArtifactComparison( Artifact requested, Artifact attached )
223     {
224         //
225         // We are taking as much as we can from the DefaultArtifact.equals(). The requested artifact has no file so
226         // we want to remove that from the comparison.
227         //
228         return requested.getArtifactId().equals( attached.getArtifactId() )
229             && requested.getGroupId().equals( attached.getGroupId() )
230             && requested.getVersion().equals( attached.getVersion() )
231             && requested.getExtension().equals( attached.getExtension() )
232             && requested.getClassifier().equals( attached.getClassifier() );
233     }
234 
235     /**
236      * Determines whether the specified artifact refers to test classes.
237      * 
238      * @param artifact The artifact to check, must not be {@code null}.
239      * @return {@code true} if the artifact refers to test classes, {@code false} otherwise.
240      */
241     private static boolean isTestArtifact( Artifact artifact )
242     {
243         return ( "test-jar".equals( artifact.getProperty( "type", "" ) ) )
244             || ( "jar".equals( artifact.getExtension() ) && "tests".equals( artifact.getClassifier() ) );
245     }
246 }