View Javadoc

1   package com.atlassian.maven.plugins.licenses;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.apache.maven.artifact.Artifact;
8   import org.apache.maven.model.License;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.project.MavenProject;
12  
13  /**
14   * Simply fails the build if it can't find a license for one or more
15   * dependencies of the project.
16   * 
17   * @goal validate
18   * @author Sherali Karimov
19   */
20  public class ValidateMojo extends AbstractLicensesMojo
21  {
22      public void execute() throws MojoExecutionException, MojoFailureException
23      {
24          final List unlicensedDependencies = new ArrayList();
25  
26          visitDependencyProjects(new ProjectVisitor()
27          {
28              public void onInit(int numDeps)
29              {/* nothing to do */
30              }
31  
32              public void onComplete()
33              {/* nothing to do */
34              }
35  
36              public void visit(MavenProject depProject)
37              {
38                  List licenses = depProject.getLicenses();
39                  Artifact artifact = depProject.getArtifact();
40                  if (getLog().isDebugEnabled())
41                  {
42                      getLog().debug("Validating " + artifact);
43                  }
44  
45                  if (licenses.isEmpty())
46                  {
47                      // try to see if there is a license in the JAR file or not
48                      if (createValidLicenseUrl(null, artifact) == null)
49                          addUnlicensedDependency(artifact);
50                      return;
51                  }
52  
53                  for (Iterator licIter = licenses.iterator(); licIter.hasNext();)
54                  {
55                      License license = (License) licIter.next();
56                      if (createValidLicenseUrl(license, artifact) != null)
57                          return; // if any one of these licenses is valid - we
58                                  // are fine with this project
59                  }
60                  // if we get here - licenses were invalid
61                  addUnlicensedDependency(artifact);
62              }
63  
64              private void addUnlicensedDependency(Artifact artifact)
65              {
66                  unlicensedDependencies.add(artifact);
67                  getLog().error("License not found for: " + artifact);
68              }
69          });
70  
71          if (!unlicensedDependencies.isEmpty())
72              throw new MojoFailureException(toValidationError(unlicensedDependencies));
73      }
74  
75      private String toValidationError(final List unlicensedDependencies)
76      {
77          StringBuffer buffer = new StringBuffer("The license details could not be found for the folowing libraries:");
78          for (Iterator iterator = unlicensedDependencies.iterator(); iterator.hasNext();)
79          {
80              Artifact artifact = (Artifact) iterator.next();
81              buffer.append("\n\t").append(artifact);
82          }
83          return buffer.toString();
84      }
85  }