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
13
14
15
16
17
18
19
20
21 public class ListMojo extends AbstractLicensesMojo
22 {
23
24
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 {
64 }
65
66 public void onInit(int numDeps)
67 {
68 }
69 }
70
71 private class MarkupListPrinter implements ProjectVisitor
72 {
73 private final StringBuffer buf = new StringBuffer();
74
75
76
77
78
79
80
81
82
83
84 public void visit(MavenProject depProject)
85 {
86 List licenses = depProject.getLicenses();
87
88 buf.append('|');
89 buf.append(depProject.getGroupId());
90
91
92 buf.append('|');
93 if(depProject.getUrl() != null)
94 {
95
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
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())
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 {
150 }
151 }
152 }