You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:27:03 UTC

[sling-slingstart-maven-plugin] 05/07: SLING-4945 : Model files are missing from created repository SLING-4946 : Model dependencies are not followed transitively

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

rombert pushed a commit to annotated tag slingstart-maven-plugin-1.3.2
in repository https://gitbox.apache.org/repos/asf/sling-slingstart-maven-plugin.git

commit 0acafe1069831affebe5265bceb336ea61c1d221
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 14 08:22:40 2015 +0000

    SLING-4945 : Model files are missing from created repository
    SLING-4946 : Model dependencies are not followed transitively
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/maven/slingstart-maven-plugin@1695835 13f79535-47bb-0310-9956-ffa450edef68
---
 .../maven/slingstart/AttachSlingStartModel.java    |  2 -
 .../slingstart/DependencyLifecycleParticipant.java | 46 +++++++++++--
 .../sling/maven/slingstart/ProjectHelper.java      | 29 +++++++-
 .../sling/maven/slingstart/RepositoryMojo.java     | 79 ++++++++++++++++++++--
 4 files changed, 140 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java b/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java
index 331adf8..f4131be 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java
@@ -58,8 +58,6 @@ public class AttachSlingStartModel extends AbstractSlingStartMojo {
 
         Writer writer = null;
         try {
-
-
             writer = new FileWriter(outputFile);
             ModelWriter.write(writer, model);
         } catch (IOException e) {
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 15b71c7..31e2d93 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java
@@ -77,11 +77,14 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
     private ArtifactResolver resolver;
 
     public static final class ProjectInfo {
+
         public MavenProject project;
         public Plugin       plugin;
         public Model        localModel;
         public boolean      done = false;
         public Model        model;
+        public final Map<org.apache.sling.provisioning.model.Artifact, Model> includedModels = new HashMap<org.apache.sling.provisioning.model.Artifact, Model>();
+
     }
 
     public static final class Environment {
@@ -281,27 +284,35 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
 
                             // if it's a project from the current reactor build, we can't resolve it right now
                             final String key = a.getGroupId() + ":" + a.getArtifactId();
-                            if ( env.modelProjects.containsKey(key) ) {
+                            final ProjectInfo depInfo = env.modelProjects.get(key);
+                            if ( depInfo != null ) {
                                 env.logger.debug("Found reactor " + a.getType() + " dependency : " + a);
-                                final Model model = addDependencies(env, env.modelProjects.get(key));
+                                final Model model = addDependencies(env, depInfo);
                                 if ( model == null ) {
                                     throw new MavenExecutionException("Recursive model dependency list including project " + info.project, (File)null);
                                 }
                                 dependencies.add(model);
+                                info.includedModels.put(a, depInfo.localModel);
+
                             } else {
                                 env.logger.debug("Found external " + a.getType() + " dependency: " + a);
+
                                 // "external" dependency, we can already resolve it
                                 final File modelFile = resolveSlingstartArtifact(env, info.project, dep);
                                 FileReader r = null;
                                 try {
                                     r = new FileReader(modelFile);
-                                    final Model m = ModelReader.read(r, modelFile.getAbsolutePath());
+                                    final Model model = ModelReader.read(r, modelFile.getAbsolutePath());
+
+                                    info.includedModels.put(a, model);
 
-                                    final Map<Traceable, String> errors = ModelUtility.validate(m);
+                                    final Map<Traceable, String> errors = ModelUtility.validate(model);
                                     if ( errors != null ) {
                                         throw new MavenExecutionException("Unable to read model file from " + modelFile + " : " + errors, modelFile);
                                     }
-                                    dependencies.add(m);
+                                    final Model fullModel = processSlingstartDependencies(env, info, dep,  model);
+
+                                    dependencies.add(fullModel);
                                 } catch ( final IOException ioe) {
                                     throw new MavenExecutionException("Unable to read model file from " + modelFile, ioe);
                                 } finally {
@@ -314,8 +325,6 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
                                     }
                                 }
                             }
-                            env.logger.debug("- adding dependency " + ModelUtils.toString(dep));
-                            info.project.getDependencies().add(dep);
 
                             removeList.add(a);
                         }
@@ -330,6 +339,29 @@ public class DependencyLifecycleParticipant extends AbstractMavenLifecyclePartic
         return dependencies;
     }
 
+    private static Model processSlingstartDependencies(final Environment env, final ProjectInfo info, final Dependency dep, final Model rawModel)
+    throws MavenExecutionException {
+        env.logger.debug("Processing dependency " + dep);
+
+        // we have to create an effective model to add the dependencies
+        final Model effectiveModel = ModelUtility.getEffectiveModel(rawModel, new ResolverOptions());
+
+        final List<Model> dependencies = searchSlingstartDependencies(env, info, effectiveModel);
+        Model mergingModel = new Model();
+        for(final Model d : dependencies) {
+            ModelUtility.merge(mergingModel, d);
+        }
+        ModelUtility.merge(mergingModel, effectiveModel);
+        mergingModel = ModelUtility.getEffectiveModel(mergingModel, new ResolverOptions());
+
+        final Map<Traceable, String> errors = ModelUtility.validate(mergingModel);
+        if ( errors != null ) {
+            throw new MavenExecutionException("Unable to create model file for " + dep + " : " + errors, (File)null);
+        }
+
+        return mergingModel;
+    }
+
     /**
      * Gets plugins configuration from POM (string parameter).
      * @param plugin Plugin
diff --git a/src/main/java/org/apache/sling/maven/slingstart/ProjectHelper.java b/src/main/java/org/apache/sling/maven/slingstart/ProjectHelper.java
index af6785c..35ec739 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/ProjectHelper.java
@@ -19,9 +19,12 @@ package org.apache.sling.maven.slingstart;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
+import org.apache.sling.provisioning.model.Artifact;
 import org.apache.sling.provisioning.model.Model;
 import org.apache.sling.provisioning.model.ModelUtility;
 import org.apache.sling.provisioning.model.ModelUtility.ResolverOptions;
@@ -37,8 +40,11 @@ public abstract class ProjectHelper {
     private static final String EFFECTIVE_MODEL_TXT = Model.class.getName() + "/effective.txt";
     private static final String EFFECTIVE_MODEL_CACHE = Model.class.getName() + "/effective.cache";
 
+    private static final String DEPENDENCY_MODEL = Model.class.getName() + "/dependency";
+
     /**
-     *
+     * Store all relevant information about the project for plugins to be
+     * retrieved
      * @param info The project info
      * @throws IOException If writing fails
      */
@@ -52,6 +58,15 @@ public abstract class ProjectHelper {
         final StringWriter w2 = new StringWriter();
         ModelWriter.write(w2, info.model);
         info.project.setContextValue(EFFECTIVE_MODEL_TXT, w2.toString());
+
+        // create map with model dependencies
+        final Map<String, String> map = new HashMap<String, String>();
+        for(final Map.Entry<Artifact, Model> entry : info.includedModels.entrySet()) {
+            final StringWriter w3 = new StringWriter();
+            ModelWriter.write(w3, entry.getValue());
+            map.put(entry.getKey().toMvnUrl(), w3.toString());
+        }
+        info.project.setContextValue(DEPENDENCY_MODEL, map);
     }
 
     /**
@@ -96,4 +111,16 @@ public abstract class ProjectHelper {
         }
         return result;
     }
+
+    /**
+     * Get the dependency model from the project
+     * @param project The maven projet
+     * @return The dependency  model
+     * @throws MojoExecutionException If reading fails
+     */
+    @SuppressWarnings("unchecked")
+    public static Map<String, String> getDependencyModel(final MavenProject project)
+    throws MojoExecutionException {
+        return (Map<String, String>) project.getContextValue(DEPENDENCY_MODEL);
+    }
 }
diff --git a/src/main/java/org/apache/sling/maven/slingstart/RepositoryMojo.java b/src/main/java/org/apache/sling/maven/slingstart/RepositoryMojo.java
index fa7301a..6f4f1f5 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/RepositoryMojo.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/RepositoryMojo.java
@@ -17,9 +17,13 @@
 package org.apache.sling.maven.slingstart;
 
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.io.Writer;
+import java.util.Map;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
@@ -32,7 +36,9 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.sling.provisioning.model.ArtifactGroup;
 import org.apache.sling.provisioning.model.Feature;
 import org.apache.sling.provisioning.model.Model;
+import org.apache.sling.provisioning.model.ModelUtility;
 import org.apache.sling.provisioning.model.RunMode;
+import org.apache.sling.provisioning.model.io.ModelWriter;
 
 /**
  * Create a mvn repository structure from the artifacts
@@ -61,6 +67,7 @@ public class RepositoryMojo extends AbstractSlingStartMojo {
         this.getLog().info("Creating repository...");
         final File artifactDir = new File(this.project.getBuild().getDirectory(), DIR_NAME);
 
+        // artifacts
         final Model model = ProjectHelper.getEffectiveModel(this.project, getResolverOptions());
 
         for(final Feature feature : model.getFeatures()) {
@@ -72,6 +79,7 @@ public class RepositoryMojo extends AbstractSlingStartMojo {
                 }
             }
         }
+        // base artifact
         try {
             final org.apache.sling.provisioning.model.Artifact baseArtifact = ModelUtils.findBaseArtifact(model);
             final org.apache.sling.provisioning.model.Artifact appArtifact =
@@ -84,17 +92,64 @@ public class RepositoryMojo extends AbstractSlingStartMojo {
         } catch ( final MavenExecutionException mee) {
             throw new MojoExecutionException(mee.getMessage(), mee.getCause());
         }
+        // models
+        Model rawModel = ProjectHelper.getRawModel(this.project);
+        if (usePomVariables) {
+            rawModel = ModelUtility.applyVariables(rawModel, new PomVariableResolver(project));
+        }
+        if (usePomDependencies) {
+            rawModel = ModelUtility.applyArtifactVersions(rawModel, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies));
+        }
+
+        final String classifier = (project.getPackaging().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM) ? null : BuildConstants.PACKAGING_PARTIAL_SYSTEM);
+        final org.apache.sling.provisioning.model.Artifact rawModelArtifact =
+                new org.apache.sling.provisioning.model.Artifact(
+                        this.project.getGroupId(),
+                        this.project.getArtifactId(),
+                        this.project.getVersion(),
+                        classifier,
+                        BuildConstants.TYPE_TXT);
+        final File rawModelFile = getRepositoryFile(artifactDir, rawModelArtifact);
+
+        Writer writer = null;
+        try {
+            writer = new FileWriter(rawModelFile);
+            ModelWriter.write(writer, rawModel);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Unable to write model to " + rawModelFile, e);
+        } finally {
+            IOUtils.closeQuietly(writer);
+        }
 
+        for(final Map.Entry<String, String> entry : ProjectHelper.getDependencyModel(this.project).entrySet()) {
+            final org.apache.sling.provisioning.model.Artifact modelDepArtifact = org.apache.sling.provisioning.model.Artifact.fromMvnUrl(entry.getKey());
+            final String modelClassifier = (modelDepArtifact.getType().equals(BuildConstants.PACKAGING_SLINGSTART) ? BuildConstants.PACKAGING_PARTIAL_SYSTEM : modelDepArtifact.getClassifier());
+            final org.apache.sling.provisioning.model.Artifact modelArtifact = new org.apache.sling.provisioning.model.Artifact(
+                    modelDepArtifact.getGroupId(),
+                    modelDepArtifact.getArtifactId(),
+                    modelDepArtifact.getVersion(),
+                    modelClassifier,
+                    BuildConstants.TYPE_TXT);
+            final File modelFile = getRepositoryFile(artifactDir, modelArtifact);
+            Writer modelWriter = null;
+            try {
+                modelWriter = new FileWriter(modelFile);
+                modelWriter.write(entry.getValue());
+            } catch (IOException e) {
+                throw new MojoExecutionException("Unable to write model to " + modelFile, e);
+            } finally {
+                IOUtils.closeQuietly(modelWriter);
+            }
+        }
     }
 
     /**
-     * Copy a single artifact to the repository
-     * @throws MojoExecutionException
+     * Get the file in the repository directory
+     * @param artifactDir The base artifact directory
+     * @param artifact The artifact
+     * @return The file
      */
-    private void copyArtifactToRepository(final org.apache.sling.provisioning.model.Artifact artifact,
-            final File artifactDir)
-    throws MojoExecutionException {
-
+    private File getRepositoryFile(final File artifactDir, final org.apache.sling.provisioning.model.Artifact artifact) {
         final StringBuilder artifactNameBuilder = new StringBuilder();
         artifactNameBuilder.append(artifact.getArtifactId());
         artifactNameBuilder.append('-');
@@ -120,6 +175,18 @@ public class RepositoryMojo extends AbstractSlingStartMojo {
         final File artifactFile = new File(artifactDir, destPath);
         artifactFile.getParentFile().mkdirs();
 
+        return artifactFile;
+    }
+
+    /**
+     * Copy a single artifact to the repository
+     * @throws MojoExecutionException
+     */
+    private void copyArtifactToRepository(final org.apache.sling.provisioning.model.Artifact artifact,
+            final File artifactDir)
+    throws MojoExecutionException {
+        final File artifactFile = getRepositoryFile(artifactDir, artifact);
+
         final Artifact source = ModelUtils.getArtifact(this.project, this.mavenSession, this.artifactHandlerManager, this.resolver,
                 artifact.getGroupId(),
                 artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier());

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.