You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2006/06/21 17:35:58 UTC

svn commit: r416007 - /incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java

Author: rickhall
Date: Wed Jun 21 08:35:57 2006
New Revision: 416007

URL: http://svn.apache.org/viewvc?rev=416007&view=rev
Log:
Added a patch by Hal Hildebrand to support embedded JAR file inlining
(i.e., unrolling), which makes it possible to use the bundle on the
class path as a normal JAR.

Modified:
    incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java

Modified: incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java?rev=416007&r1=416006&r2=416007&view=diff
==============================================================================
--- incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java (original)
+++ incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java Wed Jun 21 08:35:57 2006
@@ -19,6 +19,8 @@
 
 import java.io.*;
 import java.util.*;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipEntry;
 
 import org.apache.maven.archiver.*;
 import org.apache.maven.artifact.*;
@@ -46,7 +48,14 @@
 
 	int									bundleManifestVersion	= 1;
 
-	/**
+    /**
+     * Jars to Inline
+     *
+     * @parameter
+     */
+    private List inlinedArtifacts = new ArrayList();
+
+    /**
 	 * The Maven project.
 	 * 
 	 * @parameter expression="${project}"
@@ -147,7 +156,9 @@
 			ArchiverException, ManifestException,
 			DependencyResolutionRequiredException, MojoExecutionException {
 
-		verifyDeclaredBundleManifestVersion();
+        outputDirectory.mkdirs();
+
+        verifyDeclaredBundleManifestVersion();
 
 		getLog().info("Generating JAR bundle " + jarFile.getAbsolutePath());
 
@@ -159,8 +170,11 @@
 		addManifestFile();
 		addManifestEntries();
 
-		// Add the JARs that were specified in the POM
-		// as "not" provided
+        // Inline the contents of the indicated jar artifacts
+        inlineArtifacts();
+
+        // Add the JARs that were specified in the POM
+		// as "not" provided and which are not inlined
 		addEmbeddedJars();
 		addBundleVersion();
 
@@ -353,7 +367,7 @@
 		for (Iterator m = test.iterator(); m.hasNext();) {
 			Object o = m.next();
 			// For backwards compatibility with existing builds, only
-            // Issue a warning for missing imports. On the other hand,
+            // issue a warning for missing imports. On the other hand,
             // if packages are being ignored, then this is a new project
             // so be more strict and throw an error.
             if (osgiManifest.getIgnorePackage() == null) {
@@ -601,6 +615,8 @@
 	 * a dependency has a scope of either RUNTIME or COMPILE, then we'll JAR
 	 * them up inside the OSGi bundle artifact. We will then add the
 	 * Bundle-Classpath manifest entry.
+     *
+     * Artifacts which are inlined will not be included.
 	 */
 	private void addEmbeddedJars() throws MojoExecutionException {
 		Set artifacts = project.getArtifacts();
@@ -612,19 +628,22 @@
 				String type = artifact.getType();
 
 				if ("jar".equals(type)) {
-					File depFile = artifact.getFile();
-
-					try {
-						FileUtils.copyFileToDirectory(depFile, outputDirectory);
-
-					}
-					catch (Exception e) {
-						String errmsg = "Error copying "
-								+ depFile.getAbsolutePath() + " to "
-								+ outputDirectory.getAbsolutePath();
-						throw new MojoExecutionException(errmsg, e);
-					}
-				}
+                    // Do not include artifacts which are inlined
+                    if (!(inlinedArtifacts.contains(artifact.getArtifactId()))) {
+                        File depFile = artifact.getFile();
+
+                        try {
+                            FileUtils.copyFileToDirectory(depFile, outputDirectory);
+
+                        }
+                        catch (Exception e) {
+                            String errmsg = "Error copying "
+                                            + depFile.getAbsolutePath() + " to "
+                                            + outputDirectory.getAbsolutePath();
+                            throw new MojoExecutionException(errmsg, e);
+                        }
+                    }
+                }
 			}
 		}
 	}
@@ -736,4 +755,49 @@
         }
         getLog().debug("Referenced package list: \n" + buf.toString());
     }
-}
+
+    private void inlineArtifacts() throws MojoExecutionException {
+		Set artifacts = project.getArtifacts();
+
+		for (Iterator it = artifacts.iterator(); it.hasNext();) {
+			Artifact artifact = (Artifact) it.next();
+			if (!Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
+					&& !Artifact.SCOPE_TEST.equals(artifact.getScope())) {
+				String type = artifact.getType();
+
+				if ("jar".equals(type)) {
+                    if ((inlinedArtifacts.contains(artifact.getArtifactId()))) {
+                        File depFile = artifact.getFile();
+
+                        try {
+                            ZipFile inlinedJar = new ZipFile(depFile);
+                            Enumeration entries = inlinedJar.entries();
+                            byte[] buffer = new byte[4096];
+                            while (entries.hasMoreElements()) {
+                                ZipEntry entry = (ZipEntry) entries.nextElement();
+                                if (entry.isDirectory()) {
+                                    new File(outputDirectory, entry.getName()).mkdirs();
+                                } else {
+                                    // Have to do this because some JARs ship without directories, i.e., just files.
+                                    new File(outputDirectory, entry.getName()).getParentFile().mkdirs();
+                                    FileOutputStream out = new FileOutputStream(new File(outputDirectory, entry.getName()));
+                                    InputStream in = inlinedJar.getInputStream(entry);
+                                    for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
+                                        out.write(buffer, 0, read);
+                                    }
+                                    in.close();
+                                    out.close();
+                                }
+                            }
+                        } catch (Exception e) {
+                            String errmsg = "Error inlining "
+                                            + depFile.getAbsolutePath() + " to "
+                                            + outputDirectory.getAbsolutePath();
+                            throw new MojoExecutionException(errmsg, e);
+                        }
+                    }
+                }
+			}
+		}
+    }
+}
\ No newline at end of file