[MJDEPS-6] -cp should not duplicated classes/files to analyze

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1806510 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/dependenciesToAnalyze/invoker.properties b/src/it/dependenciesToAnalyze/invoker.properties
new file mode 100644
index 0000000..b472579
--- /dev/null
+++ b/src/it/dependenciesToAnalyze/invoker.properties
@@ -0,0 +1,19 @@
+# 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.

+

+invoker.java.version=1.8+

+invoker.goals=process-classes

diff --git a/src/it/dependenciesToAnalyze/pom.xml b/src/it/dependenciesToAnalyze/pom.xml
new file mode 100644
index 0000000..c6ef9c5
--- /dev/null
+++ b/src/it/dependenciesToAnalyze/pom.xml
@@ -0,0 +1,59 @@
+<?xml version='1.0' encoding='UTF-8'?>

+

+<!--

+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.

+-->

+

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

+  <modelVersion>4.0.0</modelVersion>

+  

+  <groupId>org.apache.maven.plugins.jdeps.its</groupId>

+  <artifactId>basic</artifactId>

+  <version>1.0-SNAPSHOT</version>

+  

+  <properties>

+    <maven.compiler.source>1.6</maven.compiler.source>

+    <maven.compiler.target>1.6</maven.compiler.target>

+  </properties>

+  

+  <build>

+    <plugins>

+      <plugin>

+        <groupId>@project.groupId@</groupId>

+        <artifactId>@project.artifactId@</artifactId>

+        <version>@project.version@</version>

+        <executions>

+          <execution>

+            <goals>

+              <goal>jdkinternals</goal>

+            </goals>

+          </execution>

+        </executions>

+      </plugin>

+    </plugins>

+  </build>

+  

+  <dependencies>

+    <dependency>

+       <groupId>org.codehaus.plexus</groupId>

+       <artifactId>plexus-utils</artifactId>

+       <version>3.0.24</version>

+    </dependency>

+  </dependencies>

+</project>
\ No newline at end of file
diff --git a/src/it/dependenciesToAnalyze/src/main/java/Main.java b/src/it/dependenciesToAnalyze/src/main/java/Main.java
new file mode 100644
index 0000000..a9ca32c
--- /dev/null
+++ b/src/it/dependenciesToAnalyze/src/main/java/Main.java
@@ -0,0 +1,29 @@
+/*

+ * 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.

+ */

+

+public class Main

+{

+   public static void main( String[] args )

+   {

+       for ( String validFamily : org.codehaus.plexus.util.Os.getValidFamilies() )

+       {

+           System.out.println( validFamily ); 

+       }

+   }    

+}
\ No newline at end of file
diff --git a/src/it/dependenciesToAnalyze/verify.groovy b/src/it/dependenciesToAnalyze/verify.groovy
new file mode 100644
index 0000000..d9050d0
--- /dev/null
+++ b/src/it/dependenciesToAnalyze/verify.groovy
@@ -0,0 +1,37 @@
+

+/*

+ * 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.

+ */

+ 

+def buildLog = new File( basedir, 'build.log' )

+

+def found = false;

+

+assert buildLog.readLines().each { String line -> 

+

+  if ( line.startsWith( '[DEBUG] Executing: ' ) )

+  {

+    assert line.count( "classes" ) == 1 : "invalid classes count: " + line.count( "classes" )

+

+    assert line.count( "plexus-utils-3.0.24.jar" ) == 1 : "invalid plexus-utils-3.0.24.jar count: " + line.count( "plexus-utils-3.0.24.jar" )

+    

+    found = true;

+  }

+}

+

+assert found == true : "Executing line not found"

diff --git a/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDKInternalsMojo.java b/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDKInternalsMojo.java
index 8f62c80..9b50c1a 100644
--- a/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDKInternalsMojo.java
+++ b/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDKInternalsMojo.java
@@ -19,6 +19,9 @@
  * under the License.

  */

 

+import java.nio.file.Path;

+import java.util.Set;

+

 import org.apache.maven.plugin.MojoFailureException;

 import org.codehaus.plexus.util.cli.Commandline;

 

@@ -31,10 +34,10 @@
 {

 

     @Override

-    protected void addJDepsOptions( Commandline cmd )

+    protected void addJDepsOptions( Commandline cmd, Set<Path> dependenciesToAnalyze )

         throws MojoFailureException

     {

-        super.addJDepsOptions( cmd );

+        super.addJDepsOptions( cmd, dependenciesToAnalyze );

         cmd.createArg().setValue( "-jdkinternals" );

     }

 }

diff --git a/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java b/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java
index 60d9436..35b322a 100644
--- a/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java
+++ b/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java
@@ -23,10 +23,16 @@
 import java.io.IOException;

 import java.lang.reflect.InvocationTargetException;

 import java.lang.reflect.Method;

+import java.nio.file.Path;

+import java.nio.file.Paths;

+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.Properties;

+import java.util.Set;

 import java.util.StringTokenizer;

 

 import org.apache.commons.lang3.SystemUtils;

@@ -204,8 +210,10 @@
 //      jdeps [options] classes ...

         Commandline cmd = new Commandline();

         cmd.setExecutable( jExecutable );

-        addJDepsOptions( cmd );

-        addJDepsClasses( cmd );

+        

+        Set<Path> dependenciesToAnalyze = getDependenciesToAnalyze();

+        addJDepsOptions( cmd, dependenciesToAnalyze );

+        addJDepsClasses( cmd, dependenciesToAnalyze );

         

         JDepsConsumer consumer = new JDepsConsumer();

         executeJDepsCommandLine( cmd, outputDirectory, consumer );

@@ -230,7 +238,7 @@
         }

     }

 

-    protected void addJDepsOptions( Commandline cmd )

+    protected void addJDepsOptions( Commandline cmd, Set<Path> dependenciesToAnalyze )

         throws MojoFailureException

     {

         if ( dotOutput != null )

@@ -262,8 +270,23 @@
         

         try

         {

-            cmd.createArg().setValue( "-cp" );

-            cmd.createArg().setValue( getClassPath() );

+            Collection<Path> cp = new ArrayList<>();

+            

+            for ( Path path : getClassPath() )

+            {

+                if ( !dependenciesToAnalyze.contains( path ) )

+                {

+                    cp.add( path );

+                }

+            }

+            

+            if ( !cp.isEmpty() )

+            {

+                cmd.createArg().setValue( "-cp" );

+

+                cmd.createArg().setValue( StringUtils.join( cp.iterator(), File.pathSeparator ) );

+            }

+            

         }

         catch ( DependencyResolutionRequiredException e )

         {

@@ -314,11 +337,12 @@
         // cmd.createArg().setValue( "-version" );

     }

     

-    protected void addJDepsClasses( Commandline cmd )

+    protected Set<Path> getDependenciesToAnalyze()

     {

-        // <classes> can be a pathname to a .class file, a directory, a JAR file, or a fully-qualified class name.

-        cmd.createArg().setFile( new File( getClassesDirectory() ) );

-

+        Set<Path> jdepsClasses = new LinkedHashSet<>();

+        

+        jdepsClasses.add( Paths.get( getClassesDirectory() ) );

+        

         if ( dependenciesToAnalyzeIncludes != null )

         {

             MatchPatterns includes = MatchPatterns.from( dependenciesToAnalyzeIncludes );

@@ -340,10 +364,21 @@
                 if ( includes.matchesPatternStart( versionlessKey, true ) 

                     && !excludes.matchesPatternStart( versionlessKey, true ) )

                 {

-                    cmd.createArg().setFile( artifact.getFile() );

+                    jdepsClasses.add( artifact.getFile().toPath() );

                 }

             }

         }

+        

+        return jdepsClasses;

+    }

+    

+    protected void addJDepsClasses( Commandline cmd, Set<Path> dependenciesToAnalyze )

+    {

+        // <classes> can be a pathname to a .class file, a directory, a JAR file, or a fully-qualified class name.

+        for ( Path dependencyToAnalyze : dependenciesToAnalyze )

+        {

+            cmd.createArg().setFile( dependencyToAnalyze.toFile() );

+        }

     }

 

     private String getJDepsExecutable() throws IOException

@@ -421,7 +456,7 @@
         if ( getLog().isDebugEnabled() )

         {

             // no quoted arguments

-            getLog().debug( CommandLineUtils.toString( cmd.getCommandline() ).replaceAll( "'", "" ) );

+            getLog().debug( "Executing: " + CommandLineUtils.toString( cmd.getCommandline() ).replaceAll( "'", "" ) );

         }

 

         

@@ -544,5 +579,5 @@
 

     protected abstract String getClassesDirectory();

     

-    protected abstract String getClassPath() throws DependencyResolutionRequiredException;

+    protected abstract Collection<Path> getClassPath() throws DependencyResolutionRequiredException;

 }

diff --git a/src/main/java/org/apache/maven/plugin/jdeps/JDKInternalsMojo.java b/src/main/java/org/apache/maven/plugin/jdeps/JDKInternalsMojo.java
index ef3d277..806a3c2 100644
--- a/src/main/java/org/apache/maven/plugin/jdeps/JDKInternalsMojo.java
+++ b/src/main/java/org/apache/maven/plugin/jdeps/JDKInternalsMojo.java
@@ -19,13 +19,16 @@
  * under the License.

  */

 

-import java.io.File;

+import java.nio.file.Path;

+import java.nio.file.Paths;

+import java.util.Collection;

+import java.util.LinkedHashSet;

+import java.util.Set;

 

 import org.apache.maven.artifact.DependencyResolutionRequiredException;

 import org.apache.maven.plugins.annotations.LifecyclePhase;

 import org.apache.maven.plugins.annotations.Mojo;

 import org.apache.maven.plugins.annotations.ResolutionScope;

-import org.codehaus.plexus.util.StringUtils;

 

 /**

  * Check if main classes depend on internal JDK classes

@@ -47,9 +50,16 @@
     }

     

     @Override

-    protected String getClassPath() throws DependencyResolutionRequiredException

+    protected Collection<Path> getClassPath()

+        throws DependencyResolutionRequiredException

     {

-        return StringUtils.join( getProject().getCompileClasspathElements().iterator(), File.pathSeparator );

-    }

+        Set<Path> classPath = new LinkedHashSet<>( getProject().getCompileClasspathElements().size() );

 

+        for ( String elm : getProject().getCompileClasspathElements() )

+        {

+            classPath.add( Paths.get( elm ) );

+        }

+

+        return classPath;

+    }

 }

diff --git a/src/main/java/org/apache/maven/plugin/jdeps/TestJDKInternalsMojo.java b/src/main/java/org/apache/maven/plugin/jdeps/TestJDKInternalsMojo.java
index ff80bea..466f131 100644
--- a/src/main/java/org/apache/maven/plugin/jdeps/TestJDKInternalsMojo.java
+++ b/src/main/java/org/apache/maven/plugin/jdeps/TestJDKInternalsMojo.java
@@ -19,13 +19,16 @@
  * under the License.

  */

 

-import java.io.File;

+import java.nio.file.Path;

+import java.nio.file.Paths;

+import java.util.Collection;

+import java.util.LinkedHashSet;

+import java.util.Set;

 

 import org.apache.maven.artifact.DependencyResolutionRequiredException;

 import org.apache.maven.plugins.annotations.LifecyclePhase;

 import org.apache.maven.plugins.annotations.Mojo;

 import org.apache.maven.plugins.annotations.ResolutionScope;

-import org.codehaus.plexus.util.StringUtils;

 

 /**

  * Check if test classes depend on internal JDK classes

@@ -47,8 +50,16 @@
     }

     

     @Override

-    protected String getClassPath() throws DependencyResolutionRequiredException

+    protected Collection<Path> getClassPath()

+        throws DependencyResolutionRequiredException

     {

-        return StringUtils.join( getProject().getTestClasspathElements().iterator(), File.pathSeparator );

+        Set<Path> classPath = new LinkedHashSet<>( getProject().getTestClasspathElements().size() );

+

+        for ( String elm : getProject().getTestClasspathElements() )

+        {

+            classPath.add( Paths.get( elm ) );

+        }

+

+        return classPath;

     }

 }