You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2017/12/09 15:27:17 UTC

[maven-jdeps-plugin] 05/34: Introduce JDepsConsumer to analyze the output

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

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-jdeps-plugin.git

commit 20ba7a0733bdc4724f772a72c10710836991a646
Author: Robert Scholte <rf...@apache.org>
AuthorDate: Sat Feb 7 12:45:54 2015 +0000

    Introduce JDepsConsumer to analyze the output
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1658054 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  8 +++
 src/it/unsupported-api_main/verify.groovy          | 24 +++++++++
 src/it/unsupported-api_test/pom.xml                |  1 +
 .../unsupported-api_test/src/main/java/Main.java   | 28 ++++++++++
 src/it/unsupported-api_test/verify.groovy          | 24 +++++++++
 .../maven/plugin/jdeps/AbstractJDepsMojo.java      | 44 ++++++++++++++--
 .../plugin/jdeps/consumers/JDepsConsumer.java      | 60 ++++++++++++++++++++++
 .../plugin/jdeps/consumers/JDepsConsumerTest.java  | 47 +++++++++++++++++
 8 files changed, 232 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3734d9f..7f7052f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -78,5 +78,13 @@ under the License.
       <artifactId>commons-lang</artifactId>
       <version>2.4</version>
     </dependency>
+    
+    <!-- TEST -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file
diff --git a/src/it/unsupported-api_main/verify.groovy b/src/it/unsupported-api_main/verify.groovy
new file mode 100644
index 0000000..e0e8e5d
--- /dev/null
+++ b/src/it/unsupported-api_main/verify.groovy
@@ -0,0 +1,24 @@
+
+/*
+ * 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 LS = System.getProperty("line.separator")
+ 
+def buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( "Found offending packages:${LS} sun.misc -> JDK internal API (java.base)" );
\ No newline at end of file
diff --git a/src/it/unsupported-api_test/pom.xml b/src/it/unsupported-api_test/pom.xml
index cc99881..1e50590 100644
--- a/src/it/unsupported-api_test/pom.xml
+++ b/src/it/unsupported-api_test/pom.xml
@@ -41,6 +41,7 @@ under the License.
         <executions>
           <execution>
             <goals>
+              <goal>jdkinternals</goal>
               <goal>test-jdkinternals</goal>
             </goals>
           </execution>
diff --git a/src/it/unsupported-api_test/src/main/java/Main.java b/src/it/unsupported-api_test/src/main/java/Main.java
new file mode 100644
index 0000000..d32db2f
--- /dev/null
+++ b/src/it/unsupported-api_test/src/main/java/Main.java
@@ -0,0 +1,28 @@
+/*
+ * 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 )
+    {
+        System.out.println( "Hello world!" );
+    }
+
+}
diff --git a/src/it/unsupported-api_test/verify.groovy b/src/it/unsupported-api_test/verify.groovy
new file mode 100644
index 0000000..e0e8e5d
--- /dev/null
+++ b/src/it/unsupported-api_test/verify.groovy
@@ -0,0 +1,24 @@
+
+/*
+ * 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 LS = System.getProperty("line.separator")
+ 
+def buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( "Found offending packages:${LS} sun.misc -> JDK internal API (java.base)" );
\ No newline at end of file
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 7c4c518..38cab17 100644
--- a/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java
+++ b/src/main/java/org/apache/maven/plugin/jdeps/AbstractJDepsMojo.java
@@ -21,6 +21,7 @@ package org.apache.maven.plugin.jdeps;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 
@@ -30,6 +31,7 @@ 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.plugin.jdeps.consumers.JDepsConsumer;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
@@ -127,6 +129,12 @@ public abstract class AbstractJDepsMojo
     public void execute()
         throws MojoExecutionException, MojoFailureException
     {
+        if ( !new File( getClassesDirectory() ).exists() )
+        {
+            getLog().debug( "No classes to analyze" );
+            return;
+        }
+
         String jExecutable;
         try
         {
@@ -144,7 +152,23 @@ public abstract class AbstractJDepsMojo
         addJDepsOptions( cmd );
         addJDepsClasses( cmd );
         
-        executeJavadocCommandLine( cmd, outputDirectory );
+        JDepsConsumer consumer = new JDepsConsumer();
+        executeJavadocCommandLine( cmd, outputDirectory, consumer );
+        
+        // @ TODO if there will be more goals, this should be pushed down to AbstractJDKInternals
+        if ( consumer.getOffendingPackages().size() > 0 )
+        {
+            final String ls = System.getProperty( "line.separator" );
+            
+            StringBuilder msg = new StringBuilder();
+            msg.append( "Found offending packages:" ).append( ls );
+            for ( Map.Entry<String, String> offendingPackage : consumer.getOffendingPackages().entrySet() )
+            {
+                msg.append( ' ' ).append( offendingPackage.getKey() )
+                   .append( " -> " ).append( offendingPackage.getValue() ).append( ls );
+            }
+            throw new MojoExecutionException( msg.toString() );
+        }
     }
 
     protected void addJDepsOptions( Commandline cmd )
@@ -223,7 +247,7 @@ public abstract class AbstractJDepsMojo
             cmd.createArg().setValue( "-R" );
         }
         
-        cmd.createArg().setValue( "-version" );
+        // cmd.createArg().setValue( "-version" );
     }
     
     protected void addJDepsClasses( Commandline cmd )
@@ -322,7 +346,9 @@ public abstract class AbstractJDepsMojo
         return jdepsExe.getAbsolutePath();
     }
     
-    private void executeJavadocCommandLine( Commandline cmd, File javadocOutputDirectory ) throws MojoExecutionException
+    private void executeJavadocCommandLine( Commandline cmd, File jOutputDirectory,
+                                            CommandLineUtils.StringStreamConsumer consumer )
+        throws MojoExecutionException
     {
         if ( getLog().isDebugEnabled() )
         {
@@ -330,8 +356,18 @@ public abstract class AbstractJDepsMojo
             getLog().debug( CommandLineUtils.toString( cmd.getCommandline() ).replaceAll( "'", "" ) );
         }
 
+        
         CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
-        CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
+        CommandLineUtils.StringStreamConsumer out;
+        if ( consumer != null )
+        {
+            out = consumer;
+        }
+        else
+        {
+            out = new CommandLineUtils.StringStreamConsumer();
+        }
+                        
         try
         {
             int exitCode = CommandLineUtils.executeCommandLine( cmd, out, err );
diff --git a/src/main/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumer.java b/src/main/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumer.java
new file mode 100644
index 0000000..999dbc2
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumer.java
@@ -0,0 +1,60 @@
+package org.apache.maven.plugin.jdeps.consumers;
+
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.StreamConsumer;
+
+/**
+ * Consumes the output of the jdeps tool
+ *  
+ * @author Robert Scholte
+ *
+ */
+public class JDepsConsumer
+    extends CommandLineUtils.StringStreamConsumer
+    implements StreamConsumer
+{
+
+    private static final Pattern JDKINTERNALAPI = Pattern.compile( "\\s+->\\s([a-z\\.]+)\\s+(\\S.*)" );
+
+    private Map<String, String> offendingPackages = new HashMap<String, String>();
+
+    public void consumeLine( String line )
+    {
+        super.consumeLine( line );
+        
+        Matcher matcher = JDKINTERNALAPI.matcher( line );
+        if ( matcher.matches() )
+        {
+            offendingPackages.put( matcher.group( 1 ), matcher.group( 2 ) );
+        }
+    }
+
+    public Map<String, String> getOffendingPackages()
+    {
+        return offendingPackages;
+    }
+}
diff --git a/src/test/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumerTest.java b/src/test/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumerTest.java
new file mode 100644
index 0000000..e13316e
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugin/jdeps/consumers/JDepsConsumerTest.java
@@ -0,0 +1,47 @@
+package org.apache.maven.plugin.jdeps.consumers;
+
+/*
+ * 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 static org.junit.Assert.*;
+
+import org.apache.maven.plugin.jdeps.consumers.JDepsConsumer;
+import org.junit.Test;
+
+public class JDepsConsumerTest
+{
+
+    private JDepsConsumer consumer;
+    
+    @Test
+    public void testJDKInterAPI()
+    {
+        
+        consumer = new JDepsConsumer();
+        consumer.consumeLine( "test-classes -> java.base" );
+        consumer.consumeLine( "   <unnamed> (test-classes)" );
+        consumer.consumeLine( "      -> java.io                                            " );
+        consumer.consumeLine( "      -> java.lang                                          " );
+        consumer.consumeLine( "      -> sun.misc                                           JDK internal API (java.base)" );
+        
+        assertEquals( 1, consumer.getOffendingPackages().size() );
+        assertEquals( "JDK internal API (java.base)", consumer.getOffendingPackages().get( "sun.misc" ) );
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <co...@maven.apache.org>.