You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2020/05/27 04:12:57 UTC

[GitHub] [maven-dependency-plugin] bimargulies-google commented on a change in pull request #57: [MDEP-645] Created new mojo GetClassesMojo which lists all class dependencies for a specified artifact.

bimargulies-google commented on a change in pull request #57:
URL: https://github.com/apache/maven-dependency-plugin/pull/57#discussion_r430506873



##########
File path: src/main/java/org/apache/maven/plugins/dependency/GetClassesMojo.java
##########
@@ -0,0 +1,396 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+import org.codehaus.plexus.util.StringUtils;
+
+@Mojo( name = "get-classes", requiresProject = false, threadSafe = true )
+public class GetClassesMojo
+    extends AbstractMojo
+{
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    private MavenSession session;
+
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private DependencyResolver dependencyResolver;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Map that contains the layouts.
+     */
+    @Component( role = ArtifactRepositoryLayout.class )
+    private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
+
+    /**
+     * The repository system.
+     */
+    @Component
+    private RepositorySystem repositorySystem;
+
+    private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
+
+    /**
+     * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "groupId" )
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "artifactId" )
+    private String artifactId;
+
+    /**
+     * The version of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "version" )
+    private String version;
+
+    /**
+     * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "classifier" )
+    private String classifier;
+
+    /**
+     * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "packaging", defaultValue = "jar" )
+    private String packaging = "jar";
+
+    /**
+     * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
+     * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
+     */
+    @Parameter( property = "remoteRepositories" )
+    private String remoteRepositories;
+
+    /**
+     * A string of the form groupId:artifactId:version[:packaging[:classifier]].
+     */
+    @Parameter( property = "artifact" )
+    private String artifact;
+
+    @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
+    private List<ArtifactRepository> pomRemoteRepositories;
+
+    /**
+     * Download transitively, retrieving the specified artifact and all of its dependencies.
+     */
+    @Parameter( property = "transitive", defaultValue = "true" )
+    private boolean transitive = true;
+
+    /**
+     * Skip plugin execution completely.
+     *
+     * @since 2.7
+     */
+    @Parameter( property = "mdep.skip", defaultValue = "false" )
+    private boolean skip;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        ProjectBuildingRequest buildingRequest = buildBuildingRequest();
+        DefaultDependableCoordinate coordinate = getCoordinate();
+
+        try
+        {
+            getLog().info( "Retrieving classes of dependencies for " + artifact );
+            if ( isTransitive() )
+            {
+                Iterable<ArtifactResult> artifacts = getDependencyResolver().resolveDependencies( buildingRequest, coordinate, null );
+
+                for ( ArtifactResult result : artifacts )
+                {
+                    printClassesFromArtifactResult( result );
+                }
+            }
+            else
+            {
+                ArtifactResult result = getArtifactResolver().resolveArtifact( buildingRequest, toArtifactCoordinate( coordinate ) );
+
+                printClassesFromArtifactResult( result );
+            }
+        }
+        catch ( ArtifactResolverException | DependencyResolverException | IOException e )
+        {
+            throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
+        }
+    }
+
+    public void printClassesFromArtifactResult( ArtifactResult result )
+            throws IOException
+    {
+        JarFile jarFile = new JarFile( result.getArtifact().getFile() );
+        Enumeration e = jarFile.entries();
+
+        while ( e.hasMoreElements() )
+        {
+            JarEntry entry = ( JarEntry )e.nextElement();
+            String name = entry.getName();
+
+            // filter out files that do not end in .class
+            if ( name.length() <= 6 || !name.substring( name.length() - 6 ).equals( ".class" )) {
+                continue;
+            }
+
+            // remove .class from the end and change format to use periods instead of forward slashes
+            name = name.substring( 0, name.length() - 6 ).replace( '/', '.' );
+            getLog().info( name );
+        }
+        jarFile.close();
+    }
+
+    public ProjectBuildingRequest buildBuildingRequest()
+            throws MojoExecutionException, MojoFailureException
+    {
+        if ( coordinate.getArtifactId() == null && artifact == null )

Review comment:
       You don't use coordinate below. Are you missing an else clause? Is that the code that would run if someone used -DgroupId ... instead of -Dartifact?

##########
File path: src/test/java/org/apache/maven/plugins/dependency/TestGetClassesMojo.java
##########
@@ -0,0 +1,88 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.util.DefaultRepositorySystemSession;
+
+import java.io.File;
+
+public class TestGetClassesMojo
+        extends AbstractDependencyMojoTestCase
+{
+    GetClassesMojo mojo;
+
+    protected void setUp()
+            throws Exception
+    {
+        // required for mojo lookups to work
+        super.setUp( "markers", false );
+
+        File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
+        assert testPom.exists();
+        mojo = (GetClassesMojo) lookupMojo( "get-classes", testPom );
+
+        assertNotNull( mojo );
+
+        LegacySupport legacySupport = lookup( LegacySupport.class );
+        MavenSession session = newMavenSession( new MavenProjectStub() );
+        Settings settings = session.getSettings();
+        Server server = new Server();
+        server.setId( "myserver" );
+        server.setUsername( "foo" );
+        server.setPassword( "bar" );
+        settings.addServer( server );
+        legacySupport.setSession( session );
+        DefaultRepositorySystemSession repoSession =
+                (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
+        repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( testDir.getAbsolutePath() ) );
+
+        setVariableValueToObject( mojo, "session", legacySupport.getSession() );
+    }
+
+    public void testGetClassesNotTransitive()
+            throws Exception
+    {
+        setVariableValueToObject( mojo, "remoteRepositories", "central::default::https://repo.maven.apache.org/maven2,"
+                + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2" );
+        mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
+        setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
+
+        mojo.execute();
+        return;
+    }
+
+    public void testGetClassesTransitive()
+            throws Exception
+    {
+        setVariableValueToObject( mojo, "remoteRepositories", "central::default::https://repo.maven.apache.org/maven2,"
+                + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2" );
+        mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
+        setVariableValueToObject( mojo, "transitive", Boolean.TRUE );
+
+        mojo.execute();
+        return;

Review comment:
       No need for return.

##########
File path: src/main/java/org/apache/maven/plugins/dependency/GetClassesMojo.java
##########
@@ -0,0 +1,396 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+import org.codehaus.plexus.util.StringUtils;
+
+@Mojo( name = "get-classes", requiresProject = false, threadSafe = true )
+public class GetClassesMojo
+    extends AbstractMojo
+{
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    private MavenSession session;
+
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private DependencyResolver dependencyResolver;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Map that contains the layouts.
+     */
+    @Component( role = ArtifactRepositoryLayout.class )
+    private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
+
+    /**
+     * The repository system.
+     */
+    @Component
+    private RepositorySystem repositorySystem;
+
+    private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
+
+    /**
+     * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "groupId" )
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "artifactId" )
+    private String artifactId;
+
+    /**
+     * The version of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "version" )
+    private String version;
+
+    /**
+     * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "classifier" )
+    private String classifier;
+
+    /**
+     * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "packaging", defaultValue = "jar" )
+    private String packaging = "jar";
+
+    /**
+     * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
+     * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
+     */
+    @Parameter( property = "remoteRepositories" )
+    private String remoteRepositories;
+
+    /**
+     * A string of the form groupId:artifactId:version[:packaging[:classifier]].
+     */
+    @Parameter( property = "artifact" )
+    private String artifact;
+
+    @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
+    private List<ArtifactRepository> pomRemoteRepositories;
+
+    /**
+     * Download transitively, retrieving the specified artifact and all of its dependencies.
+     */
+    @Parameter( property = "transitive", defaultValue = "true" )
+    private boolean transitive = true;
+
+    /**
+     * Skip plugin execution completely.
+     *
+     * @since 2.7
+     */
+    @Parameter( property = "mdep.skip", defaultValue = "false" )

Review comment:
       I think you can remove all of the 'skip' processing; it's not terribly useful for a mojo intended for CLI.

##########
File path: src/main/java/org/apache/maven/plugins/dependency/GetClassesMojo.java
##########
@@ -0,0 +1,396 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+import org.codehaus.plexus.util.StringUtils;
+
+@Mojo( name = "get-classes", requiresProject = false, threadSafe = true )
+public class GetClassesMojo
+    extends AbstractMojo
+{
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    private MavenSession session;
+
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private DependencyResolver dependencyResolver;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Map that contains the layouts.
+     */
+    @Component( role = ArtifactRepositoryLayout.class )
+    private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
+
+    /**
+     * The repository system.
+     */
+    @Component
+    private RepositorySystem repositorySystem;
+
+    private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
+
+    /**
+     * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "groupId" )
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "artifactId" )
+    private String artifactId;
+
+    /**
+     * The version of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "version" )
+    private String version;
+
+    /**
+     * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "classifier" )
+    private String classifier;
+
+    /**
+     * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "packaging", defaultValue = "jar" )
+    private String packaging = "jar";
+
+    /**
+     * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
+     * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
+     */
+    @Parameter( property = "remoteRepositories" )
+    private String remoteRepositories;
+
+    /**
+     * A string of the form groupId:artifactId:version[:packaging[:classifier]].
+     */
+    @Parameter( property = "artifact" )
+    private String artifact;
+
+    @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
+    private List<ArtifactRepository> pomRemoteRepositories;
+
+    /**
+     * Download transitively, retrieving the specified artifact and all of its dependencies.
+     */
+    @Parameter( property = "transitive", defaultValue = "true" )
+    private boolean transitive = true;
+
+    /**
+     * Skip plugin execution completely.
+     *
+     * @since 2.7
+     */
+    @Parameter( property = "mdep.skip", defaultValue = "false" )
+    private boolean skip;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        ProjectBuildingRequest buildingRequest = buildBuildingRequest();
+        DefaultDependableCoordinate coordinate = getCoordinate();
+
+        try
+        {
+            getLog().info( "Retrieving classes of dependencies for " + artifact );
+            if ( isTransitive() )
+            {
+                Iterable<ArtifactResult> artifacts = getDependencyResolver().resolveDependencies( buildingRequest, coordinate, null );
+
+                for ( ArtifactResult result : artifacts )
+                {
+                    printClassesFromArtifactResult( result );

Review comment:
       Print the file name of the jar before printing its contents if you are printing more than one jar file's worth?

##########
File path: src/test/java/org/apache/maven/plugins/dependency/TestGetClassesMojo.java
##########
@@ -0,0 +1,88 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.util.DefaultRepositorySystemSession;
+
+import java.io.File;
+
+public class TestGetClassesMojo
+        extends AbstractDependencyMojoTestCase
+{
+    GetClassesMojo mojo;
+
+    protected void setUp()
+            throws Exception
+    {
+        // required for mojo lookups to work
+        super.setUp( "markers", false );
+
+        File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
+        assert testPom.exists();
+        mojo = (GetClassesMojo) lookupMojo( "get-classes", testPom );
+
+        assertNotNull( mojo );
+
+        LegacySupport legacySupport = lookup( LegacySupport.class );
+        MavenSession session = newMavenSession( new MavenProjectStub() );
+        Settings settings = session.getSettings();
+        Server server = new Server();
+        server.setId( "myserver" );
+        server.setUsername( "foo" );
+        server.setPassword( "bar" );
+        settings.addServer( server );
+        legacySupport.setSession( session );
+        DefaultRepositorySystemSession repoSession =
+                (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
+        repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( testDir.getAbsolutePath() ) );
+
+        setVariableValueToObject( mojo, "session", legacySupport.getSession() );
+    }
+
+    public void testGetClassesNotTransitive()
+            throws Exception
+    {
+        setVariableValueToObject( mojo, "remoteRepositories", "central::default::https://repo.maven.apache.org/maven2,"
+                + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2" );
+        mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
+        setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
+
+        mojo.execute();
+        return;
+    }
+
+    public void testGetClassesTransitive()

Review comment:
       Um, this does not seem to test anything except that it does not explode.
   
   
   

##########
File path: src/main/java/org/apache/maven/plugins/dependency/GetClassesMojo.java
##########
@@ -0,0 +1,396 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+import org.codehaus.plexus.util.StringUtils;
+
+@Mojo( name = "get-classes", requiresProject = false, threadSafe = true )
+public class GetClassesMojo
+    extends AbstractMojo
+{
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    private MavenSession session;
+
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private DependencyResolver dependencyResolver;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Map that contains the layouts.
+     */
+    @Component( role = ArtifactRepositoryLayout.class )
+    private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
+
+    /**
+     * The repository system.
+     */
+    @Component
+    private RepositorySystem repositorySystem;
+
+    private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
+
+    /**
+     * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "groupId" )
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "artifactId" )
+    private String artifactId;
+
+    /**
+     * The version of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "version" )
+    private String version;
+
+    /**
+     * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "classifier" )
+    private String classifier;
+
+    /**
+     * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "packaging", defaultValue = "jar" )
+    private String packaging = "jar";
+
+    /**
+     * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
+     * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
+     */
+    @Parameter( property = "remoteRepositories" )
+    private String remoteRepositories;
+
+    /**
+     * A string of the form groupId:artifactId:version[:packaging[:classifier]].
+     */
+    @Parameter( property = "artifact" )
+    private String artifact;
+
+    @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
+    private List<ArtifactRepository> pomRemoteRepositories;
+
+    /**
+     * Download transitively, retrieving the specified artifact and all of its dependencies.
+     */
+    @Parameter( property = "transitive", defaultValue = "true" )
+    private boolean transitive = true;
+
+    /**
+     * Skip plugin execution completely.
+     *
+     * @since 2.7
+     */
+    @Parameter( property = "mdep.skip", defaultValue = "false" )
+    private boolean skip;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        ProjectBuildingRequest buildingRequest = buildBuildingRequest();
+        DefaultDependableCoordinate coordinate = getCoordinate();
+
+        try
+        {
+            getLog().info( "Retrieving classes of dependencies for " + artifact );
+            if ( isTransitive() )
+            {
+                Iterable<ArtifactResult> artifacts = getDependencyResolver().resolveDependencies( buildingRequest, coordinate, null );
+
+                for ( ArtifactResult result : artifacts )
+                {
+                    printClassesFromArtifactResult( result );
+                }
+            }
+            else
+            {
+                ArtifactResult result = getArtifactResolver().resolveArtifact( buildingRequest, toArtifactCoordinate( coordinate ) );
+
+                printClassesFromArtifactResult( result );
+            }
+        }
+        catch ( ArtifactResolverException | DependencyResolverException | IOException e )
+        {
+            throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
+        }
+    }
+
+    public void printClassesFromArtifactResult( ArtifactResult result )
+            throws IOException
+    {
+        JarFile jarFile = new JarFile( result.getArtifact().getFile() );
+        Enumeration e = jarFile.entries();
+
+        while ( e.hasMoreElements() )
+        {
+            JarEntry entry = ( JarEntry )e.nextElement();
+            String name = entry.getName();
+
+            // filter out files that do not end in .class
+            if ( name.length() <= 6 || !name.substring( name.length() - 6 ).equals( ".class" )) {
+                continue;
+            }
+
+            // remove .class from the end and change format to use periods instead of forward slashes
+            name = name.substring( 0, name.length() - 6 ).replace( '/', '.' );
+            getLog().info( name );

Review comment:
       I don't think that using the log is the best delivery mechanism, but I could be confused. @elharo what do you think? Also, I have no idea how to test for log output, you'd need to mock the log somehow.

##########
File path: src/test/java/org/apache/maven/plugins/dependency/TestGetClassesMojo.java
##########
@@ -0,0 +1,88 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.util.DefaultRepositorySystemSession;
+
+import java.io.File;
+
+public class TestGetClassesMojo
+        extends AbstractDependencyMojoTestCase
+{
+    GetClassesMojo mojo;
+
+    protected void setUp()
+            throws Exception
+    {
+        // required for mojo lookups to work
+        super.setUp( "markers", false );
+
+        File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
+        assert testPom.exists();
+        mojo = (GetClassesMojo) lookupMojo( "get-classes", testPom );
+
+        assertNotNull( mojo );
+
+        LegacySupport legacySupport = lookup( LegacySupport.class );
+        MavenSession session = newMavenSession( new MavenProjectStub() );
+        Settings settings = session.getSettings();
+        Server server = new Server();
+        server.setId( "myserver" );
+        server.setUsername( "foo" );
+        server.setPassword( "bar" );
+        settings.addServer( server );
+        legacySupport.setSession( session );
+        DefaultRepositorySystemSession repoSession =
+                (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
+        repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( testDir.getAbsolutePath() ) );
+
+        setVariableValueToObject( mojo, "session", legacySupport.getSession() );
+    }
+
+    public void testGetClassesNotTransitive()
+            throws Exception
+    {
+        setVariableValueToObject( mojo, "remoteRepositories", "central::default::https://repo.maven.apache.org/maven2,"
+                + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2" );
+        mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
+        setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
+
+        mojo.execute();
+        return;

Review comment:
       No need for return.

##########
File path: src/main/java/org/apache/maven/plugins/dependency/GetClassesMojo.java
##########
@@ -0,0 +1,406 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+import org.codehaus.plexus.util.StringUtils;
+
+
+/**
+ * Retrieves and lists all class dependencies for the specified artifact from the specified remote repositories.
+ */
+@Mojo( name = "get-classes", requiresProject = false, threadSafe = true )
+public class GetClassesMojo
+    extends AbstractMojo
+{
+    private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    private MavenSession session;
+
+    @Component
+    private ArtifactResolver artifactResolver;
+
+    @Component
+    private DependencyResolver dependencyResolver;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Map that contains the layouts.
+     */
+    @Component( role = ArtifactRepositoryLayout.class )
+    private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
+
+    /**
+     * The repository system.
+     */
+    @Component
+    private RepositorySystem repositorySystem;
+
+    private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
+
+    /**
+     * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "groupId" )
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "artifactId" )
+    private String artifactId;
+
+    /**
+     * The version of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "version" )
+    private String version;
+
+    /**
+     * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "classifier" )
+    private String classifier;
+
+    /**
+     * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
+     */
+    @Parameter( property = "packaging", defaultValue = "jar" )
+    private String packaging = "jar";

Review comment:
       It will work for a .war or any other 'renamed jar'. But the substitution of dots and slashes should perhaps be removed for non-.jar.

##########
File path: src/test/java/org/apache/maven/plugins/dependency/TestGetClassesMojo.java
##########
@@ -0,0 +1,86 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;

Review comment:
       Do you have an example to point at here?

##########
File path: src/test/java/org/apache/maven/plugins/dependency/TestGetClassesMojo.java
##########
@@ -0,0 +1,86 @@
+package org.apache.maven.plugins.dependency;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.util.DefaultRepositorySystemSession;
+
+import java.io.File;
+
+public class TestGetClassesMojo
+        extends AbstractDependencyMojoTestCase
+{
+    GetClassesMojo mojo;
+
+    protected void setUp()
+            throws Exception
+    {
+        // required for mojo lookups to work
+        super.setUp( "markers", false );
+
+        File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
+        assert testPom.exists();

Review comment:
       oh, whoops, sorry I missed this.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org