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

cziegeler 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 f2bb13d  SLING-10317 : Write a report file about used sources and 
dependencies
f2bb13d is described below

commit f2bb13dc9b9c93ceb57649d84d38e355b76c230f
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Apr 19 17:59:02 2021 +0200

    SLING-10317 : Write a report file about used sources and dependencies
---
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 117 ++++++++++++++++-----
 .../feature/maven/mojos/apis/ApisJarContext.java   |  12 ++-
 .../sling/feature/maven/mojos/apis/ApisUtil.java   |  39 +++++++
 .../feature/maven/mojos/apis/ArtifactType.java     |   3 +-
 .../feature/maven/mojos/apis/RegionSupport.java    |  27 ++---
 5 files changed, 155 insertions(+), 43 deletions(-)

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 aaa6cf9..b17f729 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
@@ -29,6 +29,7 @@ import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -513,20 +514,19 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
             onArtifact(regions, ctx, regionSupport, artifact);
         }
 
-        if (this.generateSourceJar || this.generateJavadocJar) {
-            
getLog().info("--------------------------------------------------------");
-            getLog().info("Used sources:");
-            for (final ArtifactInfo info : ctx.getArtifactInfos()) {
-                if (info.getSources().isEmpty()) {
-                    getLog().info("- ".concat(info.getId().toMvnId()).concat(" 
: NO SOURCES FOUND"));
-                } else {
-                    getLog().info(
-                            "- ".concat(info.getId().toMvnId()).concat(" : 
").concat(info.getSources().toString()));
-                }
+        final List<ArtifactInfo> additionalInfos = new ArrayList<>();
+        if ( this.generateJavadocJar ) {
+            for (final ApiRegion apiRegion : regions.listRegions()) {
+                additionalInfos.addAll(getAdditionalJavadocArtifacts(ctx, 
apiRegion, regionSupport));  
             }
-            
getLog().info("--------------------------------------------------------");
         }
 
+        // sources report
+        final List<ArtifactInfo> allInfos = new 
ArrayList<>(ctx.getArtifactInfos());
+        allInfos.addAll(additionalInfos);
+        final File sourcesReport = new File(mainOutputDir, 
this.project.getArtifactId().concat("-sources-report.txt"));
+        ApisUtil.writeSourceReport(this.generateSourceJar || 
this.generateJavadocJar, getLog(), sourcesReport, allInfos);
+
         boolean hasErrors = false;
 
         // recollect and package stuff per region
@@ -545,7 +545,10 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
 
             // run processor on sources
             if ( generateSourceJar || generateJavadocJar ) {
-                final Collection<ArtifactInfo> infos = 
ctx.getArtifactInfos(regionName, false);
+                final List<ArtifactInfo> infos = new 
ArrayList<>(ctx.getArtifactInfos(regionName, false));
+                if ( generateJavadocJar ) {
+                    infos.addAll(getAdditionalJavadocArtifacts(ctx, apiRegion, 
regionSupport));
+                }
                 this.runProcessor(ctx, apiRegion, ArtifactType.SOURCES, 
this.apiResources, infos);
             }
 
@@ -594,6 +597,35 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
                 }
             }
 
+            // write dependency report
+            final ArtifactId dependencyReportId = this.buildArtifactId(ctx, 
apiRegion, ArtifactType.DEPENDENCY_REPORT);
+            final File dependencyReportFile = new File(mainOutputDir, 
dependencyReportId.toMvnName());
+            if ( this.useApiDependencies ) {
+                final List<String> output = new ArrayList<>();
+                for(final ArtifactInfo info : ctx.getArtifactInfos(regionName, 
false)) {
+                    if ( !info.isUseAsDependencyPerRegion(regionName) && 
!"".equals(info.getNotUseAsDependencyPerRegionReason(regionName))) {
+                        output.add("- 
".concat(info.getId().toMvnId()).concat(" : 
").concat(info.getNotUseAsDependencyPerRegionReason(regionName)));
+                    }
+                }
+                Collections.sort(output);
+                if ( output.isEmpty() ) {
+                    output.add("All artifacts are used as a dependency");
+                } else {
+                    output.add(0, "The following artifacts are not used as a 
dependency:");
+                }
+                output.stream().forEach(msg -> getLog().info(msg));
+                try {
+                    Files.write(dependencyReportFile.toPath(), output);
+                } catch (final IOException e) {
+                    throw new MojoExecutionException("Unable to write " + 
dependencyReportFile, e);
+                }
+             } else {
+                if ( dependencyReportFile.exists() ) {
+                    dependencyReportFile.delete();
+                }
+            }
+            
+            // write report
             final ArtifactId reportId = this.buildArtifactId(ctx, apiRegion, 
ArtifactType.REPORT);
             final File reportFile = new File(mainOutputDir, 
reportId.toMvnName());
             if (!report.isEmpty()) {
@@ -788,18 +820,20 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
                             exportedPackageClauses, usedExportedPackages);
 
                     // check whether packages are included in api jars - or 
added as a dependency
-                    boolean useAsDependency = 
ctx.getConfig().isUseApiDependencies()
-                            ? 
regionSupport.calculateOmitDependenciesFlag(region, exportedPackageClauses,
-                                    usedExportedPackagesPerRegion)
-                            : false;
-                    if (useAsDependency) {
-                        useAsDependency = ctx.findDependencyArtifact(getLog(), 
info);
-                        if (useAsDependency) {
+                    String useAsDependency = "";
+                    if ( ctx.getConfig().isUseApiDependencies() ) {
+                        useAsDependency = 
regionSupport.calculateOmitDependenciesFlag(region, exportedPackageClauses,
+                                     usedExportedPackagesPerRegion);
+                    }
+                    if (useAsDependency == null ) {
+                        if (ctx.findDependencyArtifact(getLog(), info)) {      
                      
                             // check scm info
                             if 
(artifact.getMetadata().get(ApisUtil.SCM_LOCATION) != null) {
                                 throw new MojoExecutionException("Dependency 
artifact must not specify "
                                         + ApisUtil.SCM_LOCATION + " : " + 
artifact.getId().toMvnId());
                             }
+                        } else {
+                            useAsDependency = "Unable to find artifact in 
maven repository.";
                         }
                     }
                     info.setUsedExportedPackages(region.getName(), 
usedExportedPackagesPerRegion, useAsDependency);
@@ -1538,8 +1572,10 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
                     }
                 };
                 if ( archiveType == ArtifactType.APIS ) {
+                    getLog().info("Running processor " + p.getName() + " on 
binaries...");
                     p.processBinaries(pc, sources);
                 } else {
+                    getLog().info("Running processor " + p.getName() + " on 
sources...");
                     p.processSources(pc, sources);
                 }
             }
@@ -1751,16 +1787,10 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
                 }
     
                 if ( !exportedPackages.isEmpty() ) {
-                    info.setUsedExportedPackages(regionName, exportedPackages, 
false);
+                    info.setUsedExportedPackages(regionName, exportedPackages, 
"");
                     if ( !infoExists ) {
                         info.setUsedExportedPackages(exportedPackageNames);
                         info.setSourceDirectory(new 
File(ctx.getDeflatedSourcesDir(), info.getId().toMvnName()));
-                        final boolean skipSourceDeflate = 
info.getSourceDirectory().exists();
-                        if (skipSourceDeflate) {
-                            getLog().debug("Source for artifact " + 
info.getId().toMvnName() + " already deflated");
-                        } else {
-                            this.downloadSources(ctx, info, artifact);
-                        }
                     }
     
                     usedInfos.add(info);
@@ -1946,5 +1976,40 @@ public class ApisJarMojo extends 
AbstractIncludingFeatureMojo {
         getLog().debug("License for " + info.getId().toMvnId() + " = " + 
result);
         return result;
     }
+
+    private List<ArtifactInfo> getAdditionalJavadocArtifacts(final 
ApisJarContext ctx, final ApiRegion region, final RegionSupport regionSupport)
+    throws MojoExecutionException {
+        final List<ArtifactInfo> result = new ArrayList<>();
+        for(final Artifact artifact : 
ApisUtil.getAdditionalJavadocArtifacts(ctx, region.getName()) ) {
+            final ArtifactInfo info = new ArtifactInfo(artifact);
+
+            final Set<Clause> exportedPackages = 
regionSupport.getAllPublicPackages(ctx, artifact, 
getArtifactFile(artifact.getId()));
+            final Set<String> exportedPackageNames = new LinkedHashSet<>();
+            final Iterator<Clause> clauseIter = exportedPackages.iterator();
+            while ( clauseIter.hasNext() ) {
+                final Clause c = clauseIter.next();
+                if ( region.getAllExportByName(c.getName()) == null ) {
+                    exportedPackageNames.add(c.getName());
+                } else {
+                    clauseIter.remove();
+                }
+            }
+
+            if ( !exportedPackages.isEmpty() ) {
+                info.setUsedExportedPackages(region.getName(), 
exportedPackages, "");
+                info.setUsedExportedPackages(exportedPackageNames);
+                info.setSourceDirectory(new File(ctx.getDeflatedSourcesDir(), 
info.getId().toMvnName()));
+                final boolean skipSourceDeflate = 
info.getSourceDirectory().exists();
+                if (skipSourceDeflate) {
+                    getLog().debug("Source for artifact " + 
info.getId().toMvnName() + " already deflated");
+                    info.addSourceInfo("USE CACHE FROM PREVIOUS BUILD");
+                } else {
+                    this.downloadSources(ctx, info, artifact);
+                }
+                result.add(info);
+            }
+        }
+        return result;
+    }
 }
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
index fc1d3ed..cda3dff 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
@@ -62,7 +62,7 @@ public class ApisJarContext {
         private final Map<String, Set<Clause>> usedExportedPackagesRegion = 
new HashMap<>();
 
         /** Flag if used as dependency */
-        private final Map<String, Boolean> useAsDependencyPerRegion = new 
HashMap<>();
+        private final Map<String, String> useAsDependencyPerRegion = new 
HashMap<>();
 
         private final Set<File> includedResources = new HashSet<>();
 
@@ -120,9 +120,11 @@ public class ApisJarContext {
             return this.usedExportedPackagesRegion.get(regionName);
         }
 
-        public void setUsedExportedPackages(final String regionName, final 
Set<Clause> usedExportedPackages, final boolean useAsDependency) {
+        public void setUsedExportedPackages(final String regionName, final 
Set<Clause> usedExportedPackages, final String useAsDependency) {
             this.usedExportedPackagesRegion.put(regionName, 
usedExportedPackages);
-            this.useAsDependencyPerRegion.put(regionName, useAsDependency);
+            if ( useAsDependency != null ) {
+                this.useAsDependencyPerRegion.put(regionName, useAsDependency);
+            }
         }
 
         public String[] getUsedExportedPackageIncludes(final String 
regionName) {
@@ -135,6 +137,10 @@ public class ApisJarContext {
         }
 
         public boolean isUseAsDependencyPerRegion(final String regionName) {
+            return this.useAsDependencyPerRegion.get(regionName) == null;
+        }
+
+        public String getNotUseAsDependencyPerRegionReason(final String 
regionName) {
             return this.useAsDependencyPerRegion.get(regionName);
         }
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
index 329eb15..455d74c 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
@@ -23,6 +23,7 @@ import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -474,4 +475,42 @@ public class ApisUtil {
 
         return result;
     }
+
+    public static void writeSourceReport(final boolean write, final Log log, 
final File reportFile, final List<ArtifactInfo> infos) throws 
MojoExecutionException {
+        if (write) {
+            Collections.sort(infos, new Comparator<ArtifactInfo>(){
+
+                @Override
+                public int compare(ArtifactInfo o1, ArtifactInfo o2) {
+                    return o1.getId().compareTo(o2.getId());
+                }
+                
+            });
+            final List<String> output = new ArrayList<>();
+            for (final ArtifactInfo info : infos) {
+                if (info.getSources().isEmpty()) {
+                    output.add("- ".concat(info.getId().toMvnId()).concat(" : 
NO SOURCES FOUND"));
+                } else {
+                    output.add(
+                            "- ".concat(info.getId().toMvnId()).concat(" : 
").concat(info.getSources().toString()));
+                }
+            }
+            if ( output.isEmpty() ) {
+                output.add("NO SOURCES FOUND");
+            }
+            
log.info("--------------------------------------------------------");
+            log.info("Used sources:");
+            
log.info("--------------------------------------------------------");
+            output.stream().forEach(msg -> log.info(msg));
+            try {
+                Files.write(reportFile.toPath(), output);
+            } catch (final IOException e) {
+                throw new MojoExecutionException("Unable to write " + 
reportFile, e);
+            }
+        } else {
+            if ( reportFile.exists() ) {
+                reportFile.delete();
+            }
+        }        
+    }
 }
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java
index 54f088b..709d239 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java
@@ -23,7 +23,8 @@ public enum ArtifactType {
     JAVADOC_ALL("javadoc-all", "html", "jar"),
     DEPENDENCIES("apideps", "txt", "ref"),
     CND("cnd", "cnd", "jar"),
-    REPORT("report", "txt", "txt");
+    REPORT("report", "txt", "txt"),
+    DEPENDENCY_REPORT("dependency-report", "txt", "txt");
 
     private final String id;
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/apis/RegionSupport.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/apis/RegionSupport.java
index 7f15216..bffe86c 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/RegionSupport.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/RegionSupport.java
@@ -265,12 +265,12 @@ public class RegionSupport {
      * @param region The api region
      * @param exportedPackageClauses All exported packages
      * @param usedExportedPackagesPerRegion Used exported packages
-     * @return {@code true} if the artifact can be used as a dependency
+     * @return {@code null} if the artifact can be used as a dependency
      */
-    public boolean calculateOmitDependenciesFlag(final ApiRegion region, final 
Clause[] exportedPackageClauses,
+    public String calculateOmitDependenciesFlag(final ApiRegion region, final 
Clause[] exportedPackageClauses,
             final Set<Clause> usedExportedPackagesPerRegion) {
         // check whether all packages are exported in this region
-        boolean fullUsage = true;
+        String reason = null;
         for (final Clause c : exportedPackageClauses) {
             boolean found = false;
             for (final Clause current : usedExportedPackagesPerRegion) {
@@ -281,18 +281,19 @@ public class RegionSupport {
             }
 
             if (!found) {
-                fullUsage = false;
-                break;
-            }
-            // check deprecation - if deprecation is set, artifact can't be 
used as a
-            // dependency
-            final ApiExport exp = region.getAllExportByName(c.getName());
-            if (exp != null && (exp.getDeprecation().getPackageInfo() != null 
|| !exp.getDeprecation().getMemberInfos().isEmpty())) {
-                fullUsage = false;
-                break;
+                final String msg = "Package ".concat(c.getName()).concat(" not 
exported.");
+                reason = reason == null ? msg : reason.concat(" ").concat(msg);
+            } else {
+                // check deprecation - if deprecation is set, artifact can't 
be used as a
+                // dependency
+                final ApiExport exp = region.getAllExportByName(c.getName());
+                if (exp != null && (exp.getDeprecation().getPackageInfo() != 
null || !exp.getDeprecation().getMemberInfos().isEmpty())) {
+                    final String msg = "Package (or parts) 
".concat(c.getName()).concat(" marked as deprecated.");
+                    reason = reason == null ? msg : reason.concat(" 
").concat(msg);
+                }
             }
         }
 
-        return fullUsage;
+        return reason;
     }
 }

Reply via email to