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