You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2018/06/10 22:20:21 UTC

[maven-dependency-plugin] branch MDEP-617 created (now 176b07d)

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a change to branch MDEP-617
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git.


      at 176b07d  [MDEP-617] Use plexus-java for showing module info during dependency:resolve

This branch includes the following new commits:

     new 176b07d  [MDEP-617] Use plexus-java for showing module info during dependency:resolve

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


-- 
To stop receiving notification emails like this one, please contact
rfscholte@apache.org.

[maven-dependency-plugin] 01/01: [MDEP-617] Use plexus-java for showing module info during dependency:resolve

Posted by rf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MDEP-617
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git

commit 176b07d8b995f668dbce2ecb0588038ebbf3e1ea
Author: rfscholte <rf...@apache.org>
AuthorDate: Mon Jun 11 00:20:09 2018 +0200

    [MDEP-617] Use plexus-java for showing module info during dependency:resolve
---
 pom.xml                                            |   5 +
 .../resolvers/ResolveDependenciesMojo.java         | 172 ++++++---------------
 .../resolvers/ResolveDependenciesMojoTest.java     |  13 +-
 3 files changed, 59 insertions(+), 131 deletions(-)

diff --git a/pom.xml b/pom.xml
index 00f2cae..aada742 100644
--- a/pom.xml
+++ b/pom.xml
@@ -202,6 +202,11 @@ under the License.
       <artifactId>plexus-io</artifactId>
       <version>3.0.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-java</artifactId>
+      <version>0.9.10</version>
+    </dependency>
 
     <!-- shared -->
     <dependency>
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
index 86b595c..1a7d9d2 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojo.java
@@ -26,6 +26,7 @@ import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.plugins.dependency.utils.filters.ResolveFileFilter;
 import org.apache.maven.plugins.dependency.utils.markers.SourcesFileMarkerHandler;
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -33,18 +34,21 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
 import org.apache.maven.shared.utils.logging.MessageBuilder;
 import org.apache.maven.shared.utils.logging.MessageUtils;
+import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
+import org.codehaus.plexus.languages.java.jpms.LocationManager;
+import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
+import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
+import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource;
 
 import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
 
 /**
  * Goal that resolves the project dependencies from the repository. When using this goal while running on Java 9 the
@@ -88,6 +92,9 @@ public class ResolveDependenciesMojo
      */
     @Parameter( property = "includeParents", defaultValue = "false" )
     boolean includeParents;
+    
+    @Component
+    private LocationManager locationManager;
 
     /**
      * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
@@ -183,6 +190,37 @@ public class ResolveDependenciesMojo
     {
         StringBuilder sb = new StringBuilder();
         List<String> artifactStringList = new ArrayList<String>();
+        
+        Collection<File> artifactFiles = new ArrayList<File>( artifacts.size() );
+        for ( Artifact artifact : artifacts )
+        {
+            if ( artifact.getFile() != null )
+            {
+                artifactFiles.add( artifact.getFile() );
+            }
+        }
+        
+        ResolvePathsRequest<File> request = ResolvePathsRequest.ofFiles( artifactFiles );
+        ResolvePathsResult<File> result = null;
+        try
+        {
+            result = locationManager.resolvePaths( request );
+        }
+        catch ( IOException e1 )
+        {
+            // noop
+        }
+        
+        for ( Map.Entry<File, Exception> entry : result.getPathExceptions().entrySet() )
+        {
+            Throwable cause = entry.getValue();
+            while ( cause.getCause() != null )
+            {
+                cause = cause.getCause();
+            }
+            getLog().info( "Can't extract module name from " + entry.getKey().getName() + ": " + cause.getMessage() );
+        }
+        
         for ( Artifact artifact : artifacts )
         {
             MessageBuilder messageBuilder = MessageUtils.buffer();
@@ -221,14 +259,15 @@ public class ResolveDependenciesMojo
             // dependencies:collect won't download jars
             if ( artifact.getFile() != null )
             {
-                ModuleDescriptor moduleDescriptor = getModuleDescriptor( artifact.getFile() );
+                JavaModuleDescriptor moduleDescriptor = result.getPathElements().get( artifact.getFile() );
                 if ( moduleDescriptor != null )
                 {
-                    messageBuilder.project( " -- module " + moduleDescriptor.name );
+                    messageBuilder.project( " -- module " + moduleDescriptor.name() );
 
-                    if ( moduleDescriptor.automatic )
+                    if ( moduleDescriptor.isAutomatic() )
                     {
-                        if ( "MANIFEST".equals( moduleDescriptor.moduleNameSource ) )
+                        if ( ModuleNameSource.MANIFEST.
+                                        equals( result.getModulepathElements().get( artifact.getFile() ) ) )
                         {
                             messageBuilder.strong( " [auto]" );
                         }
@@ -251,121 +290,4 @@ public class ResolveDependenciesMojo
         }
         return sb;
     }
-
-    private ModuleDescriptor getModuleDescriptor( File artifactFile )
-    {
-        ModuleDescriptor moduleDescriptor = null;
-        try
-        {
-            // Use Java9 code to get moduleName, don't try to do it better with own implementation
-            Class<?> moduleFinderClass = Class.forName( "java.lang.module.ModuleFinder" );
-
-            java.nio.file.Path path = artifactFile.toPath();
-
-            Method ofMethod = moduleFinderClass.getMethod( "of", java.nio.file.Path[].class );
-            Object moduleFinderInstance = ofMethod.invoke( null, new Object[] { new java.nio.file.Path[] { path } } );
-
-            Method findAllMethod = moduleFinderClass.getMethod( "findAll" );
-            @SuppressWarnings( "unchecked" )
-            Set<Object> moduleReferences = (Set<Object>) findAllMethod.invoke( moduleFinderInstance );
-
-            // moduleReferences can be empty when referring to target/classes without module-info.class
-            if ( !moduleReferences.isEmpty() )
-            {
-                Object moduleReference = moduleReferences.iterator().next();
-                Method descriptorMethod = moduleReference.getClass().getMethod( "descriptor" );
-                Object moduleDescriptorInstance = descriptorMethod.invoke( moduleReference );
-
-                Method nameMethod = moduleDescriptorInstance.getClass().getMethod( "name" );
-                String name = (String) nameMethod.invoke( moduleDescriptorInstance );
-
-                moduleDescriptor = new ModuleDescriptor();
-                moduleDescriptor.name = name;
-
-                Method isAutomaticMethod = moduleDescriptorInstance.getClass().getMethod( "isAutomatic" );
-                moduleDescriptor.automatic = (Boolean) isAutomaticMethod.invoke( moduleDescriptorInstance );
-
-                if ( moduleDescriptor.automatic )
-                {
-                    if ( artifactFile.isFile() )
-                    {
-                        JarFile jarFile = null;
-                        try
-                        {
-                            jarFile = new JarFile( artifactFile );
-
-                            Manifest manifest = jarFile.getManifest();
-
-                            if ( manifest != null
-                                && manifest.getMainAttributes().getValue( "Automatic-Module-Name" ) != null )
-                            {
-                                moduleDescriptor.moduleNameSource = "MANIFEST";
-                            }
-                            else
-                            {
-                                moduleDescriptor.moduleNameSource = "FILENAME";
-                            }
-                        }
-                        catch ( IOException e )
-                        {
-                            // noop
-                        }
-                        finally
-                        {
-                            if ( jarFile != null )
-                            {
-                                try
-                                {
-                                    jarFile.close();
-                                }
-                                catch ( IOException e )
-                                {
-                                    // noop
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        catch ( ClassNotFoundException e )
-        {
-            // do nothing
-        }
-        catch ( NoSuchMethodException e )
-        {
-            e.printStackTrace();
-        }
-        catch ( SecurityException e )
-        {
-            // do nothing
-        }
-        catch ( IllegalAccessException e )
-        {
-            // do nothing
-        }
-        catch ( IllegalArgumentException e )
-        {
-            // do nothing
-        }
-        catch ( InvocationTargetException e )
-        {
-            Throwable cause = e.getCause();
-            while ( cause.getCause() != null )
-            {
-                cause = cause.getCause();
-            }
-            getLog().info( "Can't extract module name from " + artifactFile.getName() + ": " + cause.getMessage() );
-        }
-        return moduleDescriptor;
-    }
-
-    private class ModuleDescriptor
-    {
-        String name;
-
-        boolean automatic = true;
-
-        String moduleNameSource;
-    }
 }
diff --git a/src/test/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojoTest.java b/src/test/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojoTest.java
index 74b45f6..7db544f 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/resolvers/ResolveDependenciesMojoTest.java
@@ -19,7 +19,6 @@ package org.apache.maven.plugins.dependency.resolvers;
  * under the License.
  */
 
-import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -38,14 +37,14 @@ public class ResolveDependenciesMojoTest
     }
 
     public void testDependencyStatusLog()
-        throws IOException
+        throws Exception
     {
         Set<Artifact> artifacts = this.stubFactory.getMixedArtifacts();
         doTestDependencyStatusLog( artifacts );
     }
 
     public void testDependencyStatusLogNullFiles()
-        throws IOException
+        throws Exception
     {
         this.stubFactory.setCreateFiles( false );
         Set<Artifact> artifacts = this.stubFactory.getMixedArtifacts();
@@ -53,11 +52,12 @@ public class ResolveDependenciesMojoTest
     }
 
     public void testDependencyStatusEmptySet()
+        throws Exception
     {
         doTestDependencyStatusLog( new HashSet<Artifact>() );
     }
 
-    public void doTestDependencyStatusLog( Set<Artifact> artifacts )
+    public void doTestDependencyStatusLog( Set<Artifact> artifacts ) throws Exception
     {
         // TODO: implement logger to check correct output
         // this test is just looking for unexpected exceptions.
@@ -97,9 +97,10 @@ public class ResolveDependenciesMojoTest
         mojo.getOutput( true, false, false );
     }
 
-    private ResolveDependenciesMojo newMojo( final DependencyStatusSets dss )
+    private ResolveDependenciesMojo newMojo( final DependencyStatusSets dss ) throws Exception
     {
-        ResolveDependenciesMojo mojo = new ResolveDependenciesMojo();
+        ResolveDependenciesMojo mojo =
+            (ResolveDependenciesMojo) lookupMojo( "resolve", getTestFile( "target/test-classes/unit/resolve-test/plugin-config.xml" ) );
         mojo.results = dss;
         return mojo;
     }

-- 
To stop receiving notification emails like this one, please contact
rfscholte@apache.org.