1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.codehaus.mojo.license;
26
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.MavenProjectBuilder;
33 import org.apache.maven.project.ProjectBuildingException;
34 import org.codehaus.plexus.logging.AbstractLogEnabled;
35 import org.codehaus.plexus.logging.Logger;
36
37 import java.util.List;
38 import java.util.Set;
39 import java.util.SortedMap;
40 import java.util.TreeMap;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import java.util.regex.PatternSyntaxException;
44
45
46
47
48
49
50
51
52
53 public class DefaultDependenciesTool
54 extends AbstractLogEnabled
55 implements DependenciesTool
56 {
57
58 public static final String INVALID_PATTERN_MESSAGE =
59 "The pattern specified by expression <%s> seems to be invalid.";
60
61
62
63
64
65
66 private MavenProjectBuilder mavenProjectBuilder;
67
68
69
70
71 public SortedMap<String, MavenProject> loadProjectDependencies( MavenProject project,
72 MavenProjectDependenciesConfigurator configuration,
73 ArtifactRepository localRepository,
74 List<ArtifactRepository> remoteRepositories,
75 SortedMap<String, MavenProject> cache )
76 {
77
78 boolean haveNoIncludedGroups = StringUtils.isEmpty( configuration.getIncludedGroups() );
79 boolean haveNoIncludedArtifacts = StringUtils.isEmpty( configuration.getIncludedArtifacts() );
80
81 boolean haveExcludedGroups = StringUtils.isNotEmpty( configuration.getExcludedGroups() );
82 boolean haveExcludedArtifacts = StringUtils.isNotEmpty( configuration.getExcludedArtifacts() );
83 boolean haveExclusions = haveExcludedGroups || haveExcludedArtifacts;
84
85 Pattern includedGroupPattern = null;
86 Pattern includedArtifactPattern = null;
87 Pattern excludedGroupPattern = null;
88 Pattern excludedArtifactPattern = null;
89
90 if ( !haveNoIncludedGroups )
91 {
92 includedGroupPattern = Pattern.compile( configuration.getIncludedGroups() );
93 }
94 if ( !haveNoIncludedArtifacts )
95 {
96 includedArtifactPattern = Pattern.compile( configuration.getIncludedArtifacts() );
97 }
98 if ( haveExcludedGroups )
99 {
100 excludedGroupPattern = Pattern.compile( configuration.getExcludedGroups() );
101 }
102 if ( haveExcludedArtifacts )
103 {
104 excludedArtifactPattern = Pattern.compile( configuration.getExcludedArtifacts() );
105 }
106
107 Set<?> depArtifacts;
108
109 if ( configuration.isIncludeTransitiveDependencies() )
110 {
111
112 depArtifacts = project.getArtifacts();
113 }
114 else
115 {
116
117 depArtifacts = project.getDependencyArtifacts();
118 }
119
120 List<String> includedScopes = configuration.getIncludedScopes();
121 List<String> excludeScopes = configuration.getExcludedScopes();
122
123 boolean verbose = configuration.isVerbose();
124
125 SortedMap<String, MavenProject> result = new TreeMap<String, MavenProject>();
126
127 for ( Object o : depArtifacts )
128 {
129 Artifact artifact = (Artifact) o;
130
131 String scope = artifact.getScope();
132 if ( CollectionUtils.isNotEmpty( includedScopes ) && !includedScopes.contains( scope ) )
133 {
134
135
136 continue;
137 }
138 {
139 if ( excludeScopes.contains( scope ) )
140 {
141
142
143 continue;
144 }
145 }
146
147 Logger log = getLogger();
148
149 String id = MojoHelper.getArtifactId( artifact );
150
151 if ( verbose )
152 {
153 log.info( "detected artifact " + id );
154 }
155
156
157
158 boolean isToInclude = haveNoIncludedArtifacts && haveNoIncludedGroups ||
159 isIncludable( artifact, includedGroupPattern, includedArtifactPattern );
160
161
162 boolean isToExclude = isToInclude && haveExclusions &&
163 isExcludable( artifact, excludedGroupPattern, excludedArtifactPattern );
164
165 if ( !isToInclude || isToExclude )
166 {
167 if ( verbose )
168 {
169 log.info( "skip artifact " + id );
170 }
171 continue;
172 }
173
174 MavenProject depMavenProject = null;
175
176 if ( cache != null )
177 {
178
179
180 depMavenProject = cache.get( id );
181 }
182
183 if ( depMavenProject != null )
184 {
185 if ( verbose )
186 {
187 log.info( "add dependency [" + id + "] (from cache)" );
188 }
189 }
190 else
191 {
192
193
194
195 try
196 {
197 depMavenProject =
198 mavenProjectBuilder.buildFromRepository( artifact, remoteRepositories, localRepository, true );
199 }
200 catch ( ProjectBuildingException e )
201 {
202 log.warn( "Unable to obtain POM for artifact : " + artifact, e );
203 continue;
204 }
205
206 if ( verbose )
207 {
208 log.info( "add dependency [" + id + "]" );
209 }
210 if ( cache != null )
211 {
212
213
214 cache.put( id, depMavenProject );
215 }
216 }
217
218
219 result.put( id, depMavenProject );
220 }
221
222 return result;
223 }
224
225 protected boolean isIncludable( Artifact project, Pattern includedGroupPattern, Pattern includedArtifactPattern )
226 {
227
228 Logger log = getLogger();
229
230
231 if ( includedGroupPattern != null )
232 {
233
234 try
235 {
236 Matcher matchGroupId = includedGroupPattern.matcher( project.getGroupId() );
237 if ( matchGroupId.find() )
238 {
239 if ( log.isDebugEnabled() )
240 {
241 log.debug( "Include " + project.getGroupId() );
242 }
243 return true;
244 }
245 }
246 catch ( PatternSyntaxException e )
247 {
248 log.warn( String.format( INVALID_PATTERN_MESSAGE, includedGroupPattern.pattern() ) );
249 }
250 }
251
252
253 if ( includedArtifactPattern != null )
254 {
255
256 try
257 {
258 Matcher matchGroupId = includedArtifactPattern.matcher( project.getArtifactId() );
259 if ( matchGroupId.find() )
260 {
261 if ( log.isDebugEnabled() )
262 {
263 log.debug( "Include " + project.getArtifactId() );
264 }
265 return true;
266 }
267 }
268 catch ( PatternSyntaxException e )
269 {
270 log.warn( String.format( INVALID_PATTERN_MESSAGE, includedArtifactPattern.pattern() ) );
271 }
272 }
273
274 return false;
275 }
276
277 protected boolean isExcludable( Artifact project, Pattern excludedGroupPattern, Pattern excludedArtifactPattern )
278 {
279
280 Logger log = getLogger();
281
282
283 if ( excludedGroupPattern != null )
284 {
285
286 try
287 {
288 Matcher matchGroupId = excludedGroupPattern.matcher( project.getGroupId() );
289 if ( matchGroupId.find() )
290 {
291 if ( log.isDebugEnabled() )
292 {
293 log.debug( "Exclude " + project.getGroupId() );
294 }
295 return true;
296 }
297 }
298 catch ( PatternSyntaxException e )
299 {
300 log.warn( String.format( INVALID_PATTERN_MESSAGE, excludedGroupPattern.pattern() ) );
301 }
302 }
303
304
305 if ( excludedArtifactPattern != null )
306 {
307
308 try
309 {
310 Matcher matchGroupId = excludedArtifactPattern.matcher( project.getArtifactId() );
311 if ( matchGroupId.find() )
312 {
313 if ( log.isDebugEnabled() )
314 {
315 log.debug( "Exclude " + project.getArtifactId() );
316 }
317 return true;
318 }
319 }
320 catch ( PatternSyntaxException e )
321 {
322 log.warn( String.format( INVALID_PATTERN_MESSAGE, excludedArtifactPattern.pattern() ) );
323 }
324 }
325
326 return false;
327 }
328 }