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