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 2018/11/07 09:24:49 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-8058 : Make configuration of slingfeature-maven-plugin consistent. Adjust repository mojos

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 f035d8d   SLING-8058 : Make configuration of slingfeature-maven-plugin consistent. Adjust repository mojos
f035d8d is described below

commit f035d8de23264c3c26dc7168bb928503a9b3d02c
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Nov 7 10:24:41 2018 +0100

     SLING-8058 : Make configuration of slingfeature-maven-plugin consistent. Adjust repository mojos
---
 .../apache/sling/feature/maven/ProjectHelper.java  |   5 +
 .../maven/mojos/AbstractIncludingFeatureMojo.java  |  14 +++
 .../maven/mojos/AbstractRepositoryMojo.java        | 101 +++------------------
 ...rtifactsMojo.java => CollectArtifactsMojo.java} |  29 +++---
 .../sling/feature/maven/mojos/Repository.java      |  43 +++++++++
 .../sling/feature/maven/mojos/RepositoryMojo.java  |  19 ++--
 .../feature/maven/mojos/UpdateVersionsMojo.java    |   5 +-
 7 files changed, 106 insertions(+), 110 deletions(-)

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 9ca28eb..620579c 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -273,6 +273,11 @@ public abstract class ProjectHelper {
         return dep;
     }
 
+    public static ArtifactId toArtifactId(final Dependency dep) {
+        return new ArtifactId(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(),
+                dep.getType());
+    }
+
     public static void setFeatureInfo(final MavenProject project, final Feature feature) {
         // set title, description, vendor, license
         if ( feature.getTitle() == null ) {
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojo.java
index 3857538..1e8721d 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojo.java
@@ -54,6 +54,20 @@ public abstract class AbstractIncludingFeatureMojo extends AbstractFeatureMojo {
         return selection;
     }
 
+    protected Map<String, Feature> selectAllFeatureFiles() throws MojoExecutionException {
+        final FeatureSelectionConfig config = new FeatureSelectionConfig();
+        config.setFilesInclude("**/*.*");
+
+        return this.getSelectedFeatures(config);
+    }
+
+    protected Map<String, Feature> selectAllFeatureFilesAndAggregates() throws MojoExecutionException {
+        final FeatureSelectionConfig config = new FeatureSelectionConfig();
+        config.setFilesInclude("**/*.*");
+        config.setIncludeClassifier("*");
+        return this.getSelectedFeatures(config);
+    }
+
     private void selectFeatureClassifiers(final FeatureSelectionConfig config, final Map<String, Feature> selection)
             throws MojoExecutionException {
         final Map<String, Feature> projectFeatures = ProjectHelper.getAssembledFeatures(this.project);
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
index 0385b67..b4cc9c7 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractRepositoryMojo.java
@@ -17,10 +17,8 @@
 package org.apache.sling.feature.maven.mojos;
 
 import java.io.File;
-import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.Reader;
 import java.io.Writer;
 import java.util.List;
 import java.util.Map;
@@ -28,7 +26,7 @@ import java.util.Map;
 import org.apache.commons.io.FileUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
-import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
@@ -37,11 +35,10 @@ import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
-import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
 import org.apache.sling.feature.maven.ProjectHelper;
 
-public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
+public abstract class AbstractRepositoryMojo extends AbstractIncludingFeatureMojo {
 
     /**
      * The directory to store the artifacts into.
@@ -52,42 +49,20 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
     @Component
     ArtifactHandlerManager artifactHandlerManager;
 
-    /**
-     * Used to look up Artifacts in the remote repository.
-     *
-     */
-    @Component
-    ArtifactResolver resolver;
-
     @Override
     public abstract void execute() throws MojoExecutionException, MojoFailureException;
 
-    protected void doExecute(final File artifactDir, final List<Include> featureIncludes, final List<Include> embed)
+    protected void doExecute(final File artifactDir, final Map<String, Feature> features, final List<Dependency> embed)
             throws MojoExecutionException, MojoFailureException {
         this.getLog().info("Creating repository in '" + artifactDir.getPath() + "'...");
 
-        final Map<String, org.apache.sling.feature.Feature> features = ProjectHelper.getAssembledFeatures(this.project);
-
-        if (featureIncludes != null && !featureIncludes.isEmpty()) {
-            for (Include include : featureIncludes) {
-                if (ProjectHelper.isLocalProjectArtifact(this.project, include.getID())) {
-                    final Feature inc = this.getLocalFeature(include.getID());
-                    if (inc == null) {
-                        throw new MojoExecutionException("Unable to find project feature " + include.getID().toMvnId());
-                    }
-                    processFeature(artifactDir, inc);
-                } else {
-                    processRemoteFeature(artifactDir, include.getID());
-                }
-            }
-        } else {
-            for (final Feature f : features.values()) {
-                processFeature(artifactDir, f);
-            }
+        for (final Feature feature : features.values()) {
+            processFeature(artifactDir, feature);
         }
+
         if (embed != null) {
-            for (Include include : embed) {
-                copyArtifactToRepository(include.getID(), artifactDir);
+            for (Dependency include : embed) {
+                copyArtifactToRepository(ProjectHelper.toArtifactId(include), artifactDir);
             }
         }
     }
@@ -118,7 +93,7 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
         try ( final Writer writer = new FileWriter(featureFile)) {
             FeatureJSONWriter.write(writer, f);
         } catch (final IOException e) {
-            throw new MojoExecutionException("Unable to write feature file ", e);
+            throw new MojoExecutionException("Unable to write feature file  :" + f.getId().toMvnId(), e);
         }
         if ( f.getInclude() != null ) {
             if (ProjectHelper.isLocalProjectArtifact(this.project, f.getInclude().getId())) {
@@ -129,33 +104,20 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
                 }
                 processFeature(artifactDir, inc);
             } else {
-                processRemoteFeature(artifactDir, f.getInclude().getId());
+                final Feature inc = ProjectHelper.getOrResolveFeature(project, mavenSession, artifactHandlerManager,
+                        artifactResolver, f.getInclude().getId());
+                processFeature(artifactDir, inc);
             }
         }
     }
 
-    protected void processRemoteFeature(final File artifactDir, final ArtifactId id) throws MojoExecutionException {
-        final Artifact source = ProjectHelper.getOrResolveArtifact(this.project,
-            this.mavenSession,
-            this.artifactHandlerManager,
-            this.resolver,
-            id);
-
-        try (final Reader reader = new FileReader(source.getFile()) ) {
-            final Feature inc = FeatureJSONReader.read(reader, id.toMvnId());
-            processFeature(artifactDir, inc);
-        } catch (final IOException e) {
-            throw new MojoExecutionException("Unable to read feature file ", e);
-        }
-    }
-
     /**
      * Get the file in the repository directory
      * @param artifactDir The base artifact directory
      * @param artifact The artifact
      * @return The file
      */
-    protected File getRepositoryFile(final File artifactDir, final org.apache.sling.feature.ArtifactId artifact) {
+    private File getRepositoryFile(final File artifactDir, final org.apache.sling.feature.ArtifactId artifact) {
         final StringBuilder artifactNameBuilder = new StringBuilder();
         artifactNameBuilder.append(artifact.getArtifactId());
         artifactNameBuilder.append('-');
@@ -188,7 +150,7 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
      * Copy a single artifact to the repository
      * @throws MojoExecutionException
      */
-    protected void copyArtifactToRepository(final ArtifactId artifactId,
+    private void copyArtifactToRepository(final ArtifactId artifactId,
             final File artifactDir)
     throws MojoExecutionException {
         final File artifactFile = getRepositoryFile(artifactDir, artifactId);
@@ -199,7 +161,7 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
         final Artifact source = ProjectHelper.getOrResolveArtifact(this.project,
                 this.mavenSession,
                 this.artifactHandlerManager,
-                this.resolver,
+                this.artifactResolver,
                 artifactId);
 
         try {
@@ -209,37 +171,4 @@ public abstract class AbstractRepositoryMojo extends AbstractFeatureMojo {
         }
     }
 
-    public static class Include {
-        String groupId;
-        String artifactId;
-        String version;
-        String type;
-        String classifier;
-
-        public void setGroupId(String gid) {
-            groupId = gid;
-        }
-
-        public void setArtifactId(String aid) {
-            artifactId = aid;
-        }
-
-        public void setVersion(String ver) {
-            version = ver;
-        }
-
-        public void setType(String t) {
-            type = t;
-        }
-
-        public void setClassifier(String clf) {
-            classifier = clf;
-        }
-
-        public ArtifactId getID()
-        {
-            return new ArtifactId(groupId, artifactId, version, classifier, type);
-        }
-    }
-
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/ArtifactsMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/CollectArtifactsMojo.java
similarity index 76%
rename from src/main/java/org/apache/sling/feature/maven/mojos/ArtifactsMojo.java
rename to src/main/java/org/apache/sling/feature/maven/mojos/CollectArtifactsMojo.java
index bfc7db4..d5ea2e0 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/ArtifactsMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/CollectArtifactsMojo.java
@@ -17,23 +17,24 @@
 package org.apache.sling.feature.maven.mojos;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.sling.feature.Feature;
 
 @Mojo(
     name = "collect-artifacts",
     requiresProject = false,
     threadSafe = true
 )
-public final class ArtifactsMojo extends AbstractRepositoryMojo {
+public final class CollectArtifactsMojo extends AbstractRepositoryMojo {
 
     private final Pattern gavPattern = Pattern.compile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)");
 
@@ -53,11 +54,11 @@ public final class ArtifactsMojo extends AbstractRepositoryMojo {
         } else {
             artifactDir = new File(this.project.getBuild().getDirectory(), repositoryDir);
         }
-        this.doExecute(artifactDir, getIncludes(), null);
+        this.doExecute(artifactDir, getFeatures(), null);
     }
 
-    protected List<Include> getIncludes() {
-        List<Include> includes = new ArrayList<>();
+    protected Map<String, Feature> getFeatures() throws MojoExecutionException {
+        final FeatureSelectionConfig config = new FeatureSelectionConfig();
 
         if (csvFeaturesGAV != null && !csvFeaturesGAV.isEmpty()) {
 
@@ -72,17 +73,17 @@ public final class ArtifactsMojo extends AbstractRepositoryMojo {
                             + " specified on 'features' property, expected format is groupId:artifactId[:packaging[:classifier]]:version");
                 }
 
-                Include include = new Include();
-                include.setGroupId(gavMatcher.group(1));
-                include.setArtifactId(gavMatcher.group(2));
-                include.setVersion(gavMatcher.group(7));
-                include.setType(gavMatcher.group(4));
-                include.setClassifier(gavMatcher.group(6));
+                final Dependency dep = new Dependency();
+                dep.setGroupId(gavMatcher.group(1));
+                dep.setArtifactId(gavMatcher.group(2));
+                dep.setVersion(gavMatcher.group(7));
+                dep.setType(gavMatcher.group(4));
+                dep.setClassifier(gavMatcher.group(6));
 
-                includes.add(include);
+                config.setIncludeArtifact(dep);
             }
         }
 
-        return includes;
+        return this.getSelectedFeatures(config);
     }
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/Repository.java b/src/main/java/org/apache/sling/feature/maven/mojos/Repository.java
new file mode 100644
index 0000000..4ee453b
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/Repository.java
@@ -0,0 +1,43 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+
+import org.apache.maven.model.Dependency;
+import org.apache.maven.plugins.annotations.Parameter;
+
+public class Repository extends FeatureSelectionConfig {
+
+    private List<Dependency> embeds = new ArrayList<>();
+
+    /**
+     * The directory for the repository to store. This directory is relative to the
+     * build directory.
+     */
+    @Parameter(defaultValue = "artifacts")
+    public String repositoryDir;
+
+    public void setEmbedArtifact(final Dependency dep) {
+        this.embeds.add(dep);
+    }
+
+    public List<Dependency> getEmbeds() {
+        return this.embeds;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/RepositoryMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/RepositoryMojo.java
index 0e9c1bb..c55e042 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/RepositoryMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/RepositoryMojo.java
@@ -18,6 +18,7 @@ package org.apache.sling.feature.maven.mojos;
 
 import java.io.File;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -25,6 +26,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.sling.feature.Feature;
 
 /**
  * Create a Maven repository structure from the referenced artifacts in the features.
@@ -38,14 +40,19 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
 public class RepositoryMojo extends AbstractRepositoryMojo {
 
     @Parameter
-    List<Include> includes;
-
-    @Parameter
-    List<Include> embed;
+    List<Repository> repositories;
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
-        final File artifactDir = new File(this.project.getBuild().getDirectory(), repositoryDir);
-        this.doExecute(artifactDir, includes, embed);
+        if (repositories == null || repositories.isEmpty()) {
+            final File artifactDir = new File(this.project.getBuild().getDirectory(), this.repositoryDir);
+            this.doExecute(artifactDir, this.selectAllFeatureFilesAndAggregates(), null);
+        } else {
+            for (final Repository rep : this.repositories) {
+                final Map<String, Feature> selection = this.getSelectedFeatures(rep);
+                final File artifactDir = new File(this.project.getBuild().getDirectory(), rep.repositoryDir);
+                this.doExecute(artifactDir, selection, rep.getEmbeds());
+            }
+        }
     }
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/UpdateVersionsMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/UpdateVersionsMojo.java
index f8c8865..220453b 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/UpdateVersionsMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/UpdateVersionsMojo.java
@@ -147,10 +147,7 @@ public class UpdateVersionsMojo extends AbstractIncludingFeatureMojo {
     }
 
     private Map<String, Feature> getFeatures() throws MojoExecutionException {
-        final FeatureSelectionConfig config = new FeatureSelectionConfig();
-        config.setFilesInclude("**/*.*");
-
-        return this.getSelectedFeatures(config);
+        return this.selectAllFeatureFiles();
     }
 
     private void addDependencies(final Set<Dependency> dependencies, final List<Artifact> artifacts) {