001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.maven.plugin.legal;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.net.URL;
022    
023    import org.apache.maven.model.Resource;
024    import org.apache.maven.plugin.AbstractMojo;
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.maven.plugin.MojoFailureException;
027    import org.apache.maven.project.MavenProject;
028    import org.codehaus.plexus.util.FileUtils;
029    
030    /**
031     *
032     * @goal copy
033     * @phase generate-sources
034     */
035    public class LegalMojo extends AbstractMojo {
036    
037        /**
038         * The maven project.
039         * 
040         * @parameter expression="${project}"
041         * @required
042         * @readonly
043         */
044        protected MavenProject project;
045        
046        /**
047         * @parameter
048         */         
049        protected File outputDir;
050    
051        public void execute() throws MojoExecutionException, MojoFailureException {
052            try {
053                if (outputDir != null) {
054                    copyLegalFiles(outputDir);
055                } else if (project.getPackaging().equals("jar")) {
056                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
057                } else if (project.getPackaging().equals("maven-plugin")) {
058                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
059                } else if (project.getPackaging().equals("war")) {
060                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "/"));
061                } else if (project.getPackaging().equals("jbi-shared-library")) {
062                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
063                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
064                } else if (project.getPackaging().equals("jbi-component")) {
065                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
066                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
067                } else if (project.getPackaging().equals("jbi-service-unit")) {
068                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
069                } else if (project.getPackaging().equals("jbi-service-assembly")) {
070                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
071                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
072                }
073    
074                File outDir = new File(project.getBasedir(), "target/maven-shared-archive-resources");
075                copyLegalFiles(outDir);
076                Resource resource = new Resource();
077                resource.setDirectory(outDir.getAbsolutePath());
078                project.getResources().add(resource);
079                project.getTestResources().add(resource);
080                
081            } catch (IOException e) {
082                throw new MojoExecutionException("Unable to copy legal files", e);
083            }
084        }
085    
086        protected void copyLegalFiles(File outputDir) throws IOException {
087            String[] names = { "/META-INF/NOTICE", "/META-INF/LICENSE"};
088            for (int i = 0; i < names.length; i++) {
089                URL res = getClass().getResource(names[i]);
090                FileUtils.copyURLToFile(res, new File(outputDir, names[i]));
091            }
092        }
093    
094    }