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 2019/04/30 09:18:06 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-8382 : Add new mojo to include features in the artifact

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 734b158  SLING-8382 : Add new mojo to include features in the artifact
734b158 is described below

commit 734b158ee4dfe2b961c9eb2f63869e9a189d43d1
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Apr 30 11:17:57 2019 +0200

    SLING-8382 : Add new mojo to include features in the artifact
---
 .../feature/maven/mojos/IncludeFeaturesMojo.java   | 79 ++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/IncludeFeaturesMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/IncludeFeaturesMojo.java
new file mode 100644
index 0000000..eebb8f6
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/IncludeFeaturesMojo.java
@@ -0,0 +1,79 @@
+/*
+ * 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.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+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.maven.plugins.annotations.ResolutionScope;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONWriter;
+import org.apache.sling.feature.maven.ProjectHelper;
+
+/**
+ * Include the features in the resources
+ */
+@Mojo(name = "include-features", defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
+      requiresDependencyResolution = ResolutionScope.TEST,
+      threadSafe = true
+    )
+public class IncludeFeaturesMojo extends AbstractFeatureMojo {
+
+    /** Path where the features are included. */
+    @Parameter(defaultValue = "META-INF/features")
+    private String resourcesPath;
+
+    /**
+     * The project's build output directory (i.e. target/classes).
+     */
+    @Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true)
+    private File buildOutputDirectory;
+
+    private void include(final File directory, final Feature feature)
+    throws MojoExecutionException {
+        // write the feature
+        final String classifier = feature.getId().getClassifier();
+        final File outputFile = new File(directory,
+                classifier == null ? "feature.json" : "feature-" + classifier + ".json");
+        outputFile.getParentFile().mkdirs();
+
+        try ( final Writer writer = new FileWriter(outputFile)) {
+            FeatureJSONWriter.write(writer, feature);
+        } catch (final IOException e) {
+            throw new MojoExecutionException("Unable to write feature " + feature.getId().toMvnId() + " to " + outputFile, e);
+        }
+    }
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        ProjectHelper.checkPreprocessorRun(this.project);
+
+        getLog().info("Including features at " + this.resourcesPath);
+
+        final File directory = new File(buildOutputDirectory, this.resourcesPath.replace('/', File.separatorChar));
+        for (final Feature f : ProjectHelper.getFeatures(this.project).values()) {
+            this.include(directory, f);
+        }
+    }
+}