You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/03/20 14:22:32 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-8264 - Make the APIs JARs able to generate and package -javadoc artifacts

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 35ca3a0  SLING-8264 - Make the APIs JARs able to generate and package -javadoc artifacts
35ca3a0 is described below

commit 35ca3a07da2c66258ed4cac5780e93d94b3e008e
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Wed Mar 20 15:22:25 2019 +0100

    SLING-8264 - Make the APIs JARs able to generate and package -javadoc
    artifacts
    
    building classpath and shortening arguments, putting them in argfile,
    make javadocs errors reduction to trivial number and index resources are
    generated
---
 src/it/apis-jar/pom.xml                            |   5 +
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 129 ++++++++++++++++++---
 .../sling/feature/maven/mojos/JavadocExecutor.java |  40 ++++++-
 3 files changed, 154 insertions(+), 20 deletions(-)

diff --git a/src/it/apis-jar/pom.xml b/src/it/apis-jar/pom.xml
index 1ecf87e..546d807 100644
--- a/src/it/apis-jar/pom.xml
+++ b/src/it/apis-jar/pom.xml
@@ -45,6 +45,11 @@
             <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>
         </configuration>
       </plugin>
     </plugins>
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 c8f2e90..9165d0b 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
@@ -44,6 +44,10 @@ import org.apache.felix.utils.manifest.Clause;
 import org.apache.felix.utils.manifest.Parser;
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 import org.apache.maven.archiver.MavenArchiver;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
+import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.Scm;
 import org.apache.maven.model.building.ModelBuilder;
@@ -55,6 +59,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.repository.RepositorySystem;
 import org.apache.maven.scm.ScmException;
 import org.apache.maven.scm.ScmFileSet;
 import org.apache.maven.scm.ScmTag;
@@ -91,7 +96,7 @@ import edu.emory.mathcs.backport.java.util.Arrays;
     requiresDependencyResolution = ResolutionScope.TEST,
     threadSafe = true
 )
-public class ApisJarMojo extends AbstractIncludingFeatureMojo {
+public class ApisJarMojo extends AbstractIncludingFeatureMojo implements ArtifactFilter {
 
     private static final String API_REGIONS_KEY = "api-regions";
 
@@ -143,6 +148,9 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
     @Component
     private ArchiverManager archiverManager;
 
+    @Component
+    private RepositorySystem repositorySystem;
+
     private ArtifactProvider artifactProvider;
 
     @Override
@@ -204,6 +212,8 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
         // calculate all api-regions first, taking the inheritance in account
         List<ApiRegion> apiRegions = fromJson(feature, jsonRepresentation);
 
+        Set<String> javadocClasspath = new HashSet<>();
+
         // for each artifact included in the feature file:
         for (Artifact artifact : feature.getBundles()) {
             ArtifactId artifactId = artifact.getId();
@@ -219,6 +229,10 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
 
             // download sources
             downloadSources(artifact, deflatedSourcesDir, checkedOutSourcesDir);
+
+            // to suppress any javadoc error
+
+            buildJavadocClasspath(javadocClasspath, artifactId);
         }
 
         // recollect and package stuff
@@ -241,7 +255,7 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
             inflate(feature.getId(), sourcesDir, apiRegion, SOURCES, null);
 
             File javadocsDir = new File(regionDir, JAVADOC);
-            generateJavadoc(apiRegion, sourcesDir, javadocsDir);
+            generateJavadoc(apiRegion, sourcesDir, javadocsDir, javadocClasspath);
             inflate(feature.getId(), javadocsDir, apiRegion, JAVADOC, null);
         }
 
@@ -249,6 +263,75 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                 .a(" succesfully created").toString());
     }
 
+    private void buildJavadocClasspath(Set<String> javadocClasspath, ArtifactId artifactId)
+            throws MojoExecutionException {
+        getLog().debug("Retrieving " + artifactId + " and related dependencies...");
+
+        org.apache.maven.artifact.Artifact toBeResolvedArtifact = repositorySystem.createArtifactWithClassifier(artifactId.getGroupId(),
+                                                                                                                artifactId.getArtifactId(),
+                                                                                                                artifactId.getVersion(),
+                                                                                                                artifactId.getType(),
+                                                                                                                artifactId.getClassifier());
+        ArtifactResolutionRequest request = new ArtifactResolutionRequest()
+                                            .setArtifact(toBeResolvedArtifact)
+                                            .setServers(mavenSession.getRequest().getServers())
+                                            .setMirrors(mavenSession.getRequest().getMirrors())
+                                            .setProxies(mavenSession.getRequest().getProxies())
+                                            .setLocalRepository(mavenSession.getLocalRepository())
+                                            .setRemoteRepositories(mavenSession.getRequest().getRemoteRepositories())
+                                            .setForceUpdate(false)
+                                            .setResolveRoot(true)
+                                            .setResolveTransitively(true)
+                                            .setCollectionFilter(this);
+
+        ArtifactResolutionResult result = repositorySystem.resolve(request);
+
+        if (!result.isSuccess()) {
+            if (result.hasCircularDependencyExceptions()) {
+                getLog().warn("Cyclic dependency errors detected:");
+                reportWarningMessages(result.getCircularDependencyExceptions());
+            }
+
+            if (result.hasErrorArtifactExceptions()) {
+                getLog().warn("Resolution errors detected:");
+                reportWarningMessages(result.getErrorArtifactExceptions());
+            }
+
+            if (result.hasMetadataResolutionExceptions()) {
+                getLog().warn("Metadata resolution errors detected:");
+                reportWarningMessages(result.getMetadataResolutionExceptions());
+            }
+
+            if (result.hasMissingArtifacts()) {
+                getLog().warn("Missing artifacts detected:");
+                for (org.apache.maven.artifact.Artifact missingArtifact : result.getMissingArtifacts()) {
+                    getLog().warn(" - " + missingArtifact.getId());
+                }
+            }
+
+            if (result.hasExceptions()) {
+                getLog().warn("Generic errors detected:");
+                for (Exception exception : result.getExceptions()) {
+                    getLog().warn(" - " + exception.getMessage());
+                }
+            }
+        }
+
+        for (org.apache.maven.artifact.Artifact resolvedArtifact : result.getArtifacts()) {
+            javadocClasspath.add(resolvedArtifact.getFile().getAbsolutePath());
+        }
+    }
+
+    private <E extends ArtifactResolutionException> void reportWarningMessages(Collection<E> exceptions) {
+        for (E exception : exceptions) {
+            getLog().warn(" - "
+                          + exception.getMessage()
+                          + " ("
+                          + exception.getArtifact().getId()
+                          + ")");
+        }
+    }
+
     private File retrieve(ArtifactId artifactId) {
         getLog().debug("Retrieving artifact " + artifactId + "...");
         File sourceFile = artifactProvider.provide(artifactId);
@@ -354,7 +437,7 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                 getLog().warn("SCM not defined in "
                               + artifactId
                               + " bundle neither in "
-                              + pomArtifactId
+                              + pomModel
                               + " POM file, sources can not be retrieved, then will be ignored");
                 return;
             }
@@ -595,30 +678,30 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
         }
     }
 
-    private void generateJavadoc(ApiRegion apiRegion, File sourcesDir, File javadocDir) throws MojoExecutionException {
+    private void generateJavadoc(ApiRegion apiRegion, File sourcesDir, File javadocDir, Set<String> javadocClasspath) throws MojoExecutionException {
         javadocDir.mkdirs();
 
-        JavadocExecutor javadocExecutor = new JavadocExecutor()
+        JavadocExecutor javadocExecutor = new JavadocExecutor(javadocDir.getParentFile())
                                           .addArgument("-public")
-                                          .addArgument("-d")
+                                          .addArgument("-d", false)
                                           .addArgument(javadocDir.getAbsolutePath())
-                                          .addArgument("-sourcepath")
+                                          .addArgument("-sourcepath", false)
                                           .addArgument(sourcesDir.getAbsolutePath());
 
         if (isNotEmpty(project.getName())) {
-            javadocExecutor.addArgument("-doctitle")
+            javadocExecutor.addArgument("-doctitle", false)
                            .addQuotedArgument(project.getName());
         }
 
         if (isNotEmpty(project.getDescription())) {
-            javadocExecutor.addArgument("-windowtitle")
+            javadocExecutor.addArgument("-windowtitle", false)
                            .addQuotedArgument(project.getDescription());
         }
 
         if (isNotEmpty(project.getInceptionYear())
                 && project.getOrganization() != null
                 && isNotEmpty(project.getOrganization().getName())) {
-            javadocExecutor.addArgument("-bottom")
+            javadocExecutor.addArgument("-bottom", false)
                            .addQuotedArgument(String.format("Copyright &copy; %s - %s %s. All Rights Reserved",
                                               project.getInceptionYear(),
                                               Calendar.getInstance().get(Calendar.YEAR),
@@ -629,16 +712,24 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
             javadocExecutor.addArguments("-link", javadocLinks);
         }
 
-        //javadocExecutor.addArgument("-classpath")
-        //               .addArgument(javadocClasspath, File.pathSeparator);
+        if (!javadocClasspath.isEmpty()) {
+            javadocExecutor.addArgument("-classpath", false)
+                           .addArgument(javadocClasspath, File.pathSeparator);
+        }
 
         // turn off doclint when running Java8
         // http://blog.joda.org/2014/02/turning-off-doclint-in-jdk-8-javadoc.html
         if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)) {
-            javadocExecutor.addArgument("-Xdoclint:none").addArgument("-quiet");
+            javadocExecutor.addArgument("-Xdoclint:none");
         }
 
-        javadocExecutor.addArguments(apiRegion.apis).execute(javadocDir, getLog());
+        // use the -subpackages to reduce the list of the arguments
+
+        javadocExecutor.addArgument("--allow-script-in-comments")
+                       .addArgument("-subpackages", false)
+                       .addArgument(sourcesDir.list(), File.pathSeparator)
+                       //.addArgument("-J-Xmx2048m")
+                       .execute(javadocDir, getLog());
     }
 
     private static ArtifactId newArtifacId(ArtifactId original, String classifier, String type) {
@@ -876,4 +967,14 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
         return s != null && !s.isEmpty();
     }
 
+    // artifact filter
+
+    @Override
+    public boolean include(org.apache.maven.artifact.Artifact artifact) {
+        if (org.apache.maven.artifact.Artifact.SCOPE_TEST.equals(artifact.getScope())) {
+            return false;
+        }
+        return true;
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/JavadocExecutor.java b/src/main/java/org/apache/sling/feature/maven/mojos/JavadocExecutor.java
index 2d2c7b6..2acf84c 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/JavadocExecutor.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/JavadocExecutor.java
@@ -17,8 +17,10 @@
 package org.apache.sling.feature.maven.mojos;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Collection;
 
@@ -33,15 +35,32 @@ final class JavadocExecutor {
 
     private static final char QUOTE_CHAR = '"';
 
-    private final CommandLine javadocCommand;
+    private final File argFile;
 
-    public JavadocExecutor() throws MojoExecutionException {
-        javadocCommand = new CommandLine(getJavadocExecutable());
-        addArgument("-J-Xmx2048m");
+    private final PrintWriter argFileWriter;
+
+    public JavadocExecutor(File regionDir) throws MojoExecutionException {
+        argFile = new File(regionDir, regionDir.getName() + "-javadoc");
+        try {
+            argFileWriter = new PrintWriter(argFile);
+        } catch (FileNotFoundException e) {
+            throw new MojoExecutionException("Impossible to create the javadoc arg file on " + argFile, e);
+        }
     }
 
     public <T> JavadocExecutor addArgument(T value) {
-        javadocCommand.addArgument(String.valueOf(value));
+        return this.addArgument(value, true);
+    }
+
+    public <T> JavadocExecutor addArgument(T value, boolean newLine) {
+        String stringValue = String.valueOf(value);
+
+        if (newLine) {
+            argFileWriter.println(stringValue);
+        } else {
+            argFileWriter.print(stringValue);
+            argFileWriter.print(' ');
+        }
         return this;
     }
 
@@ -49,6 +68,10 @@ final class JavadocExecutor {
         return addArgument(StringUtils.quoteAndEscape(String.valueOf(value), QUOTE_CHAR));
     }
 
+    public <T> JavadocExecutor addArgument(T[] value, String valueSeparator) {
+        return addArgument(StringUtils.join(value, valueSeparator));
+    }
+
     public <T> JavadocExecutor addArgument(Collection<T> value, String valueSeparator) {
         return addArgument(StringUtils.join(value.iterator(), valueSeparator));
     }
@@ -66,13 +89,18 @@ final class JavadocExecutor {
 
     public <T> JavadocExecutor addArguments(String key, Collection<T> value) {
         for (T current : value) {
-            addArgument(key);
+            addArgument(key, false);
             addArgument(current);
         }
         return this;
     }
 
     public void execute(File workingDir, Log logger) throws MojoExecutionException {
+        argFileWriter.close();
+
+        CommandLine javadocCommand = new CommandLine(getJavadocExecutable());
+        javadocCommand.addArgument('@' + argFile.getAbsolutePath(), false);
+
         logger.info("Executing javadoc tool: " + javadocCommand);
 
         DefaultExecutor executor = new DefaultExecutor();