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    
024    import org.codehaus.plexus.util.DirectoryScanner;
025    
026    import org.apache.maven.plugin.MojoExecutionException;
027    import org.apache.maven.plugin.MojoFailureException;
028    import org.apache.maven.project.MavenProject;
029    import org.apache.tools.ant.taskdefs.Copy;
030    import org.apache.tools.ant.types.FileSet;
031    
032    import org.apache.geronimo.genesis.MojoSupport;
033    import org.apache.geronimo.genesis.ant.AntHelper;
034    
035    /**
036     * Copy legal files for inclusion into generated jars.
037     *
038     * @goal copy-legal-files
039     * @phase validate
040     *
041     * @version $Rev: 486420 $ $Date: 2006-12-12 15:51:25 -0800 (Tue, 12 Dec 2006) $
042     */
043    public class CopyLegalFilesMojo
044        extends MojoSupport
045    {
046        /**
047         * The default includes when no fileset is configured.
048         */
049        private static final String[] DEFAULT_INCLUDES = {
050            "LICENSE.txt",
051            "LICENSE",
052            "NOTICE.txt",
053            "NOTICE",
054            "DISCLAIMER.txt",
055            "DISCLAIMER"
056        };
057    
058        /**
059         * Directory to copy legal files into.
060         *
061         * @parameter expression="${project.build.outputDirectory}/META-INF"
062         * @required
063         */
064        private File outputDirectory = null;
065    
066        /**
067         * The basedir of the project.
068         *
069         * @parameter expression="${basedir}"
070         * @required
071         * @readonly
072         */
073        protected File basedir;
074    
075        /**
076         * The set of legal files to be copied.  Default fileset includes: LICENSE[.txt], NOTICE[.txt] and DISCLAIMER[.txt].
077         *
078         * @parameter
079         */
080        private DirectoryScanner fileset;
081        
082        /**
083         * When set to true, fail the build when no legal files are found.
084         *
085         * @parameter default-value="false"
086         */
087        private boolean strict;
088        
089        /**
090         * @component
091         */
092        private AntHelper ant;
093    
094        //
095        // MojoSupport Hooks
096        //
097    
098        /**
099         * The maven project.
100         *
101         * @parameter expression="${project}"
102         * @required
103         * @readonly
104         */
105        protected MavenProject project = null;
106    
107        protected MavenProject getProject() {
108            return project;
109        }
110    
111        //
112        // Mojo
113        //
114    
115        protected void init() throws MojoExecutionException, MojoFailureException {
116            super.init();
117    
118            ant.setProject(getProject());
119        }
120    
121        protected void doExecute() throws Exception {
122            if (!shouldInstallLegalFiles(getProject())) {
123                return;
124            }
125    
126            if (fileset == null) {
127                fileset = new DirectoryScanner();
128                fileset.setBasedir(basedir);
129                fileset.setIncludes(DEFAULT_INCLUDES);
130            }
131    
132            fileset.addDefaultExcludes();
133            fileset.scan();
134    
135            String[] filenames = fileset.getIncludedFiles();
136    
137            if (filenames.length == 0) {
138                if (strict) {
139                    throw new MojoExecutionException("No legal files found to copy");
140                }
141                else {
142                    log.warn("No legal files found to copy");
143                }
144                
145                return;
146            }
147    
148            ant.mkdir(outputDirectory);
149    
150            Copy copy = (Copy)ant.createTask("copy");
151            copy.setTodir(outputDirectory);
152    
153            FileSet files = ant.createFileSet();
154            files.setDir(basedir);
155    
156            for (int i=0; i<filenames.length; i++) {
157                files.createInclude().setName(filenames[i]);
158            }
159    
160            copy.addFileset(files);
161    
162            copy.execute();
163        }
164    
165        private boolean shouldInstallLegalFiles(final MavenProject project) {
166            assert project != null;
167    
168            //
169            // TODO: Expose a list of packagings
170            //
171            
172            return !"pom".equals(getProject().getPackaging());
173        }
174    }