View Javadoc

1   package com.atlassian.maven.plugins.licenses;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   
6   import org.apache.maven.model.License;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.maven.plugin.MojoFailureException;
9   import org.apache.maven.project.MavenProject;
10  
11  /**
12   * Simply prints license information obtained from the POM or inferred from the
13   * JAR (i.e. URL to the license found inside the JAR file) for all the
14   * dependencies (including transitive) of the specified project.
15   * 
16   * In wiki markup mode - prints the table in Confluence wiki markup format as a table.
17   * 
18   * @goal list
19   * @author Sherali Karimov
20   */
21  public class ListMojo extends AbstractLicensesMojo
22  {
23      /**
24       * @parameter expression="${licenses.printAsWikiMarkup}" default-value="false"
25       */
26      private boolean printAsWikiMarkup;
27  
28      public void execute() throws MojoExecutionException, MojoFailureException
29      {
30          if (printAsWikiMarkup)
31              visitDependencyProjects(new MarkupListPrinter());
32          else
33              visitDependencyProjects(new RawListPrinter());
34      }
35  
36      private class RawListPrinter implements ProjectVisitor
37      {
38          public void visit(MavenProject depProject)
39          {
40              List licenses = depProject.getLicenses();
41              getLog().info("Artifact: " + depProject.getArtifact());
42              if (licenses.isEmpty())
43              {
44                  String url = createValidLicenseUrl(null, depProject.getArtifact());
45                  if (url == null)
46                      getLog().info("\tNO license information found.");
47                  else
48                      getLog().info("\tLicense found in the JAR: " + url);
49  
50                  return;
51              }
52  
53              for (Iterator licIter = licenses.iterator(); licIter.hasNext();)
54              {
55                  License license = (License) licIter.next();
56                  getLog().info("\tLicense 1");
57                  getLog().info("\t\tName: " + license.getName());
58                  getLog().info("\t\tURL: " + license.getUrl());
59              }
60          }
61  
62          public void onComplete()
63          {/* nothing to do, really */
64          }
65  
66          public void onInit(int numDeps)
67          {/* nothing to do, really */
68          }
69      }
70  
71      private class MarkupListPrinter implements ProjectVisitor
72      {
73          private final StringBuffer buf = new StringBuffer();
74  
75          /**
76           * Prints the list in a: 
77           * <em>||groupId||artifactId||Version||License||</em>
78           * form. 
79           * ArtifactId and License columns will contain the link to 
80           * the project and to the license where possible.
81           * 
82           * This method simply buffers the information and prints it all in one when all dependencies have been visited.
83           */
84          public void visit(MavenProject depProject)
85          {
86              List licenses = depProject.getLicenses();
87              // groupId
88              buf.append('|');
89              buf.append(depProject.getGroupId());
90  
91              // artifactId
92              buf.append('|');
93              if(depProject.getUrl() != null)
94              {
95                  // give the project a bit of Google linkage love
96                  buf.append('[');
97                  buf.append(depProject.getArtifactId());
98                  buf.append('|');
99                  buf.append(depProject.getUrl());
100                 buf.append(']');
101             }
102             else
103             {
104                 buf.append(depProject.getArtifactId());
105             }
106             
107             buf.append('|');
108             buf.append(depProject.getVersion());
109             buf.append('|');
110 
111             if (licenses.isEmpty())
112             {
113                 String url = createValidLicenseUrl(null, depProject.getArtifact());
114                 if (url == null)
115                     buf.append("N/A");
116                 else
117                     buf.append(url);
118             }
119             else
120             {
121                 // will print every license in a list on a new line including the link to it where possible.
122                 for (Iterator licIter = licenses.iterator(); licIter.hasNext();)
123                 {
124                     License license = (License) licIter.next();
125                     buf.append('[');
126                     buf.append(license.getName());
127                     buf.append('|');
128                     buf.append(license.getUrl());
129                     buf.append(']');
130                     
131                     if (licIter.hasNext()) // has more licenses
132                         buf.append('\\').append('\n');
133                 }
134             }
135             buf.append("|\n");
136         }
137 
138         public void onComplete()
139         {
140             if (buf.length() > 0)
141             {
142                 System.out.println("||groupId||artifactId||Version||License||");
143                 System.out.println(buf.toString());
144             }
145 
146         }
147 
148         public void onInit(int numDeps)
149         {/* nothing to do, really */
150         }
151     }
152 }