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/03/22 14:39:28 UTC
[sling-slingfeature-maven-plugin] branch master updated: SLING-9232
: Support ref files in aggregates and other 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 791df0e SLING-9232 : Support ref files in aggregates and other mojos
791df0e is described below
commit 791df0e6289b25f5476f2993c869faf18016693f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sun Mar 22 15:39:05 2020 +0100
SLING-9232 : Support ref files in aggregates and other mojos
---
pom.xml | 4 +-
.../apache/sling/feature/maven/ProjectHelper.java | 2 +-
.../maven/mojos/AbstractIncludingFeatureMojo.java | 49 +++++-
.../maven/mojos/FeatureSelectionConfig.java | 10 +-
.../mojos/AbstractIncludingFeatureMojoTest.java | 177 +++++++++++++++++++++
.../maven/mojos/FeatureSelectionConfigTest.java | 18 ++-
6 files changed, 247 insertions(+), 13 deletions(-)
diff --git a/pom.xml b/pom.xml
index 25e7f49..3f20aec 100644
--- a/pom.xml
+++ b/pom.xml
@@ -156,12 +156,12 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.johnzon</artifactId>
- <version>1.0.0</version>
+ <version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.feature</artifactId>
- <version>1.1.4</version>
+ <version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
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 9828b29..4c45663 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -242,7 +242,7 @@ public abstract class ProjectHelper {
if ( artifact.getGroupId().equals(id.getGroupId())
&& artifact.getArtifactId().equals(id.getArtifactId())
&& artifact.getVersion().equals(id.getVersion())
- && artifact.getType().equals(id.getVersion())
+ && artifact.getType().equals(id.getType())
&& ((id.getClassifier() == null && artifact.getClassifier() == null) || (id.getClassifier() != null && id.getClassifier().equals(artifact.getClassifier()))) ) {
return artifact;
}
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 90d8968..a72eb1e 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
@@ -17,6 +17,7 @@
package org.apache.sling.feature.maven.mojos;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -25,8 +26,10 @@ import java.util.StringTokenizer;
import java.util.TreeMap;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.shared.utils.io.DirectoryScanner;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.IOUtils;
import org.apache.sling.feature.maven.ProjectHelper;
import org.codehaus.plexus.util.AbstractScanner;
@@ -40,16 +43,21 @@ public abstract class AbstractIncludingFeatureMojo extends AbstractFeatureMojo {
for(final FeatureSelectionConfig.Selection selection : config.getSelections()) {
switch ( selection.type ) {
- case FILE_INCLUDE:
+ case FILES_INCLUDE:
hasFileInclude = true;
selectFeatureFiles(selection.instruction, config.getFilesExcludes(), result);
break;
- case AGGREGATE_CLASSIFIER:
+ case CLASSIFIER:
selectFeatureClassifier(selection.instruction, result);
break;
case ARTIFACT:
selectFeatureArtifact(selection.instruction, result);
break;
+ case REFS_INCLUDE:
+ selectRefsFiles(selection.instruction, config.getFilesExcludes(), result);
+ break;
+ default:
+ break;
}
}
@@ -134,6 +142,43 @@ public abstract class AbstractIncludingFeatureMojo extends AbstractFeatureMojo {
}
}
+ private void selectRefsFiles(final String selection, final List<String> excludes, final Map<String, Feature> result)
+ throws MojoExecutionException {
+ final DirectoryScanner scanner = new DirectoryScanner();
+ scanner.setBasedir(this.project.getBasedir());
+ if (!excludes.isEmpty()) {
+ scanner.setExcludes(excludes.toArray(new String[excludes.size()]));
+ }
+ scanner.setIncludes(new String[] { selection });
+ scanner.scan();
+
+ if (!selection.contains("*") && scanner.getIncludedFiles().length == 0 ) {
+ throw new MojoExecutionException("RefsInclude " + selection + " not found.");
+ }
+ for(final String path : scanner.getIncludedFiles()) {
+ final File selectedFile = new File(this.project.getBasedir(), path.replace('/', File.separatorChar));
+ try {
+ final List<String> urls = IOUtils.parseFeatureRefFile(selectedFile);
+ for(final String url : urls) {
+ try {
+ final ArtifactId id = ArtifactId.parse(url);
+ if (ProjectHelper.isLocalProjectArtifact(this.project, id)) {
+ throw new MojoExecutionException(
+ "RefsFile configuration is used to select a local feature: " + id.toMvnId());
+ }
+ final Feature feature = ProjectHelper.getOrResolveFeature(this.project, this.mavenSession,
+ this.artifactHandlerManager, this.artifactResolver, id);
+ result.put(id.toMvnUrl(), feature);
+ } catch ( final IllegalArgumentException e) {
+ throw new MojoExecutionException("Reference " + url + " in " + selectedFile.getAbsolutePath() + " is not a supported url.");
+ }
+ }
+ } catch (final IOException e) {
+ throw new MojoExecutionException(e.getMessage(), e);
+ }
+ }
+ }
+
private void selectFeatureArtifact(final String artifactId, final Map<String, Feature> result)
throws MojoExecutionException {
final ArtifactId id = ArtifactId.parse(artifactId);
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfig.java b/src/main/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfig.java
index 430b975..c9a7b13 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfig.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfig.java
@@ -25,7 +25,7 @@ import org.apache.sling.feature.maven.ProjectHelper;
public class FeatureSelectionConfig {
enum SelectionType {
- FILE_INCLUDE, AGGREGATE_CLASSIFIER, ARTIFACT
+ FILES_INCLUDE, CLASSIFIER, ARTIFACT, REFS_INCLUDE
}
static class Selection {
@@ -51,19 +51,23 @@ public class FeatureSelectionConfig {
}
public void setFilesInclude(final String val) {
- selections.add(new Selection(SelectionType.FILE_INCLUDE, val));
+ selections.add(new Selection(SelectionType.FILES_INCLUDE, val));
}
public void setFilesExclude(final String val) {
this.filesExcludes.add(val);
}
+ public void setRefsInclude(final String val) {
+ selections.add(new Selection(SelectionType.REFS_INCLUDE, val));
+ }
+
public void setIncludeArtifact(final Dependency a) {
selections.add(new Selection(SelectionType.ARTIFACT, ProjectHelper.toArtifactId(a).toMvnId()));
}
public void setIncludeClassifier(final String classifier) {
- selections.add(new Selection(SelectionType.AGGREGATE_CLASSIFIER, ":".equals(classifier) ? "" : classifier));
+ selections.add(new Selection(SelectionType.CLASSIFIER, ":".equals(classifier) ? "" : classifier));
}
public List<String> getFilesExcludes() {
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojoTest.java b/src/test/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojoTest.java
new file mode 100644
index 0000000..06b1a07
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/AbstractIncludingFeatureMojoTest.java
@@ -0,0 +1,177 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.utils.io.FileUtils;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONWriter;
+import org.apache.sling.feature.maven.ProjectHelper;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class AbstractIncludingFeatureMojoTest {
+
+ private AbstractIncludingFeatureMojo mojo;
+
+ @Before
+ public void setup() {
+ mojo = new AbstractIncludingFeatureMojo() {
+
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ // nothing to do
+ }
+ };
+
+ mojo.project = Mockito.mock(MavenProject.class);
+ Mockito.when(mojo.project.getGroupId()).thenReturn("g");
+ Mockito.when(mojo.project.getArtifactId()).thenReturn("a");
+ Mockito.when(mojo.project.getVersion()).thenReturn("1");
+ }
+
+ @Test
+ public void testSelectAggregateClassifierNotFound() throws Exception {
+ final FeatureSelectionConfig fsc = new FeatureSelectionConfig();
+ fsc.setIncludeClassifier("agg");
+
+ try {
+ mojo.getSelectedFeatures(fsc);
+ fail();
+ } catch ( MojoExecutionException mee) {
+ assertTrue(mee.getMessage().contains(" agg not found"));
+ }
+ }
+
+ @Test
+ public void testSelectAggregateClassifier() throws Exception {
+ setupProjectFeatures(mojo.project, "agg", "foo");
+
+ final FeatureSelectionConfig fsc = new FeatureSelectionConfig();
+ fsc.setIncludeClassifier("agg");
+
+ final Map<String, Feature> selection = mojo.getSelectedFeatures(fsc);
+ assertEquals(1, selection.size());
+ assertEquals("agg", selection.values().iterator().next().getId().getClassifier());
+ }
+
+
+ @Test
+ public void testSelectAggregateClassifierFromFiles() throws Exception {
+ setupProjectFeatures(mojo.project, Collections.singletonMap("/foo/file", new Feature(ArtifactId.parse("g:a:slingosgifeature:foo:1"))));
+
+ final FeatureSelectionConfig fsc = new FeatureSelectionConfig();
+ fsc.setIncludeClassifier("foo");
+
+ final Map<String, Feature> selection = mojo.getSelectedFeatures(fsc);
+ assertEquals(1, selection.size());
+ assertEquals("foo", selection.values().iterator().next().getId().getClassifier());
+ }
+
+ @Test
+ public void testSelectRefFiles() throws Exception {
+ final File baseDirectory = Files.createTempDirectory("ref-files").toFile();
+ try {
+ Mockito.when(mojo.project.getBasedir()).thenReturn(baseDirectory);
+
+ final ArtifactId a1 = ArtifactId.parse("g1:a1:1.0");
+ final ArtifactId a2 = ArtifactId.parse("g2:a2:2.0");
+
+ final Feature f1 = new Feature(a1);
+ final Feature f2 = new Feature(a2);
+
+ try ( final Writer writer = new FileWriter(new File(baseDirectory, "f1.json"))) {
+ FeatureJSONWriter.write(writer, f1);
+ }
+ try ( final Writer writer = new FileWriter(new File(baseDirectory, "f2.json"))) {
+ FeatureJSONWriter.write(writer, f2);
+ }
+
+ final Set<Artifact> depArtifacts = new HashSet<>();
+ final Artifact art1 = new DefaultArtifact(a1.getGroupId(), a1.getArtifactId(), a1.getVersion(),
+ Artifact.SCOPE_PROVIDED, a1.getType(), a1.getClassifier(), new DefaultArtifactHandler());
+ art1.setFile(new File(baseDirectory, "f1.json"));
+ depArtifacts.add(art1);
+
+ final Artifact art2 = new DefaultArtifact(a2.getGroupId(), a2.getArtifactId(), a2.getVersion(),
+ Artifact.SCOPE_PROVIDED, a2.getType(), a2.getClassifier(), new DefaultArtifactHandler());
+ art2.setFile(new File(baseDirectory, "f2.json"));
+ depArtifacts.add(art2);
+
+ Mockito.when(mojo.project.getDependencyArtifacts()).thenReturn(depArtifacts);
+
+ final File refsDir = new File(baseDirectory, "src" + File.separatorChar + "main" + File.separatorChar + "references");
+ refsDir.mkdirs();
+
+ final List<String> refs = new ArrayList<>();
+ refs.add("# Some comment");
+ refs.add(a1.toMvnUrl());
+ refs.add(a2.toMvnUrl());
+ Files.write(new File(refsDir, "artifacts.refs").toPath(), refs);
+ final List<String> garbage = Collections.singletonList("foo");
+ Files.write(new File(refsDir, "artifacts.txt").toPath(), garbage);
+
+ final FeatureSelectionConfig fsc = new FeatureSelectionConfig();
+ fsc.setRefsInclude("src/main/references/*.refs");
+
+ final Map<String, Feature> selection = mojo.getSelectedFeatures(fsc);
+ assertEquals(2, selection.size());
+ final List<ArtifactId> ids = new ArrayList<>();
+ selection.values().stream().forEach(feature -> ids.add(feature.getId()));
+ assertTrue(ids.contains(a1));
+ assertTrue(ids.contains(a2));
+
+ } finally {
+ FileUtils.deleteDirectory(baseDirectory);
+ }
+ }
+
+ private void setupProjectFeatures(final MavenProject project, final Map<String, Feature> features) {
+ Mockito.when(project.getContextValue(Mockito.anyString())).thenReturn(features);
+ }
+
+ private void setupProjectFeatures(final MavenProject project, String... aggregates) {
+ final Map<String, Feature> map = new HashMap<>();
+ for(final String agg : aggregates) {
+ map.put(ProjectHelper.generateAggregateFeatureKey(agg, true), new Feature(ArtifactId.parse("g:a:slingosgifeature:" + agg + ":1")));
+ }
+ setupProjectFeatures(project, map);
+ }
+}
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfigTest.java b/src/test/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfigTest.java
index 6eee31d..c54f293 100644
--- a/src/test/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfigTest.java
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/FeatureSelectionConfigTest.java
@@ -33,26 +33,34 @@ public class FeatureSelectionConfigTest {
cfg.setFilesInclude("INC1");
cfg.setIncludeClassifier("another");
cfg.setFilesInclude("INC2");
+ cfg.setRefsInclude("src/main/references/**.ref");
+ cfg.setFilesInclude("INC3");
// we should have five items
- assertEquals(5, cfg.getSelections().size());
+ assertEquals(7, cfg.getSelections().size());
// check ordering
- assertEquals(FeatureSelectionConfig.SelectionType.AGGREGATE_CLASSIFIER, cfg.getSelections().get(0).type);
+ assertEquals(FeatureSelectionConfig.SelectionType.CLASSIFIER, cfg.getSelections().get(0).type);
assertEquals("classifier", cfg.getSelections().get(0).instruction);
assertEquals(FeatureSelectionConfig.SelectionType.ARTIFACT, cfg.getSelections().get(1).type);
assertEquals("gr:ar:1.1", cfg.getSelections().get(1).instruction);
- assertEquals(FeatureSelectionConfig.SelectionType.FILE_INCLUDE, cfg.getSelections().get(2).type);
+ assertEquals(FeatureSelectionConfig.SelectionType.FILES_INCLUDE, cfg.getSelections().get(2).type);
assertEquals("INC1", cfg.getSelections().get(2).instruction);
- assertEquals(FeatureSelectionConfig.SelectionType.AGGREGATE_CLASSIFIER, cfg.getSelections().get(3).type);
+ assertEquals(FeatureSelectionConfig.SelectionType.CLASSIFIER, cfg.getSelections().get(3).type);
assertEquals("another", cfg.getSelections().get(3).instruction);
- assertEquals(FeatureSelectionConfig.SelectionType.FILE_INCLUDE, cfg.getSelections().get(4).type);
+ assertEquals(FeatureSelectionConfig.SelectionType.FILES_INCLUDE, cfg.getSelections().get(4).type);
assertEquals("INC2", cfg.getSelections().get(4).instruction);
+ assertEquals(FeatureSelectionConfig.SelectionType.REFS_INCLUDE, cfg.getSelections().get(5).type);
+ assertEquals("src/main/references/**.ref", cfg.getSelections().get(5).instruction);
+
+ assertEquals(FeatureSelectionConfig.SelectionType.FILES_INCLUDE, cfg.getSelections().get(4).type);
+ assertEquals("INC3", cfg.getSelections().get(6).instruction);
+
// check excludes
assertEquals(1, cfg.getFilesExcludes().size());
assertEquals("EXCLUDE", cfg.getFilesExcludes().get(0));