1 package org.apache.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
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
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
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
168
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
188
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
205
206
207
208
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
235
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
246
247
248
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 }