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 =
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
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
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
169
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
189
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
206
207
208
209
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
236
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
247
248
249
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 }