View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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              // Some artifacts might not have files, so skip them
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 }