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 2018/04/30 09:36:41 UTC

[sling-slingstart-maven-plugin] 01/07: Convert the feature model into the provisioning model

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

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

commit 211c82088fdaadb533b2cc2c7c98c7a6eb5d2f28
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Tue Mar 6 15:50:53 2018 +0000

    Convert the feature model into the provisioning model
---
 pom.xml                                            |  20 ++++
 .../slingstart/DependencyLifecycleParticipant.java |  15 +--
 .../maven/slingstart/FeatureModelConverter.java    |  86 ++++++++++++++++++
 .../maven/slingstart/GenerateResourcesMojo.java    | 101 +++++++++++++++++++++
 .../sling/maven/slingstart/ModelPreprocessor.java  |  38 +++++---
 src/main/resources/META-INF/plexus/components.xml  |   1 +
 .../maven/slingstart/ReadFeatureModelTest.java     |  40 ++++++++
 src/test/resources/johnzon-feature.json            |   4 +
 src/test/resources/launchpad-feature.json          |   4 +
 9 files changed, 287 insertions(+), 22 deletions(-)

diff --git a/pom.xml b/pom.xml
index c47ead9..3ae0a73 100644
--- a/pom.xml
+++ b/pom.xml
@@ -160,6 +160,26 @@
             <version>1.8.4</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.analyser</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.modelconverter</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.resolver</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.support</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.maven</groupId>
             <artifactId>maven-core</artifactId>
             <version>${maven.version}</version>
diff --git a/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java b/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
index 3a11d9a..96ba69a 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
@@ -16,10 +16,6 @@
  */
 package org.apache.sling.maven.slingstart;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
 import org.apache.maven.AbstractMavenLifecycleParticipant;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
@@ -33,6 +29,10 @@ import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.logging.Logger;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
 /**
  * Maven lifecycle participant which adds the artifacts of the model to the dependencies.
  * This cannot happen as part of a regular Mojo (as there the dependencies have already been calculated)
@@ -46,7 +46,7 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
 
     private static final String GROUP_ID = "org.apache.sling";
     private static final String ARTIFACT_ID = "slingstart-maven-plugin";
-    
+
     /**
      *  the plugin ID consists of <code>groupId:artifactId</code>, see {@link Plugin#constructKey(String, String)}
      */
@@ -97,12 +97,13 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
             }
         }
 
+        new FeatureModelConverter().convert(session, env);
         new ModelPreprocessor().addDependencies(env);
     }
-    
+
     /**
      * Retrieves the version of the encapsulating Mojo by evaluating the {@code pom.properties} loaded via the extension classloader
-     * @throws IOException 
+     * @throws IOException
      * @see <a href="https://maven.apache.org/shared/maven-archiver/#pom-properties-content">Maven Archiver - pom.properties</a>
      */
     static final String getCurrentPluginVersion() throws IOException {
diff --git a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
new file mode 100644
index 0000000..2756036
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
@@ -0,0 +1,86 @@
+/*
+ * 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.maven.slingstart;
+
+import org.apache.maven.MavenExecutionException;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.apache.sling.feature.modelconverter.impl.FeatureToProvisioning;
+import org.apache.sling.feature.support.ArtifactManager;
+import org.apache.sling.feature.support.ArtifactManagerConfig;
+import org.apache.sling.maven.slingstart.ModelPreprocessor.Environment;
+import org.apache.sling.maven.slingstart.ModelPreprocessor.ProjectInfo;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class FeatureModelConverter {
+    static final String BUILD_DIR = "provisioning/converted";
+
+    public void convert(MavenSession session, Environment env) throws MavenExecutionException {
+        Map<String, ProjectInfo> projs = env.modelProjects;
+        for (ProjectInfo pi : projs.values()) {
+            convert(session, pi.project);
+        }
+    }
+
+    private void convert(MavenSession session, MavenProject project) throws MavenExecutionException {
+        File featuresDir = new File(project.getBasedir(), "src/main/features");
+
+        File[] files = featuresDir.listFiles();
+        List<File> featureFiles;
+        if (files != null) {
+            featureFiles = Arrays.asList(files);
+        } else {
+            featureFiles = Collections.emptyList();
+        }
+
+        if (featureFiles.size() == 0)
+            return;
+
+        File targetFile = new File(project.getBuild().getDirectory(), BUILD_DIR + "/model.txt");
+        targetFile.getParentFile().mkdirs();
+
+        try {
+            ArtifactManager am = getArtifactManager(project, session);
+            System.out.println("$$$ Converting: " + featureFiles);
+            FeatureToProvisioning.convert(featureFiles, targetFile.getAbsolutePath(), false, am);
+            System.out.println("~~~ Converted at: " + targetFile);
+        } catch (Exception e) {
+            throw new MavenExecutionException("Cannot convert feature files to provisioning model", e);
+        }
+    }
+
+    private ArtifactManager getArtifactManager(MavenProject project, MavenSession session)
+            throws IOException {
+        List<String> repos = new ArrayList<>();
+        repos.add(session.getLocalRepository().getUrl());
+        for (ArtifactRepository ar : project.getRemoteArtifactRepositories()) {
+            repos.add(ar.getUrl());
+        }
+
+        final ArtifactManagerConfig amConfig = new ArtifactManagerConfig();
+        amConfig.setRepositoryUrls(repos.toArray(new String[] {}));
+        return ArtifactManager.getArtifactManager(amConfig);
+    }
+}
diff --git a/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java b/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
new file mode 100644
index 0000000..e92d384
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
@@ -0,0 +1,101 @@
+/*
+ * 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.maven.slingstart;
+
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+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.modelconverter.impl.FeatureToProvisioning;
+import org.apache.sling.feature.support.ArtifactManager;
+import org.apache.sling.feature.support.ArtifactManagerConfig;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@Mojo(
+        name = "generate-resources",
+        defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
+        requiresDependencyResolution = ResolutionScope.TEST, // maybe something else TODO?
+        threadSafe = true)
+public class GenerateResourcesMojo extends AbstractSlingStartMojo {
+    @Parameter(defaultValue="${basedir}/src/main/features")
+    private File featuresDirectory;
+
+    /**
+     * To look up Archiver/UnArchiver implementations
+     */
+    @Component
+    private ArchiverManager archiverManager;
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Used to look up Artifacts in the remote repository.
+     *
+     */
+    @Component
+    private ArtifactResolver resolver;
+
+    @Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
+    protected MojoExecution mojoExecution;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        System.out.println("*** Checking: " + featuresDirectory);
+        List<File> files = Arrays.asList(featuresDirectory.listFiles());
+        for (File f : files) {
+            System.out.println("### Found: " + f);
+        }
+
+        String targetDir = project.getBuild().getDirectory();
+        File provModelDir = new File(targetDir + "/" + FeatureModelConverter.BUILD_DIR);
+        provModelDir.mkdirs();
+
+        try {
+            ArtifactManager am = getArtifactManager();
+            FeatureToProvisioning.convert(files, provModelDir.getAbsolutePath() + "/model.txt", false, am);
+            System.out.println("~~~ Converted files at: " + provModelDir);
+        } catch (Exception e) {
+            throw new MojoExecutionException("Cannot convert feature files to provisioning model", e);
+        }
+    }
+
+    private ArtifactManager getArtifactManager() throws IOException {
+        List<String> repos = new ArrayList<>();
+        repos.add(mavenSession.getLocalRepository().getUrl());
+        for (ArtifactRepository ar : project.getRemoteArtifactRepositories()) {
+            repos.add(ar.getUrl());
+        }
+
+        final ArtifactManagerConfig amConfig = new ArtifactManagerConfig();
+        amConfig.setRepositoryUrls(repos.toArray(new String[] {}));
+        return ArtifactManager.getArtifactManager(amConfig);
+    }
+}
diff --git a/src/main/java/org/apache/sling/maven/slingstart/ModelPreprocessor.java b/src/main/java/org/apache/sling/maven/slingstart/ModelPreprocessor.java
index 7e0be34..da40e6f 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/ModelPreprocessor.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/ModelPreprocessor.java
@@ -16,20 +16,6 @@
  */
 package org.apache.sling.maven.slingstart;
 
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 import org.apache.commons.io.IOUtils;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.Artifact;
@@ -57,6 +43,20 @@ import org.apache.sling.provisioning.model.io.ModelReader;
 import org.codehaus.plexus.logging.Logger;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 public class ModelPreprocessor {
 
     public static final class ProjectInfo {
@@ -125,7 +125,15 @@ public class ModelPreprocessor {
                 if (defaultModelDirectory.exists()) {
                     env.logger.debug("Try to extract model from default provisioning directory " + defaultModelDirectory.getAbsolutePath());
                     info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectory, pattern, env.logger);
-                } else {
+                }
+
+                File defaultConvertedModelDirectory = new File(info.project.getBuild().getDirectory() + "/" + FeatureModelConverter.BUILD_DIR);
+                if (defaultConvertedModelDirectory.exists()) {
+                    env.logger.debug("Try to extract model from generated provisioning model directory " + defaultConvertedModelDirectory.getAbsolutePath());
+                    info.localModel = readLocalModel(info.project, inlinedModel, defaultConvertedModelDirectory, pattern, env.logger);
+                }
+
+                if (info.localModel == null) {
                     File defaultModelDirectoryInTest = new File(info.project.getBasedir(), "src/test/provisioning");
                     env.logger.debug("Try to extract model from default test provisioning directory " + defaultModelDirectoryInTest.getAbsolutePath());
                     info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectoryInTest, pattern, env.logger);
diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml
index 6d60143..d694e2d 100644
--- a/src/main/resources/META-INF/plexus/components.xml
+++ b/src/main/resources/META-INF/plexus/components.xml
@@ -27,6 +27,7 @@
           <lifecycle>
             <id>default</id>
             <phases>
+              <generate-resources>org.apache.sling:slingstart-maven-plugin:generate-resources</generate-resources>
               <package>org.apache.sling:slingstart-maven-plugin:attach-slingfeature</package>
               <install>org.apache.maven.plugins:maven-install-plugin:install</install>
               <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
diff --git a/src/test/java/org/apache/sling/maven/slingstart/ReadFeatureModelTest.java b/src/test/java/org/apache/sling/maven/slingstart/ReadFeatureModelTest.java
new file mode 100644
index 0000000..684951b
--- /dev/null
+++ b/src/test/java/org/apache/sling/maven/slingstart/ReadFeatureModelTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.maven.slingstart;
+
+public class ReadFeatureModelTest {
+    /*
+    @Test
+    public void testReadSimpleFeatureModel() {
+        Feature lf = readFeature("/launchpad-feature.json", am);
+        Feature jf = readFeature("/johnzon-feature.json", am);
+
+    }
+
+    private Feature readFeature(final String res,
+            final ArtifactManager artifactManager) throws Exception {
+        URL url = getClass().getResource(res);
+        String file = new File(url.toURI()).getAbsolutePath();
+        final ArtifactHandler featureArtifact = artifactManager.getArtifactHandler(file);
+
+        try (final FileReader r = new FileReader(featureArtifact.getFile())) {
+            final Feature f = FeatureJSONReader.read(r, featureArtifact.getUrl());
+            return f;
+        }
+    }
+    */
+}
diff --git a/src/test/resources/johnzon-feature.json b/src/test/resources/johnzon-feature.json
new file mode 100644
index 0000000..f70f8f4
--- /dev/null
+++ b/src/test/resources/johnzon-feature.json
@@ -0,0 +1,4 @@
+{
+    "id": "org.apache.sling.test.features/johnzon-feature/1.0.0",
+    "bundles": ["org.apache.sling/org.apache.sling.commons.johnzon/1.0.0"] 
+}
\ No newline at end of file
diff --git a/src/test/resources/launchpad-feature.json b/src/test/resources/launchpad-feature.json
new file mode 100644
index 0000000..26a43c7
--- /dev/null
+++ b/src/test/resources/launchpad-feature.json
@@ -0,0 +1,4 @@
+{
+    "id": "org.apache.sling.test.features/launchpad-feature/1.0.0",
+    "bundles": ["org.apache.sling/org.apache.sling.commons.classloader/1.3.2"] 
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.