1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package com.soebes.maven.plugins.mlv;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.apache.maven.artifact.Artifact;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
37 import org.apache.maven.model.License;
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.MavenProjectBuilder;
42 import org.apache.maven.project.ProjectBuildingException;
43 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44
45 import com.soebes.maven.plugins.mlv.filter.PatternExcludeFilter;
46 import com.soebes.maven.plugins.mlv.licenses.LicenseValidator;
47 import com.soebes.maven.plugins.mlv.licenses.LicensesFile;
48 import com.soebes.maven.plugins.mlv.model.LicensesContainer;
49
50
51
52
53
54 public abstract class AbstractLicenseVerifierPlugIn
55 extends AbstractMojo
56 {
57
58
59
60
61
62
63
64 protected MavenProject project;
65
66
67
68
69
70
71
72
73 protected MavenProjectBuilder projectBuilder;
74
75
76
77
78
79
80
81
82 protected ArtifactRepository localRepository;
83
84
85
86
87
88
89
90
91 protected List<ArtifactRepository> remoteRepositories;
92
93
94
95
96
97
98
99
100 private boolean verbose;
101
102
103
104
105
106
107
108
109 private boolean stricktChecking;
110
111
112
113
114
115
116 protected boolean failOnValid;
117
118
119
120
121
122 protected boolean failOnInvalid;
123
124
125
126
127
128 protected boolean failOnWarning;
129
130
131
132
133
134 protected boolean failOnUnknown;
135
136
137
138
139
140
141
142 protected File licenseFile;
143
144
145
146
147
148
149
150
151
152
153
154
155 protected List<String> excludes;
156
157
158
159
160
161 private ArrayList<LicenseInformation> licenseInformations = new ArrayList<LicenseInformation>();
162
163 private HashMap<String, LicenseInformation> licenseList = new HashMap<String, LicenseInformation>();
164
165 protected LicenseValidator licenseValidator = null;
166 protected LicensesContainer licensesContainer = null;
167
168
169
170
171
172
173
174
175 protected void getDependArtifacts(Set<?> depArtifacts)
176 throws MojoExecutionException {
177
178 PatternExcludeFilter patternExcludeFilter = new PatternExcludeFilter();
179 ArtifactFilter filter = patternExcludeFilter.createFilter(excludes);
180
181 for (Iterator<?> depArtIter = depArtifacts.iterator(); depArtIter.hasNext(); ) {
182 Artifact depArt = (Artifact) depArtIter.next();
183
184 if (!filter.include(depArt)) {
185 if (isVerbose()) {
186 getLog().warn("The artifact: " + depArt.getId() + " has been execluded by the configuration.");
187 }
188 continue;
189 }
190
191 LicenseInformation li = new LicenseInformation();
192
193
194
195
196
197
198
199
200 li.setArtifact(depArt);
201 MavenProject depProject = null;
202 try
203 {
204 depProject = projectBuilder.buildFromRepository(depArt, remoteRepositories, localRepository, true);
205 }
206 catch (ProjectBuildingException e)
207 {
208 throw new MojoExecutionException( "Unable to build project: " + depArt.getDependencyConflictId(), e );
209 }
210
211
212
213 li.setProject(depProject);
214
215
216 List<?> licenses = depProject.getLicenses();
217 Iterator<?> licenseIter = licenses.iterator();
218 while (licenseIter.hasNext())
219 {
220 License license = (License) licenseIter.next();
221 li.addLicense(license);
222 }
223 licenseInformations.add(li);
224 }
225 }
226
227 public HashMap<String, LicenseInformation> getLicenseList() {
228 return licenseList;
229 }
230
231 public ArrayList<LicenseInformation> getLicenseInformations() {
232 return licenseInformations;
233 }
234
235
236
237
238
239
240 protected void loadLicensesFile() throws MojoExecutionException {
241 if (licenseFile == null)
242 {
243
244 return;
245 }
246 try {
247 getLog().debug("Trying to find " + licenseFile.getAbsolutePath() + " in file system.");
248 if (licenseFile.exists()) {
249 getLog().debug("Found licenses file in file system.");
250 getLog().info("Loading " + licenseFile.getAbsolutePath() + " licenses file.");
251 licensesContainer = LicensesFile.getLicenses(licenseFile);
252 } else {
253 getLog().info("Loading license file via classpath.");
254 URL licenseURL = this.getClass().getResource(licenseFile.getPath());
255 InputStream inputStream = null;
256 if (licenseURL == null) {
257 inputStream = this.getClass().getResourceAsStream("/licenses/licenses.xml");
258 licenseURL = this.getClass().getResource("/licenses/licenses.xml");
259 } else {
260 inputStream = this.getClass().getResourceAsStream(licenseFile.getPath());
261 licenseURL = this.getClass().getResource(licenseFile.getPath());
262 }
263 getLog().debug("Loading licenses.xml from " + licenseURL);
264 licensesContainer = LicensesFile.getLicenses(inputStream);
265 }
266 licenseValidator = new LicenseValidator(licensesContainer);
267 licenseValidator.setStrictChecking(stricktChecking);
268
269 } catch (IOException e) {
270
271 throw new MojoExecutionException(
272 "The LicenseFile configuration is wrong, " +
273 "cause we couldn't find the " + licenseFile.getAbsolutePath());
274 } catch (XmlPullParserException e) {
275
276 throw new MojoExecutionException(
277 "The LicenseFile is wrong, " +
278 "cause we couldn't read it " + e);
279 }
280 }
281
282 public void setVerbose(boolean verbose) {
283 this.verbose = verbose;
284 }
285
286 public boolean isVerbose() {
287 return verbose;
288 }
289 }