incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1145156 - in /sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport: AbstractUsingBundleListMojo.java PreparePackageMojo.java
Date Mon, 11 Jul 2011 12:56:45 GMT
Author: cziegeler
Date: Mon Jul 11 12:56:45 2011
New Revision: 1145156

URL: http://svn.apache.org/viewvc?rev=1145156&view=rev
Log:
SLING-2134 : Provide a way to add additional properties to sling.properties

Modified:
    sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/AbstractUsingBundleListMojo.java
    sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/PreparePackageMojo.java

Modified: sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/AbstractUsingBundleListMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/AbstractUsingBundleListMojo.java?rev=1145156&r1=1145155&r2=1145156&view=diff
==============================================================================
--- sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/AbstractUsingBundleListMojo.java
(original)
+++ sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/AbstractUsingBundleListMojo.java
Mon Jul 11 12:56:45 2011
@@ -224,7 +224,7 @@ public abstract class AbstractUsingBundl
             } catch (ArtifactMetadataRetrievalException e) {
                 throw new MojoExecutionException("Unable to find version for artifact", e);
             }
-            
+
         }
 
         try {
@@ -309,7 +309,7 @@ public abstract class AbstractUsingBundl
                 bundleList.remove(def.toBundle(), false);
             }
         }
-        
+
         Set<Artifact> dependencies = project.getDependencyArtifacts();
         for (Artifact artifact : dependencies) {
             if (PARTIAL.equals(artifact.getType())) {
@@ -319,8 +319,8 @@ public abstract class AbstractUsingBundl
                 bundleList.merge(readBundleList(artifact.getFile()));
             }
         }
-        
-        
+
+
         initBundleList(bundleList);
 
         rewriteBundleList(bundleList);
@@ -371,4 +371,13 @@ public abstract class AbstractUsingBundl
         }
     }
 
+    protected File getSlingProperties() {
+        if ( this.configDirectory != null && this.configDirectory.exists() &&
this.configDirectory.isDirectory() ) {
+            final File slingProps = new File(this.configDirectory, "sling.properties");
+            if ( slingProps.exists() && slingProps.isFile() ) {
+                return slingProps;
+            }
+        }
+        return null;
+    }
 }

Modified: sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/PreparePackageMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/PreparePackageMojo.java?rev=1145156&r1=1145155&r2=1145156&view=diff
==============================================================================
--- sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/PreparePackageMojo.java
(original)
+++ sling/trunk/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/PreparePackageMojo.java
Mon Jul 11 12:56:45 2011
@@ -17,7 +17,10 @@
 package org.apache.sling.maven.projectsupport;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.Enumeration;
 import java.util.Properties;
 
 import org.apache.maven.artifact.Artifact;
@@ -26,6 +29,7 @@ import org.apache.maven.plugin.MojoFailu
 import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
 import org.codehaus.plexus.archiver.ArchiverException;
 import org.codehaus.plexus.archiver.UnArchiver;
+import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 import org.codehaus.plexus.util.FileUtils;
@@ -81,13 +85,28 @@ public class PreparePackageMojo extends 
 	private File buildOutputDirectory;
 
     /**
+     * The temp directory (i.e. target/maven-launchpad-plugintmp).
+     *
+     * @parameter expression="${project.build.directory}/maven-launchpad-plugintmp"
+     * @readonly
+     */
+    private File tempDirectory;
+
+    /**
      * To look up Archiver/UnArchiver implementations
      *
      * @component
      */
     private ArchiverManager archiverManager;
 
-	public void executeWithArtifacts() throws MojoExecutionException, MojoFailureException {
+    /**
+     * The Jar archiver.
+     *
+     * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
+     */
+    private JarArchiver jarArchiver;
+
+    public void executeWithArtifacts() throws MojoExecutionException, MojoFailureException
{
 		copyBaseArtifact();
 		copyBundles(getBundleList(), getOutputDirectory());
 		copyConfigurationFiles();
@@ -131,20 +150,91 @@ public class PreparePackageMojo extends 
 		File destinationFile = new File(destinationDir, artifact
 				.getArtifactId()
 				+ "." + artifact.getArtifactHandler().getExtension());
-		if (shouldCopy(artifact.getFile(), destinationFile)) {
-			try {
-				getLog().info(
-						String.format("Copying base artifact from %s to %s.",
-								artifact.getFile(), destinationFile));
-				FileUtils.copyFile(artifact.getFile(), destinationFile);
-			} catch (IOException e) {
-				throw new MojoExecutionException(
-						"Unable to copy base artifact.", e);
-			}
+
+		// check if custom sling.properties file exists
+		final File slingProps = this.getSlingProperties();
+		if ( slingProps != null ) {
+    		// unpack to a temp destination
+		    final File dest = new File(this.tempDirectory, "basejar");
+		    try {
+        		unpack(artifact.getFile(), dest);
+        		final File origSlingProps = new File(dest, "sling.properties");
+        		if ( !origSlingProps.exists() ) {
+        		    throw new MojoExecutionException("sling.properties not found at " + origSlingProps);
+        		}
+
+        		// read original properties
+        		final Properties orig = new Properties();
+        		FileInputStream fis = null;
+        		try {
+        		    fis = new FileInputStream(origSlingProps);
+                    orig.load(fis);
+        		} catch (final IOException ioe) {
+        		    throw new MojoExecutionException("Unable to read " + origSlingProps, ioe);
+        		} finally {
+        		    if ( fis != null ) {
+        		        try { fis.close(); } catch (final IOException ignore) {}
+        		    }
+        		}
+
+        		// read additional properties
+                final Properties addProps = new Properties();
+                try {
+                    fis = new FileInputStream(slingProps);
+                    addProps.load(fis);
+                } catch (final IOException ioe) {
+                    throw new MojoExecutionException("Unable to read " + slingProps, ioe);
+                } finally {
+                    if ( fis != null ) {
+                        try { fis.close(); } catch (final IOException ignore) {}
+                    }
+                }
+
+                // patch
+                final Enumeration<Object> keys = addProps.keys();
+                if ( keys.hasMoreElements() ) {
+                    getLog().info("Patching sling.properties");
+                }
+                while ( keys.hasMoreElements() ) {
+                    final Object key = keys.nextElement();
+                    orig.put(key, addProps.get(key));
+                }
+
+                /// and save
+                FileOutputStream fos = null;
+                try {
+                    fos = new FileOutputStream(origSlingProps);
+                    orig.store(fos, null);
+                } catch (final IOException ioe) {
+                    throw new MojoExecutionException("Unable to save " + origSlingProps,
ioe);
+                } finally {
+                    if ( fis != null ) {
+                        try { fis.close(); } catch (final IOException ignore) {}
+                    }
+                }
+
+                // and repack again
+        		pack(dest, destinationFile);
+		    } finally {
+		        this.tempDirectory.delete();
+		    }
 		} else {
-			getLog().debug(
-					String.format("Skipping copy of base artifact from %s.",
-							artifact.getFile()));
+		    // we can just copy
+    		if (shouldCopy(artifact.getFile(), destinationFile)) {
+    			try {
+    				getLog().info(
+    						String.format("Copying base artifact from %s to %s.",
+    								artifact.getFile(), destinationFile));
+    				FileUtils.copyFile(artifact.getFile(), destinationFile);
+    			} catch (IOException e) {
+    				throw new MojoExecutionException(
+    						"Unable to copy base artifact.", e);
+    			}
+    		} else {
+    			getLog().debug(
+    					String.format("Skipping copy of base artifact from %s.",
+    							artifact.getFile()));
+    		}
 		}
 	}
 
@@ -212,4 +302,21 @@ public class PreparePackageMojo extends 
             throw new MojoExecutionException("Unable to unpack " + source.getPath(), e);
         }
     }
+
+    private void pack(File sourceDir, File destination)
+    throws MojoExecutionException {
+        getLog().info("Packing " + sourceDir.getPath() + " to\n  " + destination.getPath());
+        try {
+            destination.getParentFile().mkdirs();
+
+            jarArchiver.setDestFile(destination);
+            jarArchiver.addDirectory(sourceDir);
+            jarArchiver.setManifest(new File(sourceDir, "META-INF/MANIFEST.MF".replace('/',
File.separatorChar)));
+            jarArchiver.createArchive();
+        } catch (IOException e) {
+            throw new MojoExecutionException("Unable to pack " + sourceDir.getPath(), e);
+        } catch (ArchiverException e) {
+            throw new MojoExecutionException("Unable to pack " + sourceDir.getPath(), e);
+        }
+    }
 }



Mime
View raw message