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

pauls pushed a commit to branch issues/SLING-7968
in repository 
https://gitbox.apache.org/repos/asf/sling-slingstart-maven-plugin.git

commit f8f9b36d1c50b8f5f894b96b1ca5f622f2cb241c
Author: Karl Pauls <[email protected]>
AuthorDate: Tue Oct 2 00:59:45 2018 +0200

    SLING-7968: Use FeatureProvider instead of ArtifactManager.
---
 pom.xml                                            | 13 +++---
 .../maven/slingstart/FeatureModelConverter.java    | 52 +++++++++++-----------
 .../maven/slingstart/GenerateResourcesMojo.java    | 25 ++---------
 3 files changed, 36 insertions(+), 54 deletions(-)

diff --git a/pom.xml b/pom.xml
index 5888278..71888e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -70,7 +70,7 @@
             <plugin>
                 <groupId>org.codehaus.plexus</groupId>
                 <artifactId>plexus-component-metadata</artifactId>
-                <version>1.5.5</version>
+                <version>1.7.1</version>
                 <executions>
                     <execution>
                         <id>generate-metadata</id>
@@ -154,6 +154,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature</artifactId>
+            <version>0.1.3-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.analyser</artifactId>
             <version>0.1.3-SNAPSHOT</version>
         </dependency>
@@ -298,12 +303,6 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.feature</artifactId>
-            <version>0.1.3-SNAPSHOT</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <scope>test</scope>
diff --git 
a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java 
b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
index 69e1afb..e6492ea 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
@@ -17,18 +17,24 @@
 package org.apache.sling.maven.slingstart;
 
 import org.apache.maven.MavenExecutionException;
-import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.project.MavenProject;
-import org.apache.sling.feature.io.file.ArtifactManager;
-import org.apache.sling.feature.io.file.ArtifactManagerConfig;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.builder.FeatureProvider;
+import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.modelconverter.FeatureToProvisioning;
 import org.apache.sling.maven.slingstart.ModelPreprocessor.Environment;
 import org.apache.sling.maven.slingstart.ModelPreprocessor.ProjectInfo;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
@@ -38,31 +44,40 @@ import java.util.Properties;
 public class FeatureModelConverter {
     static final String BUILD_DIR = "provisioning/converted";
 
+    public static Feature getFeature(ArtifactId id, MavenSession session, 
MavenProject project, ArtifactHandlerManager manager, ArtifactResolver 
resolver) {
+        try {
+            File file = ModelUtils.getArtifact(project, session, manager, 
resolver, id.getGroupId(), id.getArtifactId(), id.getVersion(), id.getType(), 
id.getClassifier()).getFile();
+            try (Reader reader = new InputStreamReader(new 
FileInputStream(file), "UTF-8")) {
+                return FeatureJSONReader.read(reader, 
file.toURI().toURL().toString());
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
     public static void convert(MavenSession session, Environment env) throws 
MavenExecutionException {
         Map<String, ProjectInfo> projs = env.modelProjects;
         for (ProjectInfo pi : projs.values()) {
-            convert(session, pi.project);
+            convert(session, pi.project, env.artifactHandlerManager, 
env.resolver);
         }
     }
 
-    private static void convert(MavenSession session, MavenProject project) 
throws MavenExecutionException {
+    private static void convert(MavenSession session, MavenProject project, 
ArtifactHandlerManager manager, ArtifactResolver resolver) throws 
MavenExecutionException {
         File featuresDir = new File(project.getBasedir(), "src/main/features");
 
         File[] files = featuresDir.listFiles();
         if (files == null || files.length == 0)
             return;
 
-        ArtifactManager am;
         try {
-            am = getArtifactManager(project, session);
-        } catch (IOException ex) {
-            throw new MavenExecutionException("Unable to obtain 
artifactManager", ex);
+            convert(files, project, id -> getFeature(id, session, project, 
manager, resolver));
+        } catch (RuntimeException ex) {
+            throw new MavenExecutionException(ex.getMessage(), ex);
         }
 
-        convert(files, project, am);
     }
 
-    static void convert(File[] files, MavenProject project, ArtifactManager 
am) throws MavenExecutionException {
+    static void convert(File[] files, MavenProject project, FeatureProvider 
fp) throws MavenExecutionException {
         File processedFeaturesDir = new 
File(project.getBuild().getDirectory(), "features/processed");
         processedFeaturesDir.mkdirs();
 
@@ -88,7 +103,7 @@ public class FeatureModelConverter {
                     continue;
                 }
                 File genFile = new File(targetDir, f.getName() + ".txt");
-                FeatureToProvisioning.convert(f, genFile, am, 
substedFiles.toArray(new File[] {}));
+                FeatureToProvisioning.convert(f, genFile, fp, 
substedFiles.toArray(new File[] {}));
             }
         } catch (Exception e) {
             throw new MavenExecutionException("Cannot convert feature files to 
provisioning model", e);
@@ -132,17 +147,4 @@ public class FeatureModelConverter {
     private static String replaceAll(String s, String key, String value) {
         return s.replaceAll("\\Q${" + key + "}\\E", value);
     }
-
-    private static 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
index bd3a16f..296977d 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
@@ -18,24 +18,17 @@ package org.apache.sling.maven.slingstart;
 
 import org.apache.maven.MavenExecutionException;
 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.io.file.ArtifactManager;
-import org.apache.sling.feature.io.file.ArtifactManagerConfig;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 
 @Mojo(
         name = "generate-resources",
@@ -66,29 +59,17 @@ public class GenerateResourcesMojo extends 
AbstractSlingStartMojo {
     protected MojoExecution mojoExecution;
 
     @Override
-    public void execute() throws MojoExecutionException, MojoFailureException {
+    public void execute() throws MojoExecutionException {
         File[] featureFiles = featuresDirectory.listFiles();
         if (featureFiles == null)
             return;
 
         try {
-            FeatureModelConverter.convert(featureFiles, project, 
getArtifactManager());
+            FeatureModelConverter.convert(featureFiles, project, id -> 
FeatureModelConverter.getFeature(id, mavenSession, project, 
artifactHandlerManager, resolver));
         } catch (MavenExecutionException e) {
             throw new MojoExecutionException("Cannot convert feature files to 
provisioning model.", e);
-        } catch (IOException e) {
+        } catch (RuntimeException e) {
             throw new MojoExecutionException("Problem obtaining artifact 
manager.", 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);
-    }
 }

Reply via email to