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