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 0a11d70 SLING-8042 : The slingfeature-maven-plugin should be able to
derive the name of a feature from the file name
0a11d70 is described below
commit 0a11d7051c399bb9c289ae9c37ba600b1eb7cc34
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Oct 19 19:16:08 2018 +0200
SLING-8042 : The slingfeature-maven-plugin should be able to derive the
name of a feature from the file name
---
src/it/attach-metadata-from-pom/verify.bsh | 2 +-
.../apache/sling/feature/maven/Preprocessor.java | 79 +++++++++++++++-------
.../apache/sling/feature/maven/ProjectHelper.java | 26 ++++++-
.../feature/maven/mojos/AbstractFeatureMojo.java | 4 +-
.../feature/maven/mojos/AttachFeaturesMojo.java | 74 ++++++++------------
.../maven/mojos/AttachFeaturesMojoTest.java | 4 +-
6 files changed, 112 insertions(+), 77 deletions(-)
diff --git a/src/it/attach-metadata-from-pom/verify.bsh
b/src/it/attach-metadata-from-pom/verify.bsh
index f114d3a..aed053c 100644
--- a/src/it/attach-metadata-from-pom/verify.bsh
+++ b/src/it/attach-metadata-from-pom/verify.bsh
@@ -18,7 +18,7 @@
import java.util.*;
import org.codehaus.plexus.util.*;
boolean check() {
- File file = new File(basedir, "target/slingtest.json");
+ File file = new File(basedir,
"target/slingfeature-tmp/feature-slingtest.json");
String log = FileUtils.fileRead(file);
String[] values = {
"\"title\":\"Apache Sling Features Maven plugin test\"",
diff --git a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
index 303dc9e..d2150c4 100644
--- a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
+++ b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
@@ -21,13 +21,19 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
+import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
-import java.util.stream.Collectors;
+
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
@@ -75,6 +81,16 @@ public class Preprocessor {
classifiers.add(f.getId().getClassifier());
}
}
+ for(final Feature f : finfo.testFeatures.values()) {
+ if ( f.getId().getClassifier() == null ) {
+ throw new RuntimeException("Found test feature without
classifier in project " + finfo.project.getId());
+ } else {
+ if ( classifiers.contains(f.getId().getClassifier()) ) {
+ throw new RuntimeException("Duplicate (test) feature
classifier " + f.getId().getClassifier() + " used in project " +
finfo.project.getId());
+ }
+ classifiers.add(f.getId().getClassifier());
+ }
+ }
ProjectHelper.storeProjectInfo(finfo);
}
}
@@ -229,14 +245,47 @@ public class Preprocessor {
throw new RuntimeException("Unable to read feature " +
file.getAbsolutePath(), io);
}
- final String json =
Substitution.replaceMavenVars(info.project, sb.toString());
-
+ String json = Substitution.replaceMavenVars(info.project,
sb.toString());
+
+ // check if "id" is set
+ try (final JsonReader reader = Json.createReader(new
StringReader(json)) ) {
+ final JsonObject obj = reader.readObject();
+ if ( !obj.containsKey("id") ) {
+ final StringBuilder isb = new StringBuilder();
+ isb.append(info.project.getGroupId());
+ isb.append(':');
+ isb.append(info.project.getArtifactId());
+ isb.append(':');
+
isb.append(FeatureConstants.PACKAGING_FEATURE);
+ isb.append(':');
+ final int lastDot =
file.getName().lastIndexOf('.');
+ isb.append(file.getName().substring(0,
lastDot));
+ isb.append(':');
+ isb.append(info.project.getVersion());
+
+ final StringWriter writer = new StringWriter();
+
+ logger.debug("Generating id " + isb.toString() + " for
feature file " + file);
+ try ( final JsonGenerator generator =
Json.createGenerator(writer) ) {
+ generator.writeStartObject();
+
+ generator.write("id", isb.toString());
+
+ for(final Map.Entry<String, JsonValue> entry :
obj.entrySet()) {
+ generator.write(entry.getKey(),
entry.getValue());
+ }
+ generator.writeEnd();
+ }
+
+ json = writer.toString();
+ }
+ }
try (final Reader reader = new StringReader(json)) {
final Feature feature = FeatureJSONReader.read(reader,
file.getAbsolutePath());
this.checkFeatureId(info.project, feature);
- this.setProjectInfo(info.project, feature);
+ ProjectHelper.setProjectInfo(info.project, feature);
this.postProcessReadFeature(feature);
(config.isTestConfig() ? info.testFeatures :
info.features).put(file.getAbsolutePath(), feature);
@@ -271,28 +320,6 @@ public class Preprocessor {
return result;
}
- protected void setProjectInfo(final MavenProject project, final Feature
feature) {
- // set title, description, vendor, license
- if ( feature.getTitle() == null ) {
- feature.setTitle(project.getName());
- }
- if ( feature.getDescription() == null ) {
- feature.setDescription(project.getDescription());
- }
- if ( feature.getVendor() == null && project.getOrganization() != null
) {
- feature.setVendor(project.getOrganization().getName());
- }
- if ( feature.getLicense() == null
- && project.getLicenses() != null
- && !project.getLicenses().isEmpty()) {
- final String license = project.getLicenses().stream()
- .filter(l -> l.getName() != null )
- .map(l -> l.getName())
- .collect(Collectors.joining(", "));
-
- feature.setLicense(license);
- }
- }
protected FeatureProvider createFeatureProvider(final Environment env,
final FeatureProjectInfo info,
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 359f2b0..d819ffb 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
+import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
@@ -134,7 +135,7 @@ public abstract class ProjectHelper {
* The returned map uses the full path in the file system as the key and
* the assembled feature as a value.
* @param project The maven projet
- * @return The assembled features. The map might be empty.
+ * @return The assembled features. The map might be empty.
*/
public static Map<String, Feature> getAssembledFeatures(final MavenProject
project) {
return getFeatures(project, ASSEMBLED_FEATURE_JSON);
@@ -255,4 +256,27 @@ public abstract class ProjectHelper {
return dep;
}
+
+ public static void setProjectInfo(final MavenProject project, final
Feature feature) {
+ // set title, description, vendor, license
+ if ( feature.getTitle() == null ) {
+ feature.setTitle(project.getName());
+ }
+ if ( feature.getDescription() == null ) {
+ feature.setDescription(project.getDescription());
+ }
+ if ( feature.getVendor() == null && project.getOrganization() != null
) {
+ feature.setVendor(project.getOrganization().getName());
+ }
+ if ( feature.getLicense() == null
+ && project.getLicenses() != null
+ && !project.getLicenses().isEmpty()) {
+ final String license = project.getLicenses().stream()
+ .filter(l -> l.getName() != null )
+ .map(l -> l.getName())
+ .collect(Collectors.joining(", "));
+
+ feature.setLicense(license);
+ }
+ }
}
diff --git
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
index fc037e4..1a1eeb2 100644
---
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
+++
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
@@ -129,6 +129,8 @@ public abstract class AbstractFeatureMojo extends
AbstractMojo {
protected MavenProjectHelper projectHelper;
protected File getTmpDir() {
- return new File(this.project.getBuild().getDirectory(),
"slingfeature-tmp");
+ final File dir = new File(this.project.getBuild().getDirectory(),
"slingfeature-tmp");
+ dir.mkdirs();
+ return dir;
}
}
diff --git
a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
index 800e7bf..ee637cc 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
@@ -21,8 +21,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
-import java.util.function.Consumer;
-import org.apache.maven.model.License;
+
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -43,49 +42,36 @@ import org.apache.sling.feature.maven.ProjectHelper;
threadSafe = true
)
public class AttachFeaturesMojo extends AbstractFeatureMojo {
-
- @Parameter(defaultValue = "false")
- private boolean addPomMetadata;
+ /**
+ * Attach test features
+ */
+ @Parameter(name = "attachTestFeatures",
+ defaultValue = "false")
+ private boolean attachTestFeatures;
private void attach(final Feature feature,
final String classifier)
throws MojoExecutionException {
- if ( feature != null ) {
-
- if (addPomMetadata) {
- addPomMetadata(feature.getTitle(), project.getName(), title ->
feature.setTitle(title));
- addPomMetadata(feature.getDescription(),
project.getDescription(), description -> feature.setDescription(description));
-
- if (project.getOrganization() != null) {
- addPomMetadata(feature.getVendor(),
project.getOrganization().getName(), vendor -> feature.setVendor(vendor));
- }
-
- if (project.getLicenses() != null &&
!project.getLicenses().isEmpty()) {
- License license = project.getLicenses().iterator().next();
- addPomMetadata(feature.getLicense(), license.getName(),
licenseName -> feature.setLicense(licenseName));
- }
- }
-
- // write the feature
- final File outputFile = new
File(this.project.getBuild().getDirectory() + File.separatorChar + classifier +
".json");
- outputFile.getParentFile().mkdirs();
+ ProjectHelper.setProjectInfo(project, feature);
- try ( final Writer writer = new FileWriter(outputFile)) {
- FeatureJSONWriter.write(writer, feature);
- } catch (final IOException e) {
- throw new MojoExecutionException("Unable to write feature to "
+ outputFile, e);
- }
+ // write the feature
+ final File outputFile = new File(this.getTmpDir(), "feature-" +
classifier + ".json");
+ outputFile.getParentFile().mkdirs();
- // if this project is a feature, it's the main artifact
- if (
project.getPackaging().equals(FeatureConstants.PACKAGING_FEATURE)
- && (FeatureConstants.CLASSIFIER_FEATURE.equals(classifier))) {
- project.getArtifact().setFile(outputFile);
- } else {
+ 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);
+ }
- // otherwise attach it as an additional artifact
- projectHelper.attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE,
+ // if this project is a feature, it's the main artifact
+ if ( project.getPackaging().equals(FeatureConstants.PACKAGING_FEATURE)
+ && (FeatureConstants.CLASSIFIER_FEATURE.equals(classifier))) {
+ project.getArtifact().setFile(outputFile);
+ } else {
+ // otherwise attach it as an additional artifact
+ projectHelper.attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE,
classifier, outputFile);
- }
}
}
@@ -96,9 +82,11 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo {
attach(main, FeatureConstants.CLASSIFIER_FEATURE);
}
- final Feature test =
this.attachClassifierFeatures(ProjectHelper.getTestFeatures(this.project).values());
- if ( test != null ) {
- attach(test, FeatureConstants.CLASSIFIER_TEST_FEATURE);
+ if ( this.attachTestFeatures ) {
+ final Feature test =
this.attachClassifierFeatures(ProjectHelper.getTestFeatures(this.project).values());
+ if ( test != null ) {
+ attach(test, FeatureConstants.CLASSIFIER_TEST_FEATURE);
+ }
}
}
@@ -121,10 +109,4 @@ public class AttachFeaturesMojo extends
AbstractFeatureMojo {
}
return main;
}
-
- private static void addPomMetadata(String originalValue, String value,
Consumer<String> setter) {
- if ((originalValue == null || originalValue.isEmpty()) && value !=
null && !value.isEmpty()) {
- setter.accept(value);
- }
- }
}
diff --git
a/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
b/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
index e8f6ea9..28a621b 100644
---
a/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
+++
b/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
@@ -62,8 +62,8 @@ public class AttachFeaturesMojoTest {
af.projectHelper = helper;
af.attachClassifierFeatures(features);
- Mockito.verify(helper).attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE, "testa", new File(featuresDir,
"testa.json"));
- Mockito.verify(helper).attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE, "testd", new File(featuresDir,
"testd.json"));
+ Mockito.verify(helper).attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE, "testa", new File(featuresDir,
"slingfeature-tmp" + File.separatorChar + "feature-testa.json"));
+ Mockito.verify(helper).attachArtifact(project,
FeatureConstants.PACKAGING_FEATURE, "testd", new File(featuresDir,
"slingfeature-tmp" + File.separatorChar + "feature-testd.json"));
Mockito.verifyNoMoreInteractions(helper);
}
}