You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/10/01 06:56:04 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-9779 : Evaluate toggle configuration when generating api jars

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 97ab04f  SLING-9779 : Evaluate toggle configuration when generating api jars
97ab04f is described below

commit 97ab04f61751e25cac97d59b3d4b9ce117eced7f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Oct 1 08:55:37 2020 +0200

    SLING-9779 : Evaluate toggle configuration when generating api jars
---
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 55 ++++++++++++++++++----
 .../maven/mojos/apis/ApisConfiguration.java        | 16 +++++++
 2 files changed, 62 insertions(+), 9 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 ad12b56..76aeb56 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
@@ -344,6 +344,12 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
     @Parameter
     private List<String> javadocClasspathTops;
 
+    /**
+     * A comma separated list of enabled toggles used as input to generate the api jars.
+     * @since 1.5.0
+     */
+    @Parameter(property = "enabled.toggles")
+    private String enabledToggles;
 
     @Parameter(defaultValue = "${project.build.directory}/apis-jars", readonly = true)
     private File mainOutputDir;
@@ -500,6 +506,7 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
         ctx.getConfig().setRegionMappings(apiRegionNameMappings);
         ctx.getConfig().setManifestEntries(manifestProperties);
         ctx.getConfig().logConfiguration(getLog());
+        ctx.getConfig().setEnabledToggles(this.enabledToggles);
 
         ctx.setDependencyRepositories(this.apiRepositoryUrls);
 
@@ -587,7 +594,7 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                 .a(" succesfully created").toString());
     }
 
-    private void report(final ApisJarContext ctx,
+	private void report(final ApisJarContext ctx,
             final File jarFile,
             final ArtifactType artifactType,
             final ApiRegion apiRegion,
@@ -731,7 +738,7 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
      * @param artifact The artifact
      * @throws MojoExecutionException
      */
-    private void onArtifact(final ApisJarContext ctx, final Artifact artifact)
+    private void onArtifact(final ApisJarContext ctx, Artifact artifact)
     throws MojoExecutionException {
         final File bundleFile = getArtifactFile(artifactProvider, artifact.getId());
 
@@ -742,9 +749,33 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
         if (exportedPackageClauses.length > 0) {
 
             // calculate the exported packages in the manifest file for all regions
-            final Set<String> usedExportedPackages = computeUsedExportPackages(ctx.getApiRegions(), exportedPackageClauses, artifact);
+            final Set<String> usedExportedPackages = computeUsedExportPackages(ctx, exportedPackageClauses, artifact);
 
             if ( !usedExportedPackages.isEmpty()) {
+                // check for previous version of artifact due to toggles
+                ArtifactId previous = null;
+                for(final String pckName : usedExportedPackages) {
+                    for(final ApiRegion region : ctx.getApiRegions().listRegions()) {
+                        final ApiExport exp = region.getExportByName(pckName);
+                        if ( exp != null ) {
+                            if ( exp.getToggle() != null && !ctx.getConfig().getEnabledToggles().contains(exp.getToggle()) && exp.getPrevious() != null ) {
+                                if ( previous != null && previous.compareTo(exp.getPrevious()) != 0 ) {
+                                    throw new MojoExecutionException("More than one previous version artifact configured for " + 
+                                        artifact.getId().toMvnId() + " : " + previous.toMvnId() + ", " + exp.getPrevious().toMvnId());
+                                }
+                                previous = exp.getPrevious();
+                            }                        
+                            break;
+                        }
+                    }
+                }
+                if ( previous != null ) {
+                    final Artifact previousArtifact = new Artifact(previous);
+                    previousArtifact.getMetadata().putAll(artifact.getMetadata());
+                    getLog().debug("Using " + previous.toMvnId() + " instead of " + artifact.getId().toMvnId() + " due to disabled toggle(s)");
+                    artifact = previousArtifact;
+                }
+
                 final ArtifactInfo info = ctx.addArtifactInfo(artifact);
                 info.setUsedExportedPackages(usedExportedPackages);
 
@@ -803,7 +834,6 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                     ApisUtil.buildJavadocClasspath(getLog(), repositorySystem, mavenSession, artifact.getId()).forEach( ctx::addJavadocClasspath );
                 }
             }
-
         }
     }
 
@@ -1404,22 +1434,29 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
     /**
      * Compute exports based on all regions
      *
-     * @return List of packages exported by this bundle and used in any region
+     * @return Set of packages exported by this bundle and used in any region
      */
-    private Set<String> computeUsedExportPackages(final ApiRegions apiRegions,
+    private Set<String> computeUsedExportPackages(final ApisJarContext ctx,
             final Clause[] exportedPackages,
             final Artifact bundle)
-            throws MojoExecutionException {
+            throws MojoExecutionException {        
         final Set<String> result = new HashSet<>();
 
         // filter for each region
         for (final Clause exportedPackage : exportedPackages) {
             final String packageName = exportedPackage.getName();
 
-            for (ApiRegion apiRegion : apiRegions.listRegions()) {
+            for (ApiRegion apiRegion : ctx.getApiRegions().listRegions()) {
                 final ApiExport exp = apiRegion.getExportByName(packageName);
                 if (exp != null) {
-                    result.add(exportedPackage.getName());
+                    boolean include = true;
+                    // if the package is behind a toggle, don't include it
+                    if ( exp.getToggle() != null && !ctx.getConfig().getEnabledToggles().contains(exp.getToggle()) && exp.getPrevious() == null ) {
+                        include = false;
+                    }
+                    if ( include ) {
+                        result.add(exportedPackage.getName());
+                    }
                 }
             }
         }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisConfiguration.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisConfiguration.java
index 4a0a5e6..f63a665 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisConfiguration.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisConfiguration.java
@@ -18,9 +18,11 @@ package org.apache.sling.feature.maven.mojos.apis;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 import javax.json.JsonArray;
 import javax.json.JsonObject;
@@ -133,6 +135,8 @@ public class ApisConfiguration {
 
     private final Map<String, String> manifestEntries = new HashMap<>();
 
+    private final Set<String> enabledToggles = new HashSet<>();
+    
     public ApisConfiguration(final Feature feature) throws MojoExecutionException {
         // check for extension
         final Extension ext = feature.getExtensions().getByName(EXTENSION_NAME);
@@ -401,4 +405,16 @@ public class ApisConfiguration {
             this.manifestEntries.putAll(ProjectHelper.propertiesToMap(valuesFromProject));
         }
     }
+
+    public void setEnabledToggles(final String value) {
+        if (value != null ) {
+            for(final String name : value.split(",")) {
+                enabledToggles.add(name.trim());
+            }
+        }        
+    }
+
+    public Set<String> getEnabledToggles() {
+        return this.enabledToggles;
+    }
 }