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