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