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 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
46
47
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
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
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
157
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
177
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
194
195
196
197
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
224
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
235
236
237
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 }