1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.genesis.plugins.tools;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Iterator;
26 import java.util.ArrayList;
27 import java.util.zip.ZipFile;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipException;
30
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.artifact.Artifact;
34
35 import org.apache.geronimo.genesis.MojoSupport;
36
37 /**
38 * Verify (kinda) that legal files are in all attached zip-encoded artifacts.
39 *
40 * @goal verify-legal-files
41 * @phase verify
42 *
43 * @version $Rev: 486868 $ $Date: 2006-12-13 14:25:38 -0800 (Wed, 13 Dec 2006) $
44 */
45 public class VerifyLegalFilesMojo
46 extends MojoSupport
47 {
48 /**
49 * The default required legal files.
50 */
51 private static final String[] DEFAULT_REQUIRED_FILES = {
52 "LICENSE.txt",
53 "NOTICE.txt"
54 };
55
56 /**
57 * When set to true, fail the build when no legal files are found.
58 *
59 * @parameter default-value="false"
60 */
61 private boolean strict;
62
63
64 /**
65 * The list of required legal files.
66 *
67 * @parameter
68 */
69 private String[] requiredFiles = DEFAULT_REQUIRED_FILES;
70
71 /**
72 * The maven project.
73 *
74 * @parameter expression="${project}"
75 * @required
76 * @readonly
77 */
78 protected MavenProject project = null;
79
80 protected void doExecute() throws Exception {
81 List artifacts = new ArrayList();
82 artifacts.add(project.getArtifact());
83 artifacts.addAll(project.getAttachedArtifacts());
84
85 Iterator iter = artifacts.iterator();
86 while (iter.hasNext()) {
87 Artifact artifact = (Artifact)iter.next();
88 File file = artifact.getFile();
89
90
91 if (file == null) {
92 log.debug("Skipping artifact; no attached file: " + artifact);
93 continue;
94 }
95
96 try {
97 ZipFile zfile = new ZipFile(file);
98
99 log.info("Checking legal files in: " + file.getName());
100
101 if (!containsLegalFiles(zfile)) {
102 String msg = "Artifact does not contain any legal files: " + file.getName();
103 if (strict) {
104 throw new MojoExecutionException(msg);
105 }
106 else {
107 log.warn(msg);
108 }
109 }
110 }
111 catch (ZipException e) {
112 log.debug("Failed to check file for legal muck; ignoring: " + file, e);
113 }
114 }
115 }
116
117 private boolean containsLegalFiles(final ZipFile file) throws IOException {
118 assert file != null;
119
120 return containsLegalFiles(file, "META-INF") ||
121 containsLegalFiles(file, project.getArtifactId() + "-" + project.getVersion());
122 }
123
124 private boolean containsLegalFiles(final ZipFile file, final String basedir) {
125 assert file != null;
126 assert basedir != null;
127
128 for (int i=0; i < requiredFiles.length; i++) {
129 String filename = basedir + "/" + requiredFiles[i];
130 log.debug("Checking for: " + filename);
131
132 ZipEntry entry = file.getEntry(filename);
133 if (entry == null) {
134 return false;
135 }
136 }
137
138 return true;
139 }
140 }