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