View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    *
5    * $Id: AggregatorAddThirdPartyMojo.java 14409 2011-08-10 15:30:41Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as
12   * published by the Free Software Foundation, either version 3 of the
13   * License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   *
20   * You should have received a copy of the GNU General Lesser Public
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  
26  package org.codehaus.mojo.license;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  import java.util.SortedMap;
32  import java.util.SortedSet;
33  
34  import org.apache.commons.collections.CollectionUtils;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.ProjectBuildingException;
39  import org.codehaus.mojo.license.model.LicenseMap;
40  
41  /**
42   * This aggregator goal (will be executed only once and only on pom projects) executed the {@code add-third-party} on
43   * all his modules (in a parellel build cycle) then aggreates all the third-party files in final one in the pom project.
44   *
45   * @author tchemit <chemit@codelutin.com>
46   * @goal aggregate-add-third-party
47   * @phase generate-resources
48   * @requiresProject true
49   * @aggregator
50   * @execute goal="add-third-party"
51   * @since 1.0
52   */
53  public class AggregatorAddThirdPartyMojo extends AbstractAddThirdPartyMojo {
54  
55      /**
56       * The projects in the reactor.
57       *
58       * @parameter expression="${reactorProjects}"
59       * @readonly
60       * @required
61       * @since 1.0
62       */
63      protected List<?> reactorProjects;
64  
65      @Override
66      protected boolean checkPackaging() {
67          return acceptPackaging("pom");
68      }
69  
70      @Override
71      protected boolean checkSkip() {
72          if (!isDoGenerate() && !isDoGenerateBundle()) {
73  
74              getLog().info("All files are up to date, skip goal execution.");
75              return false;
76          }
77          return super.checkSkip();
78      }
79  
80      @Override
81      protected SortedMap<String, MavenProject> loadDependencies() {
82          // use the cache filled by modules in reactor
83          return getArtifactCache();
84      }
85  
86      @Override
87      protected SortedProperties createUnsafeMapping() throws ProjectBuildingException, IOException {
88  
89          String path = getMissingFile().getAbsolutePath().substring(
90                  getProject().getBasedir().getAbsolutePath().length() + 1);
91  
92          if (isVerbose()) {
93              getLog().info("Use missing file path : " + path);
94          }
95  
96          SortedProperties unsafeMappings = new SortedProperties(getEncoding());
97  
98          LicenseMap licenseMap = getLicenseMap();
99  
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 }