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