You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2020/11/23 13:35:17 UTC

[sling-feature-converter-maven-plugin] 07/37: Added the installation of the FM created by the converter if this plugin is used on a Content Package module

This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-feature-converter-maven-plugin.git

commit bba261319c4a08ce6fe2b0b43c213df97498470d
Author: Andreas Schaefer <sc...@iMac.local>
AuthorDate: Fri Nov 15 16:43:42 2019 -0800

    Added the installation of the FM created by the converter if this plugin is used on a Content Package module
---
 .../cpconverter/maven/mojos/ContentPackage.java    |  38 ++++++--
 .../cpconverter/maven/mojos/ConvertCPMojo.java     |  59 +++++++-----
 src/site/markdown/usage.md.vm                      | 100 ++++++++++++++++++++-
 3 files changed, 170 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java
index 0b2b36c..295f270 100644
--- a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java
+++ b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ContentPackage.java
@@ -21,7 +21,9 @@ import org.apache.maven.project.MavenProject;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import java.lang.reflect.Array;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
@@ -38,12 +40,14 @@ public class ContentPackage {
      */
     private String artifactId = "";
 
-    private String type = "zip";
+    private List<String> types = new ArrayList(Arrays.asList("zip"));
 
     // TODO: Classifier should not be set as we have the one from the converter
     private String classifier = "";
 
     private boolean excludeTransitive;
+
+    private boolean moduleIsContentPackage;
     
     public void setGroupId(String groupId) {
         this.groupId = groupId == null ? "" : groupId;
@@ -60,7 +64,20 @@ public class ContentPackage {
     public boolean isExcludeTransitive() {
         return excludeTransitive;
     }
-    
+
+    public void setModuleIsContentPackage(boolean moduleIsContentPackage) {
+        this.moduleIsContentPackage = moduleIsContentPackage;
+        if(this.moduleIsContentPackage) {
+            types.add("content-package");
+        } else {
+            types.remove("content-package");
+        }
+    }
+
+    public boolean isModuleIsContentPackage() {
+        return moduleIsContentPackage;
+    }
+
     public Collection<Artifact> getMatchingArtifacts(final MavenProject project) {
         // get artifacts depending on whether we exclude transitives or not
         final Set<Artifact> artifacts;
@@ -73,15 +90,26 @@ public class ContentPackage {
             // all dependencies, transitives included
             artifacts = project.getArtifacts();
         }
+        // Add the project artifact itself to convert after building a content package
+        if(moduleIsContentPackage) {
+            Artifact projectArtifact = project.getArtifact();
+            System.out.println("Project Artifact: " + projectArtifact);
+            artifacts.add(projectArtifact);
+        }
         return getMatchingArtifacts(artifacts);
     }
 
     public Collection<Artifact> getMatchingArtifacts(final Collection<Artifact> artifacts) {
         final List<Artifact> matches = new ArrayList<Artifact>();
         for (Artifact artifact : artifacts) {
+            System.out.println("Check Artifact: " + artifact);
+            System.out.println("Check Artifact Group: " + artifact.getGroupId());
+            System.out.println("Check Artifact Artifact: " + artifact.getArtifactId());
+            System.out.println("Check Artifact Type: " + artifact.getType());
+            System.out.println("Check Artifact Classifier: " + artifact.getClassifier());
             if(groupId.equals(artifact.getGroupId())
                 && artifactId.equals(artifact.getArtifactId())
-                && type.equals(artifact.getType())
+                && types.contains(artifact.getType())
                 && (classifier.equals(artifact.getClassifier()) || (classifier.equals("") && artifact.getClassifier() == null))
             ) {
                 matches.add(artifact);
@@ -98,8 +126,8 @@ public class ContentPackage {
         builder.append("groupId=").append(groupId).append(",");
         builder.append("artifactId=").append(artifactId).append(",");
 
-        if (type != null) {
-            builder.append("type=").append(type).append(",");
+        if (types != null) {
+            builder.append("type='").append(types).append("',");
         }
         if (classifier != null) {
             builder.append("classifier=").append(classifier).append(",");
diff --git a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java
index 4638912..a7921c5 100644
--- a/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java
+++ b/src/main/java/org/apache/sling/cpconverter/maven/mojos/ConvertCPMojo.java
@@ -17,7 +17,9 @@
 package org.apache.sling.cpconverter.maven.mojos;
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.ArtifactUtils;
 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.plugins.annotations.Mojo;
@@ -78,6 +80,8 @@ public final class ConvertCPMojo
 
     public static final String CFG_CONTENT_PACKAGES = "packages";
 
+    public static final String CFG_IS_CONTENT_PACKAGE = "isContentPackage";
+
     public static final boolean DEFAULT_STRING_VALIDATION = false;
 
     public static final boolean DEFAULT_MERGE_CONFIGURATIONS = false;
@@ -90,6 +94,8 @@ public final class ConvertCPMojo
 
     public static final String DEFAULT_FM_OUTPUT_DIRECTORY = DEFAULT_CONVERTED_CP_OUTPUT_DIRECTORY + "/fm.out";
 
+    public static final boolean DEFAULT_IS_CONTENT_PACKAGE = false;
+
     /**
      * If set to {@code true} the Content Package is strictly validated.
      */
@@ -151,6 +157,12 @@ public final class ConvertCPMojo
     @Parameter(property = CFG_CONTENT_PACKAGES)
     private List<ContentPackage> contentPackages;
 
+    /**
+     * If set to {@code true} the module is handled as Content Package
+     */
+    @Parameter(property = CFG_IS_CONTENT_PACKAGE, defaultValue = DEFAULT_IS_CONTENT_PACKAGE + "")
+    private boolean isContentPackage;
+
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         // Un-encode a given Artifact Override Id
@@ -207,18 +219,14 @@ public final class ConvertCPMojo
                 File targetFile = new File(targetPath);
                 if (targetFile.exists() && targetFile.isFile() && targetFile.canRead()) {
                     converter.convert(project.getArtifact().getFile());
-//                    Artifact convertedPackage = new DefaultArtifact(
-//                        project.getGroupId(), project.getArtifactId(), project.getVersion(),
-//                        "compile", ZIP_TYPE, PACKAGE_CLASSIFIER, artifactHandlerManager.getArtifactHandler(ZIP_TYPE)
-//                    );
-//                    installConvertedCP(convertedPackage);
                 } else {
                     getLog().error("Artifact is not found: " + targetPath);
                 }
             } else {
                 for(ContentPackage contentPackage: contentPackages) {
-                    getLog().info("Content Package Artifact File: " + contentPackage.toString());
+                    getLog().info("Content Package Artifact File: " + contentPackage.toString() + ", is module CP: " + isContentPackage);
                     contentPackage.setExcludeTransitive(true);
+                    contentPackage.setModuleIsContentPackage(isContentPackage);
                     final Collection<Artifact> artifacts = contentPackage.getMatchingArtifacts(project);
                     if (artifacts.isEmpty()) {
                         getLog().warn("No matching artifacts for " + contentPackage);
@@ -227,6 +235,7 @@ public final class ConvertCPMojo
                     getLog().info("Target Convert CP of --- " + contentPackage + " ---");
                     for (final Artifact artifact : artifacts) {
                         final File source = artifact.getFile();
+                        getLog().info("Artifact: '" + artifact + "', source file: '" + source + "'");
                         if (source.exists() && source.isFile() && source.canRead()) {
                             converter.convert(source);
                             Artifact convertedPackage = new DefaultArtifact(
@@ -258,6 +267,9 @@ public final class ConvertCPMojo
                 File destFolder = new File(userHome, ".m2/repository");
                 if(destFolder.isDirectory()) {
                     copyFiles(convertedCPOutput, destFolder);
+                    if(isContentPackage) {
+                        installFMDescriptor(project.getArtifact());
+                    }
                 }
             }
         }
@@ -312,24 +324,29 @@ public final class ConvertCPMojo
         }
     }
 
-    private void installConvertedCP(Artifact artifact) throws MojoFailureException, MojoExecutionException {
+    private void installFMDescriptor(Artifact artifact) {
         if(installConvertedCP) {
             Collection<Artifact> artifacts = Collections.synchronizedCollection(new ArrayList<>());
-            // Rebuild the converted package path
-            String convertedPackagePath = artifact.getGroupId().replaceAll("\\.", "/")
-                + "/" + artifact.getArtifactId()
-                + "/" + artifact.getVersion()
-                + "/" + artifact.getArtifactId()
-                + "-" + artifact.getVersion()
-                + "-" + PACKAGE_CLASSIFIER
-                + "." + ZIP_TYPE;
-            File convertedPackageFile = new File(convertedCPOutput, convertedPackagePath);
-            if(convertedPackageFile.exists() && convertedPackageFile.canRead()) {
-                artifact.setFile(convertedPackageFile);
-                artifacts.add(artifact);
-                installArtifact(mavenSession.getProjectBuildingRequest(), artifacts);
+            // Source FM Descriptor File Path
+            String fmDescriptorFilePath = fmPrefix + artifact.getArtifactId() + ".json";
+            File fmDescriptorFile = new File(fmOutput, fmDescriptorFilePath);
+            if(fmDescriptorFile.exists() && fmDescriptorFile.canRead()) {
+                // Need to create a new Artifact Handler for the different extension and an Artifact to not
+                // change the module artifact
+                DefaultArtifactHandler fmArtifactHandler = new DefaultArtifactHandler("slingosgifeature");
+                DefaultArtifact fmArtifact = new DefaultArtifact(
+                    artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
+                    artifact.getScope(), "slingosgifeature", artifact.getArtifactId(), fmArtifactHandler
+                );
+                fmArtifact.setFile(fmDescriptorFile);
+                artifacts.add(fmArtifact);
+                try {
+                    installArtifact(mavenSession.getProjectBuildingRequest(), artifacts);
+                } catch (MojoFailureException | MojoExecutionException e) {
+                    getLog().error("Failed to install FM Descriptor", e);
+                }
             } else {
-                getLog().error("xCould not find Converted Package: " + convertedPackageFile);
+                getLog().error("Could not find FM Descriptor File: " + fmDescriptorFile);
             }
         }
     }
diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm
index c6118c3..0c63fc9 100644
--- a/src/site/markdown/usage.md.vm
+++ b/src/site/markdown/usage.md.vm
@@ -10,7 +10,7 @@
 
 $h2 Usage
 
-$h3 Converting Sling, Project Content Packages and Lauch it
+$h3 Converting Sling, Project Content Packages and Launch it
 
 **Note**: In this sample we use Maven variables like **project.groupId** instead of a static value assuming
 that the project is within the same project that creates the content packages. Adjust that as needed.
@@ -291,3 +291,101 @@ mvn \
     install \
     -P launch
 ```
+
+$h3 Converting a Content Package and Install its Feature Model Descriptor
+
+Instead of building everything local in a dedicated Maven module now each Content Project Module
+can install their generated Feature Module descriptor into the local Maven repository so that it
+can be **included as artifact** when the FM project is assembled.
+
+First in each Content Package module add this plugin configuration to your POM in the **build** section:
+
+```
+<plugin>
+    <groupId>org.apache.sling</groupId>
+    <artifactId>sling-feature-converter-maven-plugin</artifactId>
+    <version>${sp}sling-feature-converter-maven-plugin.version}</version>
+    <extensions>true</extensions>
+    <executions>
+        <execution>
+            <id>convert-cp</id>
+            <phase>verify</phase>
+            <goals>
+                <goal>convert-cp</goal>
+            </goals>
+            <configuration>
+                <artifactIdOverride>${sp}project.groupId}:${sp}project.artifactId}:slingosgifeature:${sp3}filename}}}:${sp}project.version}</artifactIdOverride>
+                <installConvertedCP>true</installConvertedCP>
+                <convertedCPOutput>${sp}project.build.directory}/fm.out</convertedCPOutput>
+                <fmOutput>${sp}project.build.directory}/fm</fmOutput>
+                <fmPrefix>peregrine-</fmPrefix>
+                <isContentPackage>true</isContentPackage>
+                <contentPackages>
+                    <contentPackage>
+                        <groupId>${sp}project.groupId}</groupId>
+                        <artifactId>${sp}project.artifactId}</artifactId>
+                    </contentPackage>
+                </contentPackages>
+            </configuration>
+        </execution>
+    </executions>
+</plugin>
+```
+
+* **sling-feature-converter-maven-plugin.version**: is a property with the version of this plugin to be used (1.0.0-SNAPSHOT for now)
+* **artifactIdOverride**: This should match the Maven Id with this pattern: 'groupId':'artifactId':'type fixed to slingosgifeature':'classifier':'version'
+* **installConvertedCP**: must be set to true otherwise I cannot be assembled later
+* **convertedCPOutput/fmOutput/fmPrefix**: adjust to your preferences
+* **isContentPackage**: must be set to true
+* **contentPackage**: must match the module's properties (later we might remove this)
+
+After a successful build one can check the installation by going to your local Maven repository, got to the location of you artifact and
+check if there is a file with the extension **slingosgifeature**.
+
+Now we can go to the Assembly part and add the FM installed locally as artifact:
+
+```
+<plugin>
+    <groupId>org.apache.sling</groupId>
+    <artifactId>slingfeature-maven-plugin</artifactId>
+    <version>${slingfeature-maven-plugin.version}</version>
+    <extensions>true</extensions>
+    <configuration>
+        <features>target/fm</features>
+        <generatedFeatures>${basedir}/target/fm</generatedFeatures>
+    </configuration>
+    <executions>
+        <execution>
+            <id>aggregate-base-feature</id>
+            <phase>generate-test-sources</phase>
+            <goals>
+                <goal>aggregate-features</goal>
+            </goals>
+            <configuration>
+                <aggregates>
+                    <aggregate>
+                        <classifier>example-runtime</classifier>
+                        <filesInclude>**/*.json</filesInclude>
+                        <configurationOverrides>*=USE_LATEST</configurationOverrides>
+                        <includeArtifact>
+                            <groupId>${project.groupId}</groupId>
+                            <artifactId>base.ui.apps</artifactId>
+                            <version>${project.version}</version>
+                            <classifier>base.ui.apps</classifier>
+                            <type>slingosgifeature</type>
+                        </includeArtifact>
+                        ...
+                    </aggregate>
+                </aggregates>
+            </configuration>
+        </execution>
+    </executions>
+</plugin>
+```
+
+* **slingfeature-maven-plugin.version**: version of the sling feature maven plugin to be used
+* **features/generatedFeatures**: add/set depending on your project
+* **classifier/filesInclude/configurationOverrides**: set depending on your project
+* **includeArtifact**: one entry per Content Package FM. The **groupId**, **artifactId**, **version** and **classifier**
+    must match your FM descriptor
+