View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Retrieves and lists all classes contained in the specified artifact from the specified remote repositories.
43   *
44   * @since 3.1.3
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       * The group ID of the artifact to download. Ignored if {@code artifact} is used.
60       *
61       * @since 3.1.3
62       */
63      @Parameter(property = "groupId")
64      public void setGroupId(String groupId) {
65          paramArtifact.setGroupId(groupId);
66      }
67  
68      /**
69       * The artifact ID of the artifact to download. Ignored if {@code artifact} is used.
70       *
71       * @since 3.1.3
72       */
73      @Parameter(property = "artifactId")
74      public void setArtifactId(String artifactId) {
75          paramArtifact.setArtifactId(artifactId);
76      }
77  
78      /**
79       * The version of the artifact to download. Ignored if {@code artifact} is used.
80       *
81       * @since 3.1.3
82       */
83      @Parameter(property = "version")
84      public void setVersion(String version) {
85          paramArtifact.setVersion(version);
86      }
87  
88      /**
89       * The classifier of the artifact to download. Ignored if {@code artifact} is used.
90       *
91       * @since 3.1.3
92       */
93      @Parameter(property = "classifier")
94      public void setClassifier(String classifier) {
95          paramArtifact.setClassifier(classifier);
96      }
97  
98      /**
99       * The packaging of the artifact to download. Ignored if {@code artifact} is used.
100      *
101      * @since 3.1.3
102      */
103     @Parameter(property = "packaging", defaultValue = "jar")
104     public void setPackaging(String packaging) {
105         paramArtifact.setPackaging(packaging);
106     }
107 
108     /**
109      * A string of the form {@code groupId:artifactId:version[:packaging[:classifier]]}.
110      *
111      * @since 3.1.3
112      */
113     @Parameter(property = "artifact")
114     public void setArtifact(String artifact) {
115         paramArtifact.setArtifact(artifact);
116     }
117 
118     /**
119      * Repositories in the format {@code id::[layout::]url} or just URLs. That is,
120      * <code>
121      * central::default::https://repo.maven.apache.org/maven2,myrepo::https://repo.acme.com,https://repo.acme2.com
122      * </code>
123      *
124      * @since 3.1.3
125      */
126     @Parameter(property = "remoteRepositories")
127     private List<String> remoteRepositories;
128 
129     /**
130      * Download transitively, retrieving the specified artifact and all of its dependencies.
131      *
132      * @since 3.1.3
133      */
134     @Parameter(property = "transitive", defaultValue = "false")
135     private boolean transitive = false;
136 
137     /**
138      * Skip plugin execution completely.
139      *
140      * @since 3.6.0
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         // open jar file in try-with-resources statement to guarantee the file closes after use regardless of errors
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                 // filter out files that do not end in .class
188                 if (!entryName.endsWith(".class")) {
189                     continue;
190                 }
191 
192                 // remove .class from the end and change format to use periods instead of forward slashes
193                 String className =
194                         entryName.substring(0, entryName.length() - 6).replace('/', '.');
195                 getLog().info(className);
196             }
197         }
198     }
199 }