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.plugin.AbstractMojo;
024    import org.apache.maven.plugin.MojoExecutionException;
025    import org.apache.maven.plugin.MojoFailureException;
026    import org.apache.maven.project.MavenProject;
027    import org.codehaus.plexus.util.FileUtils;
028    
029    /**
030     *
031     * @goal copy
032     * @phase generate-resources
033     */
034    public class LegalMojo extends AbstractMojo {
035    
036        /**
037         * The maven project.
038         * 
039         * @parameter expression="${project}"
040         * @required
041         * @readonly
042         */
043        protected MavenProject project;
044        
045        /**
046         * @parameter
047         */         
048        protected File outputDir;
049    
050        public void execute() throws MojoExecutionException, MojoFailureException {
051            try {
052                if (outputDir != null) {
053                    copyLegalFiles(outputDir);
054                } else if (project.getPackaging().equals("jar")) {
055                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
056                } else if (project.getPackaging().equals("maven-plugin")) {
057                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
058                } else if (project.getPackaging().equals("war")) {
059                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "/"));
060                } else if (project.getPackaging().equals("jbi-shared-library")) {
061                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
062                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
063                } else if (project.getPackaging().equals("jbi-component")) {
064                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
065                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
066                } else if (project.getPackaging().equals("jbi-service-unit")) {
067                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
068                } else if (project.getPackaging().equals("jbi-service-assembly")) {
069                    copyLegalFiles(new File(project.getBasedir(), "target/classes/"));
070                    copyLegalFiles(new File(project.getBasedir(), "target/" + project.getArtifactId() + "-" + project.getVersion() + "-installer/"));
071                }
072            } catch (IOException e) {
073                throw new MojoExecutionException("Unable to copy legal files", e);
074            }
075            }
076            
077            protected void copyLegalFiles(File outputDir) throws IOException {
078            String[] names = { "/META-INF/NOTICE", "/META-INF/LICENSE"};
079            for (int i = 0; i < names.length; i++) {
080                URL res = getClass().getResource(names[i]);
081                FileUtils.copyURLToFile(res, new File(outputDir, names[i]));
082            }
083        }
084    
085    }