1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Enumeration;
24 import java.util.List;
25 import java.util.jar.JarEntry;
26 import java.util.jar.JarFile;
27
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.Component;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.dependency.utils.ParamArtifact;
35 import org.apache.maven.plugins.dependency.utils.ResolverUtil;
36 import org.eclipse.aether.artifact.Artifact;
37 import org.eclipse.aether.resolution.ArtifactResolutionException;
38 import org.eclipse.aether.resolution.DependencyResolutionException;
39
40
41
42
43
44
45 @Mojo(name = "list-classes", requiresProject = false, threadSafe = true)
46 public class ListClassesMojo extends AbstractMojo {
47
48 @Component
49 private ResolverUtil resolverUtil;
50
51 private ParamArtifact paramArtifact = new ParamArtifact();
52
53
54
55
56
57
58 @Parameter(property = "groupId")
59 public void setGroupId(String groupId) {
60 paramArtifact.setGroupId(groupId);
61 }
62
63
64
65
66
67
68 @Parameter(property = "artifactId")
69 public void setArtifactId(String artifactId) {
70 paramArtifact.setArtifactId(artifactId);
71 }
72
73
74
75
76
77
78 @Parameter(property = "version")
79 public void setVersion(String version) {
80 paramArtifact.setVersion(version);
81 }
82
83
84
85
86
87
88 @Parameter(property = "classifier")
89 public void setClassifier(String classifier) {
90 paramArtifact.setClassifier(classifier);
91 }
92
93
94
95
96
97
98 @Parameter(property = "packaging", defaultValue = "jar")
99 public void setPackaging(String packaging) {
100 paramArtifact.setPackaging(packaging);
101 }
102
103
104
105
106
107
108 @Parameter(property = "artifact")
109 public void setArtifact(String artifact) {
110 paramArtifact.setArtifact(artifact);
111 }
112
113
114
115
116
117
118
119
120
121 @Parameter(property = "remoteRepositories")
122 private List<String> remoteRepositories;
123
124
125
126
127
128
129 @Parameter(property = "transitive", defaultValue = "false")
130 private boolean transitive = false;
131
132
133
134
135
136
137 @Parameter(property = "mdep.skip", defaultValue = "false")
138 private boolean skip;
139
140 @Override
141 public void execute() throws MojoExecutionException, MojoFailureException {
142 if (skip) {
143 getLog().info("Skipping plugin execution");
144 return;
145 }
146
147 if (!paramArtifact.isDataSet()) {
148 throw new MojoExecutionException("You must specify an artifact OR GAV separately, "
149 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0 OR "
150 + "-DgroupId=org.apache.maven.plugins -DartifactId=maven-downloader-plugin -Dversion=1.0");
151 }
152
153 Artifact artifact = resolverUtil.createArtifactFromParams(paramArtifact);
154
155 try {
156 if (transitive) {
157 List<Artifact> artifacts =
158 resolverUtil.resolveDependencies(artifact, resolverUtil.remoteRepositories(remoteRepositories));
159
160 for (Artifact a : artifacts) {
161 printClassesFromArtifactResult(a.getFile());
162 }
163 } else {
164 Artifact a =
165 resolverUtil.resolveArtifact(artifact, resolverUtil.remoteRepositories(remoteRepositories));
166 printClassesFromArtifactResult(a.getFile());
167 }
168 } catch (IOException | ArtifactResolutionException | DependencyResolutionException e) {
169 throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
170 }
171 }
172
173 private void printClassesFromArtifactResult(File file) throws IOException {
174
175 try (JarFile jarFile = new JarFile(file)) {
176 Enumeration<JarEntry> entries = jarFile.entries();
177
178 while (entries.hasMoreElements()) {
179 JarEntry entry = entries.nextElement();
180 String entryName = entry.getName();
181
182
183 if (!entryName.endsWith(".class")) {
184 continue;
185 }
186
187
188 String className =
189 entryName.substring(0, entryName.length() - 6).replace('/', '.');
190 getLog().info(className);
191 }
192 }
193 }
194 }