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.project.MavenProject;
38 import org.eclipse.aether.artifact.Artifact;
39 import org.eclipse.aether.repository.WorkspaceReader;
40 import org.eclipse.aether.repository.WorkspaceRepository;
41 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
42
43
44
45
46
47
48
49 @Named( ReactorReader.HINT )
50 @SessionScoped
51 class ReactorReader
52 implements WorkspaceReader
53 {
54 public static final String HINT = "reactor";
55
56 private static final Collection<String> COMPILE_PHASE_TYPES = Arrays.asList( "jar", "ejb-client" );
57
58 private Map<String, MavenProject> projectsByGAV;
59
60 private Map<String, List<MavenProject>> projectsByGA;
61
62 private WorkspaceRepository repository;
63
64 @Inject
65 public ReactorReader( MavenSession session )
66 {
67 projectsByGAV = session.getProjectMap();
68
69 projectsByGA = new HashMap<String, List<MavenProject>>( projectsByGAV.size() * 2 );
70 for ( MavenProject project : projectsByGAV.values() )
71 {
72 String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
73
74 List<MavenProject> projects = projectsByGA.get( key );
75
76 if ( projects == null )
77 {
78 projects = new ArrayList<MavenProject>( 1 );
79 projectsByGA.put( key, projects );
80 }
81
82 projects.add( project );
83 }
84
85 repository = new WorkspaceRepository( "reactor", new HashSet<String>( projectsByGAV.keySet() ) );
86 }
87
88
89
90
91
92 public WorkspaceRepository getRepository()
93 {
94 return repository;
95 }
96
97 public File findArtifact( Artifact artifact )
98 {
99 String projectKey = ArtifactUtils.key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
100
101 MavenProject project = projectsByGAV.get( projectKey );
102
103 if ( project != null )
104 {
105 File file = find( project, artifact );
106 if ( file == null && project != project.getExecutionProject() )
107 {
108 file = find( project.getExecutionProject(), artifact );
109 }
110 return file;
111 }
112
113 return null;
114 }
115
116 public List<String> findVersions( Artifact artifact )
117 {
118 String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
119
120 List<MavenProject> projects = projectsByGA.get( key );
121 if ( projects == null || projects.isEmpty() )
122 {
123 return Collections.emptyList();
124 }
125
126 List<String> versions = new ArrayList<String>();
127
128 for ( MavenProject project : projects )
129 {
130 if ( find( project, artifact ) != null )
131 {
132 versions.add( project.getVersion() );
133 }
134 }
135
136 return Collections.unmodifiableList( versions );
137 }
138
139
140
141
142
143 private File find( MavenProject project, Artifact artifact )
144 {
145 if ( "pom".equals( artifact.getExtension() ) )
146 {
147 return project.getFile();
148 }
149
150 Artifact projectArtifact = findMatchingArtifact( project, artifact );
151
152 if ( hasArtifactFileFromPackagePhase( projectArtifact ) )
153 {
154 return projectArtifact.getFile();
155 }
156 else if ( !hasBeenPackaged( project ) )
157 {
158
159
160
161 if ( isTestArtifact( artifact ) )
162 {
163 if ( project.hasLifecyclePhase( "test-compile" ) )
164 {
165 return new File( project.getBuild().getTestOutputDirectory() );
166 }
167 }
168 else
169 {
170 String type = artifact.getProperty( "type", "" );
171 if ( project.hasLifecyclePhase( "compile" ) && COMPILE_PHASE_TYPES.contains( type ) )
172 {
173 return new File( project.getBuild().getOutputDirectory() );
174 }
175 }
176 }
177
178
179
180 return null;
181 }
182
183 private boolean hasArtifactFileFromPackagePhase( Artifact projectArtifact )
184 {
185 return projectArtifact != null && projectArtifact.getFile() != null && projectArtifact.getFile().exists();
186 }
187
188 private boolean hasBeenPackaged( MavenProject project )
189 {
190 return project.hasLifecyclePhase( "package" ) || project.hasLifecyclePhase( "install" )
191 || project.hasLifecyclePhase( "deploy" );
192 }
193
194
195
196
197
198
199
200
201 private Artifact findMatchingArtifact( MavenProject project, Artifact requestedArtifact )
202 {
203 String requestedRepositoryConflictId = ArtifactIdUtils.toVersionlessId( requestedArtifact );
204
205 Artifact mainArtifact = RepositoryUtils.toArtifact( project.getArtifact() );
206 if ( requestedRepositoryConflictId.equals( ArtifactIdUtils.toVersionlessId( mainArtifact ) ) )
207 {
208 return mainArtifact;
209 }
210
211 for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) )
212 {
213 if ( attachedArtifactComparison( requestedArtifact, attachedArtifact ) )
214 {
215 return attachedArtifact;
216 }
217 }
218
219 return null;
220 }
221
222 private boolean attachedArtifactComparison( Artifact requested, Artifact attached )
223 {
224
225
226
227
228 return requested.getArtifactId().equals( attached.getArtifactId() )
229 && requested.getGroupId().equals( attached.getGroupId() )
230 && requested.getVersion().equals( attached.getVersion() )
231 && requested.getExtension().equals( attached.getExtension() )
232 && requested.getClassifier().equals( attached.getClassifier() );
233 }
234
235
236
237
238
239
240
241 private static boolean isTestArtifact( Artifact artifact )
242 {
243 return ( "test-jar".equals( artifact.getProperty( "type", "" ) ) )
244 || ( "jar".equals( artifact.getExtension() ) && "tests".equals( artifact.getClassifier() ) );
245 }
246 }