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 4c55802  SLING-10220 : Provide a mechanism to apply configuration 
defaults
4c55802 is described below

commit 4c55802d44204dd1d5e06a09fe744e27471252d8
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Mar 16 14:28:46 2021 +0100

    SLING-10220 : Provide a mechanism to apply configuration defaults
---
 pom.xml                                            |  4 +-
 .../apache/sling/feature/maven/ProjectHelper.java  | 21 ++++++-
 .../maven/mojos/ApplyDefaultConfigMojo.java        | 69 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index e001a72..dad3b43 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,7 +17,7 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling</artifactId>
-        <version>40</version>
+        <version>41</version>
         <relativePath />
     </parent>
 
@@ -176,7 +176,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             
<artifactId>org.apache.sling.feature.extension.apiregions</artifactId>
-            <version>1.1.20</version>
+            <version>1.1.21-SNAPSHOT</version>
         </dependency>
         <!-- aux dependencies for Content-Package check -->
         <dependency>
diff --git a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java 
b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
index ed98bc2..2adc1fb 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -266,7 +266,7 @@ public abstract class ProjectHelper {
         }
         return values.isEmpty() ? null : values.iterator().next();
     }
-    
+
     private static Artifact findArtifact(final ArtifactId id, final 
Collection<Artifact> artifacts) {
         if (artifacts != null) {
             for(final Artifact artifact : artifacts) {
@@ -288,10 +288,27 @@ public abstract class ProjectHelper {
         return dir;
     }
 
+    /**
+     * Create a temporary file for the feature
+     * @param project The Maven project
+     * @param feature The feature
+     * @return Return a temporary file
+     */
     public static File createTmpFeatureFile(final MavenProject project, final 
Feature feature) {
+        return createTmpFeatureFile(project, feature, false);
+    }
+
+    /**
+     * Create a temporary file for the feature
+     * @param project The Maven project
+     * @param feature The feature
+     * @param overwrite If set to {@code true} the feature is always written 
even if the file already exists
+     * @return Return a temporary file
+     */
+    public static File createTmpFeatureFile(final MavenProject project, final 
Feature feature, final boolean overwrite) {
         final String classifier = feature.getId().getClassifier();
         final File outputFile = new File(getTmpDir(project), classifier == 
null ? "feature.json" : "feature-" + classifier + ".json");
-        if ( !outputFile.exists() ) {
+        if ( overwrite || !outputFile.exists() ) {
             outputFile.getParentFile().mkdirs();
 
             try ( final Writer writer = new FileWriter(outputFile)) {
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/ApplyDefaultConfigMojo.java
 
b/src/main/java/org/apache/sling/feature/maven/mojos/ApplyDefaultConfigMojo.java
new file mode 100644
index 0000000..8ec60ac
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/ApplyDefaultConfigMojo.java
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos;
+
+import java.util.Map;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.sling.feature.Feature;
+import 
org.apache.sling.feature.extension.apiregions.api.config.validation.FeatureValidationResult;
+import 
org.apache.sling.feature.extension.apiregions.api.config.validation.FeatureValidator;
+import org.apache.sling.feature.maven.ProjectHelper;
+
+/**
+ * This mojo applies default configurations to selected features.
+ */
+@Mojo(name = "apply-default-config",
+    defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
+    threadSafe = true)
+public class ApplyDefaultConfigMojo extends AbstractIncludingFeatureMojo {
+
+    @Parameter(name = "selection", required = true)
+    FeatureSelectionConfig selection;
+
+    @Parameter(defaultValue = "true")
+    boolean failOnValidationError;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        checkPreconditions();
+
+        getLog().info("Feature Selection: " + selection);
+
+        Map<String, Feature> selFeat = getSelectedFeatures(selection);
+        for (Map.Entry<String, Feature> entry : selFeat.entrySet()) {
+            final Feature f = entry.getValue();
+
+            final FeatureValidator validator = new FeatureValidator();
+            validator.setFeatureProvider(new BaseFeatureProvider());
+
+            final FeatureValidationResult result = validator.validate(f);
+            if ( !result.isValid() && failOnValidationError ) {
+                throw new MojoExecutionException("Unable to apply default 
configuration to invalid feature ".concat(f.getId().toMvnId()));
+            }
+
+            if ( validator.applyDefaultValues(f, result) ) {
+                getLog().info("Applied default configurations to feature 
".concat(f.getId().toMvnId()));
+                ProjectHelper.createTmpFeatureFile(project, f, true);
+            }
+        }
+    }
+}

Reply via email to