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:28:12 UTC

[sling-slingstart-maven-plugin] 03/09: SLING-6278 : Provide tooling to create an archive with the provisioning model and all artifacts

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

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

commit 5f8e80b2ce762d371cc83e0d2e9549387234ec1f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sat Nov 12 15:11:04 2016 +0000

    SLING-6278 : Provide tooling to create an archive with the provisioning model and all artifacts
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/maven/slingstart-maven-plugin@1769385 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/maven/slingstart/AttachModelArchive.java | 127 +++++++++++++++++++++
 .../maven/slingstart/AttachSlingStartModel.java    |   9 +-
 .../sling/maven/slingstart/BuildConstants.java     |   6 +
 src/main/resources/META-INF/plexus/components.xml  |  12 ++
 4 files changed, 147 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/maven/slingstart/AttachModelArchive.java b/src/main/java/org/apache/sling/maven/slingstart/AttachModelArchive.java
new file mode 100644
index 0000000..a2d3025
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/slingstart/AttachModelArchive.java
@@ -0,0 +1,127 @@
+/*
+ * 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 java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+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.ResolutionScope;
+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.io.ModelArchiveWriter;
+
+/**
+ * Attaches the model archive as a project artifact.
+ */
+@Mojo(
+        name = "attach-modelarchive",
+        defaultPhase = LifecyclePhase.PACKAGE,
+        requiresDependencyResolution = ResolutionScope.TEST,
+        threadSafe = true
+    )
+public class AttachModelArchive extends AbstractSlingStartMojo {
+
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * Used to look up Artifacts in the remote repository.
+     *
+     */
+    @Component
+    private ArtifactResolver resolver;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        Model model = ProjectHelper.getRawModel(this.project);
+        if (usePomVariables) {
+            model = ModelUtility.applyVariables(model, new PomVariableResolver(project));
+        }
+        if (usePomDependencies) {
+            model = ModelUtility.applyArtifactVersions(model, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies));
+        }
+
+        // write the model archive
+        final File outputFile = new File(this.project.getBuild().getDirectory() + File.separatorChar + BuildConstants.MODEL_ARCHIVE_NAME);
+        outputFile.getParentFile().mkdirs();
+
+        try ( final FileOutputStream fos = new FileOutputStream(outputFile)) {
+            // TODO provide base manifest
+            final JarOutputStream jos = ModelArchiveWriter.write(fos, model, null, new ModelArchiveWriter.ArtifactProvider() {
+
+                @Override
+                public InputStream getInputStream(final Artifact artifact) throws IOException {
+                    try {
+                        final org.apache.maven.artifact.Artifact a = ModelUtils.getArtifact(project, mavenSession,
+                                artifactHandlerManager, resolver,
+                                artifact.getGroupId(),
+                                artifact.getArtifactId(),
+                                artifact.getVersion(),
+                                artifact.getType(),
+                                artifact.getClassifier());
+                        return new FileInputStream(a.getFile());
+                    } catch (MojoExecutionException e) {
+                        throw (IOException)new IOException("Unable to get artifact: " + artifact.toMvnUrl()).initCause(e);
+                    }
+                }
+            });
+
+            // handle license etc.
+            final File classesDir = new File(this.project.getBuild().getOutputDirectory());
+            if ( classesDir.exists() ) {
+                final File metaInfDir = new File(classesDir, "META-INF");
+                for(final String name : new String[] {"LICENSE", "NOTICE", "DEPENDENCIES"}) {
+                    final File f = new File(metaInfDir, name);
+                    if ( f.exists() ) {
+                        final JarEntry artifactEntry = new JarEntry("META-INF/" + name);
+                        jos.putNextEntry(artifactEntry);
+
+                        final byte[] buffer = new byte[8192];
+                        try (final InputStream is = new FileInputStream(f)) {
+                            int l = 0;
+                            while ( (l = is.read(buffer)) > 0 ) {
+                                jos.write(buffer, 0, l);
+                            }
+                        }
+                        jos.closeEntry();
+
+                    }
+                }
+            }
+            jos.finish();
+        } catch (final IOException e) {
+            throw new MojoExecutionException("Unable to write model archive to " + outputFile + " : " + e.getMessage(), e);
+        }
+
+        // attach it as an additional artifact
+        projectHelper.attachArtifact(project, ModelArchiveWriter.DEFAULT_EXTENSION,
+                    BuildConstants.CLASSIFIER_MAR, outputFile);
+    }
+}
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 f4131be..4db991a 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java
@@ -21,7 +21,6 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -56,14 +55,10 @@ public class AttachSlingStartModel extends AbstractSlingStartMojo {
         final File outputFile = new File(this.project.getBuild().getDirectory() + File.separatorChar + BuildConstants.MODEL_ARTIFACT_NAME);
         outputFile.getParentFile().mkdirs();
 
-        Writer writer = null;
-        try {
-            writer = new FileWriter(outputFile);
+        try ( final Writer writer = new FileWriter(outputFile)) {
             ModelWriter.write(writer, model);
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new MojoExecutionException("Unable to write model to " + outputFile, e);
-        } finally {
-            IOUtils.closeQuietly(writer);
         }
 
         // if this project is a partial bundle list, it's the main artifact
diff --git a/src/main/java/org/apache/sling/maven/slingstart/BuildConstants.java b/src/main/java/org/apache/sling/maven/slingstart/BuildConstants.java
index 07885fd..62d00bd 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/BuildConstants.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/BuildConstants.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.sling.provisioning.model.ModelConstants;
+import org.apache.sling.provisioning.model.io.ModelArchiveWriter;
 
 public abstract class BuildConstants {
 
@@ -31,6 +32,9 @@ public abstract class BuildConstants {
     // Model artifact name
     public static final String MODEL_ARTIFACT_NAME = "slingstart.txt";
 
+    // Model archive name
+    public static final String MODEL_ARCHIVE_NAME = "slingstart." + ModelArchiveWriter.DEFAULT_EXTENSION;
+
     // Types
 
     public static final String TYPE_JAR = "jar";
@@ -55,6 +59,8 @@ public abstract class BuildConstants {
 
     public static final String CLASSIFIER_WEBAPP = "webapp";
 
+    public static final String CLASSIFIER_MAR = ModelArchiveWriter.DEFAULT_EXTENSION;
+
     // Manifest attributes
 
     public static final String ATTR_BUILT_BY = "Built-By";
diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml
index 21f4ced..6d60143 100644
--- a/src/main/resources/META-INF/plexus/components.xml
+++ b/src/main/resources/META-INF/plexus/components.xml
@@ -87,5 +87,17 @@
         <addedToClasspath>false</addedToClasspath>
       </configuration>
     </component>
+    <component>
+      <role>org.apache.maven.artifact.handler.ArtifactHandler</role>
+      <role-hint>mar</role-hint>
+      <implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
+      <configuration>
+        <type>mar</type>
+        <includesDependencies>false</includesDependencies>
+        <language>java</language>
+        <extension>mar</extension>
+        <addedToClasspath>false</addedToClasspath>
+      </configuration>
+    </component>
   </components>
 </component-set>

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