This is an automated email from the ASF dual-hosted git repository. simonetripodi pushed a commit to branch SLING-8020 in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
commit f00b1071a13e27f549e8284ee667e7897c2de217 Author: Simo Tripodi <[email protected]> AuthorDate: Mon Oct 15 20:05:03 2018 +0200 SLING-8020 - AttachFeaturesMojo should allow user to set title, description, vendor and license fields to the attached Feature, taking data from the POM initial checkin --- src/it/attach-metadata-from-pom/invoker.properties | 17 +++++ src/it/attach-metadata-from-pom/pom.xml | 79 ++++++++++++++++++++++ .../src/main/features/metadata-test.json | 9 +++ src/it/attach-metadata-from-pom/verify.bsh | 44 ++++++++++++ .../feature/maven/mojos/AttachFeaturesMojo.java | 27 ++++++++ 5 files changed, 176 insertions(+) diff --git a/src/it/attach-metadata-from-pom/invoker.properties b/src/it/attach-metadata-from-pom/invoker.properties new file mode 100644 index 0000000..4a5809a --- /dev/null +++ b/src/it/attach-metadata-from-pom/invoker.properties @@ -0,0 +1,17 @@ +# 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. + +invoker.goals = clean package +invoker.debug = true diff --git a/src/it/attach-metadata-from-pom/pom.xml b/src/it/attach-metadata-from-pom/pom.xml new file mode 100644 index 0000000..f20a773 --- /dev/null +++ b/src/it/attach-metadata-from-pom/pom.xml @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.sling</groupId> + <artifactId>slingfeature-maven-plugin-test</artifactId> + <packaging>jar</packaging> + <version>1.0.0-SNAPSHOT</version> + <name>Apache Sling Features Maven plugin test</name> + <description>This is just an Apache Sling Features Maven plugin test to verify added metadata</description> + <organization> + <name>The Apache Software Foundation</name> + <url>https://www.apache.org/</url> + </organization> + <licenses> + <license> + <name>Apache License, Version 2.0</name> + <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> + <distribution>repo</distribution> + </license> + </licenses> + <properties> + <interpolated_variable>true</interpolated_variable> + <osgi.framework.version>1.9.0</osgi.framework.version> + <sling.framework.install.incremental>true</sling.framework.install.incremental> + <sling.framework.install.startlevel>1</sling.framework.install.startlevel> + <sling.ignoreSystemProperties>true</sling.ignoreSystemProperties> + </properties> + <build> + <plugins> + <plugin> + <groupId>@project.groupId@</groupId> + <artifactId>@project.artifactId@</artifactId> + <version>@project.version@</version> + <extensions>true</extensions> + <executions> + <execution> + <id>aggregate</id> + <phase>generate-resources</phase> + <goals> + <goal>aggregate-features</goal> + </goals> + <configuration> + <aggregateClassifier>metadata-test</aggregateClassifier> + <aggregates> + <aggregate> + <includes> + <include>metadata-test.json</include> + </includes> + </aggregate> + </aggregates> + </configuration> + </execution> + <execution> + <id>attach</id> + <phase>package</phase> + <goals> + <goal>attach-features</goal> + </goals> + <configuration> + <addPomMetadata>true</addPomMetadata> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </project> + \ No newline at end of file diff --git a/src/it/attach-metadata-from-pom/src/main/features/metadata-test.json b/src/it/attach-metadata-from-pom/src/main/features/metadata-test.json new file mode 100644 index 0000000..746be00 --- /dev/null +++ b/src/it/attach-metadata-from-pom/src/main/features/metadata-test.json @@ -0,0 +1,9 @@ +{ + "id": "${project.groupId}:${project.artifactId}:slingfeature:slingtest:${project.version}", + "bundles":[ + { + "id":"org.osgi:org.osgi.framework:${osgi.framework.version}", + "start-level":"20" + } + ] +} diff --git a/src/it/attach-metadata-from-pom/verify.bsh b/src/it/attach-metadata-from-pom/verify.bsh new file mode 100644 index 0000000..d2fd467 --- /dev/null +++ b/src/it/attach-metadata-from-pom/verify.bsh @@ -0,0 +1,44 @@ +/* + * 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. + */ + import java.io.*; +import java.util.*; + import org.codehaus.plexus.util.*; + boolean check() { + File file = new File(basedir, "target/slingtest.json"); + String log = FileUtils.fileRead(file); + String[] values = { + "\"title\":\"Apache Sling Features Maven plugin test\"", + "\"description\":\"This is just an Apache Sling Features Maven plugin test to verify added metadata\"", + "\"vendor\":\"The Apache Software Foundation\"", + "\"license\":\"Apache License, Version 2.0\"" + }; + for (String value : values) { + if (log.indexOf(value) < 0) { + System.out.println("FAILED!"); + return false; + } + } + return true; + } + try { + return check(); + } + catch(Throwable t) { + t.printStackTrace(); + return false; + } + return true; 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 8d15cf5..8edbfd1 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,11 +21,15 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.Collection; +import java.util.function.Consumer; +import java.util.function.Function; +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; 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; @@ -42,11 +46,28 @@ import org.apache.sling.feature.maven.ProjectHelper; ) public class AttachFeaturesMojo extends AbstractFeatureMojo { + @Parameter(defaultValue = "false") + private boolean addPomMetadata; + private void attach(final Feature feature, final String classifier) throws MojoExecutionException { if ( feature != null ) { + if (addPomMetadata) { + addPomMetadata(project.getName(), title -> feature.setTitle(title)); + addPomMetadata(project.getDescription(), description -> feature.setDescription(description)); + + if (project.getOrganization() != null) { + addPomMetadata(project.getOrganization().getName(), vendor -> feature.setVendor(vendor)); + } + + if (project.getLicenses() != null && !project.getLicenses().isEmpty()) { + License license = project.getLicenses().iterator().next(); + addPomMetadata(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(); @@ -105,4 +126,10 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo { } return main; } + + private static void addPomMetadata(String value, Consumer<String> setter) { + if (value != null && !value.isEmpty()) { + setter.accept(value); + } + } }
