001    /*
002     * #%L
003     * License Maven Plugin
004     *
005     * $Id: AggregatorAddThirdPartyMojo.java 14409 2011-08-10 15:30:41Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java $
007     * %%
008     * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as
012     * published by the Free Software Foundation, either version 3 of the
013     * License, or (at your option) any later version.
014     *
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     *
020     * You should have received a copy of the GNU General Lesser Public
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    
026    package org.codehaus.mojo.license;
027    
028    import java.io.File;
029    import java.io.IOException;
030    import java.util.List;
031    import java.util.SortedMap;
032    import java.util.SortedSet;
033    
034    import org.apache.commons.collections.CollectionUtils;
035    import org.apache.maven.plugin.MojoFailureException;
036    import org.apache.maven.plugin.logging.Log;
037    import org.apache.maven.project.MavenProject;
038    import org.apache.maven.project.ProjectBuildingException;
039    import org.codehaus.mojo.license.model.LicenseMap;
040    
041    /**
042     * This aggregator goal (will be executed only once and only on pom projects) executed the {@code add-third-party} on
043     * all his modules (in a parellel build cycle) then aggreates all the third-party files in final one in the pom project.
044     *
045     * @author tchemit <chemit@codelutin.com>
046     * @goal aggregate-add-third-party
047     * @phase generate-resources
048     * @requiresProject true
049     * @aggregator
050     * @execute goal="add-third-party"
051     * @since 1.0
052     */
053    public class AggregatorAddThirdPartyMojo extends AbstractAddThirdPartyMojo {
054    
055        /**
056         * The projects in the reactor.
057         *
058         * @parameter expression="${reactorProjects}"
059         * @readonly
060         * @required
061         * @since 1.0
062         */
063        protected List<?> reactorProjects;
064    
065        @Override
066        protected boolean checkPackaging() {
067            return acceptPackaging("pom");
068        }
069    
070        @Override
071        protected boolean checkSkip() {
072            if (!isDoGenerate() && !isDoGenerateBundle()) {
073    
074                getLog().info("All files are up to date, skip goal execution.");
075                return false;
076            }
077            return super.checkSkip();
078        }
079    
080        @Override
081        protected SortedMap<String, MavenProject> loadDependencies() {
082            // use the cache filled by modules in reactor
083            return getArtifactCache();
084        }
085    
086        @Override
087        protected SortedProperties createUnsafeMapping() throws ProjectBuildingException, IOException {
088    
089            String path = getMissingFile().getAbsolutePath().substring(
090                    getProject().getBasedir().getAbsolutePath().length() + 1);
091    
092            if (isVerbose()) {
093                getLog().info("Use missing file path : " + path);
094            }
095    
096            SortedProperties unsafeMappings = new SortedProperties(getEncoding());
097    
098            LicenseMap licenseMap = getLicenseMap();
099    
100            for (Object o : reactorProjects) {
101                MavenProject p = (MavenProject) o;
102    
103                File file = new File(p.getBasedir(), path);
104    
105                if (file.exists()) {
106    
107                    SortedProperties tmp = getThridPartyTool().loadUnsafeMapping(licenseMap, getArtifactCache(),
108                            getEncoding(), file);
109                    unsafeMappings.putAll(tmp);
110                }
111    
112                SortedSet<MavenProject> unsafes = getThridPartyTool().getProjectsWithNoLicense(licenseMap, isVerbose());
113                if (CollectionUtils.isEmpty(unsafes)) {
114    
115                    // no more unsafe dependencies, can break
116                    break;
117                }
118            }
119            return unsafeMappings;
120        }
121    
122        @Override
123        protected void doAction() throws Exception {
124            Log log = getLog();
125    
126            if (exists(getArtifactLicenseMapping())) {
127                File propertiesFile = copyToFileSystem(getArtifactLicenseMapping());
128                setMissingFile(propertiesFile);
129            }
130    
131            if (isVerbose()) {
132                log.info("After executing on " + reactorProjects.size() + " project(s)");
133            }
134            SortedMap<String, MavenProject> artifacts = getArtifactCache();
135    
136            LicenseMap licenseMap = getLicenseMap();
137    
138            getLog().info(artifacts.size() + " detected artifact(s).");
139            if (isVerbose()) {
140                for (String id : artifacts.keySet()) {
141                    getLog().info(" - " + id);
142                }
143            }
144            getLog().info(licenseMap.size() + " detected license(s).");
145            if (isVerbose()) {
146                for (String id : licenseMap.keySet()) {
147                    getLog().info(" - " + id);
148                }
149            }
150            boolean unsafe = checkUnsafeDependencies();
151    
152            writeThirdPartyFile();
153    
154            if (unsafe && isFailIfWarning()) {
155                throw new MojoFailureException("There is some dependencies with no license, please review the modules.");
156            }
157        }
158    
159    }