001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.genesis.plugins.tools;
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.List;
025    import java.util.Iterator;
026    import java.util.ArrayList;
027    import java.util.zip.ZipFile;
028    import java.util.zip.ZipEntry;
029    import java.util.zip.ZipException;
030    
031    import org.apache.maven.project.MavenProject;
032    import org.apache.maven.plugin.MojoExecutionException;
033    import org.apache.maven.artifact.Artifact;
034    
035    import org.apache.geronimo.genesis.MojoSupport;
036    
037    /**
038     * Verify (kinda) that legal files are in all attached zip-encoded artifacts.
039     *
040     * @goal verify-legal-files
041     * @phase verify
042     *
043     * @version $Rev: 486868 $ $Date: 2006-12-13 14:25:38 -0800 (Wed, 13 Dec 2006) $
044     */
045    public class VerifyLegalFilesMojo
046        extends MojoSupport
047    {
048        /**
049         * The default required legal files.
050         */
051        private static final String[] DEFAULT_REQUIRED_FILES = {
052            "LICENSE.txt",
053            "NOTICE.txt"
054        };
055    
056        /**
057         * When set to true, fail the build when no legal files are found.
058         *
059         * @parameter default-value="false"
060         */
061        private boolean strict;
062    
063    
064        /**
065         * The list of required legal files.
066         *
067         * @parameter
068         */
069        private String[] requiredFiles = DEFAULT_REQUIRED_FILES;
070    
071        /**
072         * The maven project.
073         *
074         * @parameter expression="${project}"
075         * @required
076         * @readonly
077         */
078        protected MavenProject project = null;
079    
080        protected void doExecute() throws Exception {
081            List artifacts = new ArrayList();
082            artifacts.add(project.getArtifact());
083            artifacts.addAll(project.getAttachedArtifacts());
084    
085            Iterator iter = artifacts.iterator();
086            while (iter.hasNext()) {
087                Artifact artifact = (Artifact)iter.next();
088                File file = artifact.getFile();
089    
090                // Some artifacts might not have files, so skip them
091                if (file == null) {
092                    log.debug("Skipping artifact; no attached file: " + artifact);
093                    continue;
094                }
095    
096                try {
097                    ZipFile zfile = new ZipFile(file);
098    
099                    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    }