View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.List;
21  import java.util.SortedMap;
22  import java.util.SortedSet;
23  
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.project.ProjectBuildingException;
29  import org.codehaus.mojo.license.model.LicenseMap;
30  
31  /**
32   * This aggregator goal (will be executed only once and only on pom projects) executed the {@code add-third-party} on
33   * all his modules (in a parellel build cycle) then aggreates all the third-party files in final one in the pom project.
34   *
35   * @author tchemit <chemit@codelutin.com>
36   * @goal aggregate-add-third-party
37   * @phase generate-resources
38   * @requiresProject true
39   * @aggregator
40   * @execute goal="add-third-party"
41   * @since 1.0
42   */
43  public class AggregatorAddThirdPartyMojo extends AbstractAddThirdPartyMojo {
44  
45      /**
46       * The projects in the reactor.
47       *
48       * @parameter expression="${reactorProjects}"
49       * @readonly
50       * @required
51       * @since 1.0
52       */
53      protected List<?> reactorProjects;
54  
55      @Override
56      protected boolean checkPackaging() {
57          return acceptPackaging("pom");
58      }
59  
60      @Override
61      protected boolean checkSkip() {
62          if (!isDoGenerate() && !isDoGenerateBundle()) {
63  
64              getLog().info("All files are up to date, skip goal execution.");
65              return false;
66          }
67          return super.checkSkip();
68      }
69  
70      @Override
71      protected SortedMap<String, MavenProject> loadDependencies() {
72          // use the cache filled by modules in reactor
73          return getArtifactCache();
74      }
75  
76      @Override
77      protected SortedProperties createUnsafeMapping() throws ProjectBuildingException, IOException {
78  
79          String path = getMissingFile().getAbsolutePath().substring(
80                  getProject().getBasedir().getAbsolutePath().length() + 1);
81  
82          if (isVerbose()) {
83              getLog().info("Use missing file path : " + path);
84          }
85  
86          SortedProperties unsafeMappings = new SortedProperties(getEncoding());
87  
88          LicenseMap licenseMap = getLicenseMap();
89  
90          for (Object o : reactorProjects) {
91              MavenProject p = (MavenProject) o;
92  
93              File file = new File(p.getBasedir(), path);
94  
95              if (file.exists()) {
96  
97                  SortedProperties tmp = getThirdPartyTool().loadUnsafeMapping(licenseMap, getArtifactCache(),
98                          getEncoding(), file);
99                  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 }