You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/12/05 15:00:49 UTC

[sling-slingfeature-maven-plugin] 01/02: SLING-8876 - ApisJar mojo includess invalid javadoc package roots

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

rombert pushed a commit to branch feature/SLING-8876
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git

commit 0dcfe99b30844ea96ced6fed38ee9d23e258107e
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu Dec 5 15:58:34 2019 +0100

    SLING-8876 - ApisJar mojo includess invalid javadoc package roots
    
    Java 11 seems to be more diligent when following references for Javadoc
    information. Ensure that we only consider top-level directories that
    contain java files.
---
 src/it/apis-jar-invalid-javadoc/invoker.properties | 21 ++++++++
 src/it/apis-jar-invalid-javadoc/pom.xml            | 59 ++++++++++++++++++++++
 .../src/main/features/main.json                    | 17 +++++++
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 37 ++++++++++++--
 4 files changed, 131 insertions(+), 3 deletions(-)

diff --git a/src/it/apis-jar-invalid-javadoc/invoker.properties b/src/it/apis-jar-invalid-javadoc/invoker.properties
new file mode 100644
index 0000000..aa87eb6
--- /dev/null
+++ b/src/it/apis-jar-invalid-javadoc/invoker.properties
@@ -0,0 +1,21 @@
+# 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.
+
+# the goal of this check is to validate the fix for SLING-8876
+# therefore we don't need to check anything in addition to the build
+# completing succesfully
+
+invoker.goals = clean org.apache.sling:slingfeature-maven-plugin:apis-jar
+invoker.debug = true
diff --git a/src/it/apis-jar-invalid-javadoc/pom.xml b/src/it/apis-jar-invalid-javadoc/pom.xml
new file mode 100644
index 0000000..0f803ea
--- /dev/null
+++ b/src/it/apis-jar-invalid-javadoc/pom.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    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/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.sling</groupId>
+  <artifactId>slingfeature-maven-plugin-test</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0.0-SNAPSHOT</version>
+
+  <name>Apache Sling Features Maven plugin test</name>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+        <version>@project.version@</version>
+        <extensions>true</extensions>
+        <executions>
+          <execution>
+            <id>analyze</id>
+            <phase>package</phase>
+            <goals>
+              <goal>apis-jar</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <selection>
+            <filesInclude>**/*.json</filesInclude>
+          </selection>
+          <includeResources>
+            <includeResource>*.cnd</includeResource>
+            <includeResource>*.tld</includeResource>
+          </includeResources>
+          <javadocLinks>
+            <javadocLink>https://osgi.org/javadoc/r6/core/</javadocLink>
+            <javadocLink>https://osgi.org/javadoc/r6/annotation/</javadocLink>
+            <javadocLink>https://osgi.org/javadoc/r6/cmpn/</javadocLink>
+          </javadocLinks>
+          <incrementalApis>false</incrementalApis>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/it/apis-jar-invalid-javadoc/src/main/features/main.json b/src/it/apis-jar-invalid-javadoc/src/main/features/main.json
new file mode 100644
index 0000000..ec0ba98
--- /dev/null
+++ b/src/it/apis-jar-invalid-javadoc/src/main/features/main.json
@@ -0,0 +1,17 @@
+{
+  "id":"org.apache.sling:slingfeature-maven-plugin-test:1.0.0-SNAPSHOT",
+  "bundles":[
+    {
+      "id":"org.apache.servicemix.bundles:org.apache.servicemix.bundles.xmlbeans:3.1.0_1",
+      "sourceId": "org.apache.xmlbeans:xmlbeans:jar:sources:3.1.0"
+    }
+  ],
+  "api-regions:JSON|false": [
+    {
+      "name": "base",
+      "exports": [
+        "org.apache.xmlbeans.impl.xb.xmlschema"
+      ]
+    }
+  ]
+}
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
index 928e297..e266a17 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
@@ -17,10 +17,14 @@
 package org.apache.sling.feature.maven.mojos;
 
 import java.io.File;
+import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
@@ -34,11 +38,13 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.function.BiPredicate;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import javax.json.JsonArray;
@@ -136,6 +142,8 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo implements Artifac
 
     private static final String PROPERTY_BUNDLE = ApisJarMojo.class.getName() + ".bundle";
 
+    private static final BiPredicate<Path, BasicFileAttributes> IS_JAVA_FILE = (p,a) -> p.toFile().isFile() && p.toFile().getName().endsWith(JAVA_EXTENSION);
+
     /**
      * Select the features for api generation.
      */
@@ -441,14 +449,14 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo implements Artifac
                     this.apiSourceResources);
             report(sourceJar, SOURCES, apiRegion, "java");
 
-            if (sourcesDir.list().length > 0) {
+            if (calculateSubpackageDirectories(sourcesDir).size() > 0) {
                 File javadocsDir = new File(regionDir, JAVADOC);
                 generateJavadoc(sourcesDir, javadocsDir, javadocClasspath);
                 final File javadocJar = createArchive(feature.getId(), javadocsDir, apiRegion, JAVADOC, null,
                         this.apiJavadocResources);
                 report(javadocJar, JAVADOC, apiRegion, "html");
             } else {
-                getLog().warn("Javadoc JAR will NOT be generated - sources directory was empty!");
+                getLog().warn("Javadoc JAR will NOT be generated - sources directory " + sourcesDir +" was empty or contained no Java files!");
             }
         }
 
@@ -1263,12 +1271,35 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo implements Artifac
 
         // use the -subpackages to reduce the list of the arguments
         javadocExecutor.addArgument("-subpackages", false);
-        javadocExecutor.addArgument(sourcesDir.list(), File.pathSeparator);
+        javadocExecutor.addArgument(calculateSubpackageDirectories(sourcesDir), File.pathSeparator);
 
         // .addArgument("-J-Xmx2048m")
         javadocExecutor.execute(javadocDir, getLog(), this.ignoreJavadocErrors);
     }
 
+    /**
+     * Returns the list of subdirectories that have at least one Java source file
+     *
+     * @param sourcesDir the directory with source files
+     * @return a list of subpackages, potentially empty, never <code>null</code>
+     */
+    private List<String> calculateSubpackageDirectories(File sourcesDir) {
+        // make sure to only include files that have at least one java source file
+        return Arrays.stream(sourcesDir.listFiles( new FileFilter() {
+            @Override
+            public boolean accept(File pathname) {
+                try {
+                    try ( Stream<Path> javaFiles = Files.find(pathname.toPath(), Integer.MAX_VALUE, IS_JAVA_FILE)) {
+                        return javaFiles.findFirst().isPresent();
+                    }
+                } catch (IOException e) {
+                    getLog().warn("Failed scanning " + pathname + " for Java files", e);
+                    return false;
+                }
+            }
+        })).map( File::getName ).collect(Collectors.toList());
+    }
+
     private static ArtifactId newArtifacId(ArtifactId original, String classifier, String type) {
         return new ArtifactId(original.getGroupId(),
                               original.getArtifactId(),