001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.license;
017    
018    import java.io.File;
019    import java.io.IOException;
020    import java.util.List;
021    import java.util.SortedMap;
022    import java.util.SortedSet;
023    
024    import org.apache.commons.collections.CollectionUtils;
025    import org.apache.maven.plugin.MojoFailureException;
026    import org.apache.maven.plugin.logging.Log;
027    import org.apache.maven.project.MavenProject;
028    import org.apache.maven.project.ProjectBuildingException;
029    import org.codehaus.mojo.license.model.LicenseMap;
030    
031    /**
032     * This aggregator goal (will be executed only once and only on pom projects) executed the {@code add-third-party} on
033     * all his modules (in a parellel build cycle) then aggreates all the third-party files in final one in the pom project.
034     *
035     * @author tchemit <chemit@codelutin.com>
036     * @goal aggregate-add-third-party
037     * @phase generate-resources
038     * @requiresProject true
039     * @aggregator
040     * @execute goal="add-third-party"
041     * @since 1.0
042     */
043    public class AggregatorAddThirdPartyMojo extends AbstractAddThirdPartyMojo {
044    
045        /**
046         * The projects in the reactor.
047         *
048         * @parameter expression="${reactorProjects}"
049         * @readonly
050         * @required
051         * @since 1.0
052         */
053        protected List<?> reactorProjects;
054    
055        @Override
056        protected boolean checkPackaging() {
057            return acceptPackaging("pom");
058        }
059    
060        @Override
061        protected boolean checkSkip() {
062            if (!isDoGenerate() && !isDoGenerateBundle()) {
063    
064                getLog().info("All files are up to date, skip goal execution.");
065                return false;
066            }
067            return super.checkSkip();
068        }
069    
070        @Override
071        protected SortedMap<String, MavenProject> loadDependencies() {
072            // use the cache filled by modules in reactor
073            return getArtifactCache();
074        }
075    
076        @Override
077        protected SortedProperties createUnsafeMapping() throws ProjectBuildingException, IOException {
078    
079            String path = getMissingFile().getAbsolutePath().substring(
080                    getProject().getBasedir().getAbsolutePath().length() + 1);
081    
082            if (isVerbose()) {
083                getLog().info("Use missing file path : " + path);
084            }
085    
086            SortedProperties unsafeMappings = new SortedProperties(getEncoding());
087    
088            LicenseMap licenseMap = getLicenseMap();
089    
090            for (Object o : reactorProjects) {
091                MavenProject p = (MavenProject) o;
092    
093                File file = new File(p.getBasedir(), path);
094    
095                if (file.exists()) {
096    
097                    SortedProperties tmp = getThirdPartyTool().loadUnsafeMapping(licenseMap, getArtifactCache(),
098                            getEncoding(), file);
099                    unsafeMappings.putAll(tmp);
100                }
101    
102                SortedSet<MavenProject> unsafes = getThirdPartyTool().getProjectsWithNoLicense(licenseMap, isVerbose());
103                if (CollectionUtils.isEmpty(unsafes)) {
104    
105                    // no more unsafe dependencies, can break
106                    break;
107                }
108            }
109            return unsafeMappings;
110        }
111    
112        @Override
113        protected void doAction() throws Exception {
114            Log log = getLog();
115    
116            if (exists(getArtifactLicenseMapping())) {
117                File propertiesFile = copyToFileSystem(getArtifactLicenseMapping());
118                setMissingFile(propertiesFile);
119            }
120    
121            if (isVerbose()) {
122                log.info("After executing on " + reactorProjects.size() + " project(s)");
123            }
124            SortedMap<String, MavenProject> artifacts = getArtifactCache();
125    
126            LicenseMap licenseMap = getLicenseMap();
127    
128            getLog().info(artifacts.size() + " detected artifact(s).");
129            if (isVerbose()) {
130                for (String id : artifacts.keySet()) {
131                    getLog().info(" - " + id);
132                }
133            }
134            getLog().info(licenseMap.size() + " detected license(s).");
135            if (isVerbose()) {
136                for (String id : licenseMap.keySet()) {
137                    getLog().info(" - " + id);
138                }
139            }
140            boolean unsafe = checkUnsafeDependencies();
141    
142            writeThirdPartyFile();
143    
144            if (unsafe && isFailIfWarning()) {
145                throw new MojoFailureException("There is some dependencies with no license, please review the modules.");
146            }
147        }
148    
149    }