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 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   * Retrieves and lists all classes contained in the specified artifact from the specified remote repositories.
42   *
43   * @since 3.1.3
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       * The group ID of the artifact to download. Ignored if {@code artifact} is used.
55       *
56       * @since 3.1.3
57       */
58      @Parameter(property = "groupId")
59      public void setGroupId(String groupId) {
60          paramArtifact.setGroupId(groupId);
61      }
62  
63      /**
64       * The artifact ID of the artifact to download. Ignored if {@code artifact} is used.
65       *
66       * @since 3.1.3
67       */
68      @Parameter(property = "artifactId")
69      public void setArtifactId(String artifactId) {
70          paramArtifact.setArtifactId(artifactId);
71      }
72  
73      /**
74       * The version of the artifact to download. Ignored if {@code artifact} is used.
75       *
76       * @since 3.1.3
77       */
78      @Parameter(property = "version")
79      public void setVersion(String version) {
80          paramArtifact.setVersion(version);
81      }
82  
83      /**
84       * The classifier of the artifact to download. Ignored if {@code artifact} is used.
85       *
86       * @since 3.1.3
87       */
88      @Parameter(property = "classifier")
89      public void setClassifier(String classifier) {
90          paramArtifact.setClassifier(classifier);
91      }
92  
93      /**
94       * The packaging of the artifact to download. Ignored if {@code artifact} is used.
95       *
96       * @since 3.1.3
97       */
98      @Parameter(property = "packaging", defaultValue = "jar")
99      public void setPackaging(String packaging) {
100         paramArtifact.setPackaging(packaging);
101     }
102 
103     /**
104      * A string of the form {@code groupId:artifactId:version[:packaging[:classifier]]}.
105      *
106      * @since 3.1.3
107      */
108     @Parameter(property = "artifact")
109     public void setArtifact(String artifact) {
110         paramArtifact.setArtifact(artifact);
111     }
112 
113     /**
114      * Repositories in the format {@code id::[layout::]url} or just URLs. That is,
115      * <code>
116      * central::default::https://repo.maven.apache.org/maven2,myrepo::https://repo.acme.com,https://repo.acme2.com
117      * </code>
118      *
119      * @since 3.1.3
120      */
121     @Parameter(property = "remoteRepositories")
122     private List<String> remoteRepositories;
123 
124     /**
125      * Download transitively, retrieving the specified artifact and all of its dependencies.
126      *
127      * @since 3.1.3
128      */
129     @Parameter(property = "transitive", defaultValue = "false")
130     private boolean transitive = false;
131 
132     /**
133      * Skip plugin execution completely.
134      *
135      * @since 3.6.0
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         // open jar file in try-with-resources statement to guarantee the file closes after use regardless of errors
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                 // filter out files that do not end in .class
183                 if (!entryName.endsWith(".class")) {
184                     continue;
185                 }
186 
187                 // remove .class from the end and change format to use periods instead of forward slashes
188                 String className =
189                         entryName.substring(0, entryName.length() - 6).replace('/', '.');
190                 getLog().info(className);
191             }
192         }
193     }
194 }