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 continue;
112 }
113 if (excludeScopes.contains(scope)) {
114
115 continue;
116 }
117
118 Logger log = getLogger();
119
120 String id = MojoHelper.getArtifactId(artifact);
121
122 if (verbose) {
123 log.info("detected artifact " + id);
124 }
125
126
127
128 boolean isToInclude = haveNoIncludedArtifacts && haveNoIncludedGroups
129 || isIncludable(artifact, includedGroupPattern, includedArtifactPattern);
130
131
132 boolean isToExclude = isToInclude && haveExclusions
133 && isExcludable(artifact, excludedGroupPattern, excludedArtifactPattern);
134
135 if (!isToInclude || isToExclude) {
136 if (verbose) {
137 log.info("skip artifact " + id);
138 }
139 continue;
140 }
141
142 MavenProject depMavenProject = null;
143
144 if (cache != null) {
145
146
147 depMavenProject = cache.get(id);
148 }
149
150 if (depMavenProject != null) {
151 if (verbose) {
152 log.info("add dependency [" + id + "] (from cache)");
153 }
154 } else {
155
156 try {
157 depMavenProject = mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories,
158 localRepository, true);
159 } catch (ProjectBuildingException e) {
160 log.warn("Unable to obtain POM for artifact : " + artifact, e);
161 continue;
162 }
163
164 if (verbose) {
165 log.info("add dependency [" + id + "]");
166 }
167 if (cache != null) {
168
169 cache.put(id, depMavenProject);
170 }
171 }
172
173
174 result.put(id, depMavenProject);
175 }
176
177 return result;
178 }
179
180 protected boolean isIncludable(Artifact project, Pattern includedGroupPattern, Pattern includedArtifactPattern) {
181
182 Logger log = getLogger();
183
184
185 if (includedGroupPattern != null) {
186
187 try {
188 Matcher matchGroupId = includedGroupPattern.matcher(project.getGroupId());
189 if (matchGroupId.find()) {
190 if (log.isDebugEnabled()) {
191 log.debug("Include " + project.getGroupId());
192 }
193 return true;
194 }
195 } catch (PatternSyntaxException e) {
196 log.warn(String.format(INVALID_PATTERN_MESSAGE, includedGroupPattern.pattern()));
197 }
198 }
199
200
201 if (includedArtifactPattern != null) {
202
203 try {
204 Matcher matchGroupId = includedArtifactPattern.matcher(project.getArtifactId());
205 if (matchGroupId.find()) {
206 if (log.isDebugEnabled()) {
207 log.debug("Include " + project.getArtifactId());
208 }
209 return true;
210 }
211 } catch (PatternSyntaxException e) {
212 log.warn(String.format(INVALID_PATTERN_MESSAGE, includedArtifactPattern.pattern()));
213 }
214 }
215
216 return false;
217 }
218
219 protected boolean isExcludable(Artifact project, Pattern excludedGroupPattern, Pattern excludedArtifactPattern) {
220
221 Logger log = getLogger();
222
223
224 if (excludedGroupPattern != null) {
225
226 try {
227 Matcher matchGroupId = excludedGroupPattern.matcher(project.getGroupId());
228 if (matchGroupId.find()) {
229 if (log.isDebugEnabled()) {
230 log.debug("Exclude " + project.getGroupId());
231 }
232 return true;
233 }
234 } catch (PatternSyntaxException e) {
235 log.warn(String.format(INVALID_PATTERN_MESSAGE, excludedGroupPattern.pattern()));
236 }
237 }
238
239
240 if (excludedArtifactPattern != null) {
241
242 try {
243 Matcher matchGroupId = excludedArtifactPattern.matcher(project.getArtifactId());
244 if (matchGroupId.find()) {
245 if (log.isDebugEnabled()) {
246 log.debug("Exclude " + project.getArtifactId());
247 }
248 return true;
249 }
250 } catch (PatternSyntaxException e) {
251 log.warn(String.format(INVALID_PATTERN_MESSAGE, excludedArtifactPattern.pattern()));
252 }
253 }
254
255 return false;
256 }
257 }