Add update manifest feature
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 5 Jun 2009 07:20:31 +0000 (07:20 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 5 Jun 2009 07:20:31 +0000 (07:20 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk/maven@2481 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

maven-argeo-osgi-plugin/pom.xml
maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java
maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractOsgiMojo.java
maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/EquinoxExecMojo.java
maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/PackageBundlesMojo.java
maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/UpdateManifestsMojo.java [new file with mode: 0644]

index 137de4b62ecfaa4d3ebd1efee35545291b328979..4d2f66f1bb73e5c2383f461a1082fbae39967ea8 100644 (file)
@@ -7,7 +7,7 @@
                <relativePath>..</relativePath>\r
        </parent>\r
        <artifactId>maven-argeo-osgi-plugin</artifactId>\r
-       <version>0.1.11</version>\r
+       <version>0.1.12</version>\r
        <packaging>maven-plugin</packaging>\r
        <name>Argeo OSGi Plugin</name>\r
 \r
index ee3cd28a3168dd259ca02586c04a4c4ccf32b838..42ee07e11bacf79f633f8a0049f609632375c9af 100644 (file)
@@ -3,7 +3,6 @@ package org.argeo.slc.maven.plugins.osgi;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -13,16 +12,12 @@ import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.metadata.ArtifactMetadata;
-import org.apache.maven.model.Model;
-import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 
 /**
+ * Base class for MoJo analyzing a set of bundles directories.
+ * 
  * @author mbaudier
  * 
  */
@@ -83,14 +78,7 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
        protected List analyze(boolean willGenerate) throws MojoExecutionException {
                List list = new ArrayList();
 
-               File[] bundleDirs = bundlesDirectory.listFiles(new FileFilter() {
-                       public boolean accept(File file) {
-                               if (!file.isDirectory())
-                                       return false;
-
-                               return manifestFileFromDir(file).exists();
-                       }
-               });
+               File[] bundleDirs = bundlesDirectory.listFiles(bundleFileFilter());
                for (int i = 0; i < bundleDirs.length; i++) {
 
                        File dir = bundleDirs[i];
@@ -143,12 +131,12 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
                else
                        versionMfMain = versionMf;
 
-               int sIndex = project.getModel().getVersion().lastIndexOf("-SNAPSHOT");
+               int sIndex = snapshotIndex();
                String versionMain;
                String buildId;
                boolean isSnapshot = false;
                if (sIndex >= 0) {// SNAPSHOT
-                       versionMain = project.getVersion().substring(0, sIndex);
+                       versionMain = versionMain(sIndex);
                        // buildId = "D_" + sdf.format(new Date());// D for dev
                        buildId = "SNAPSHOT";
                        isSnapshot = true;
@@ -181,11 +169,6 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
                if (debug && willGenerate) {
                        getLog().info("\n## " + artifactId);
                        getLog().info("project.getVersion()=" + project.getVersion());
-                       // getLog().info(
-                       // "project.getModel().getVersion()="
-                       // + project.getModel().getVersion());
-                       // getLog().info("versionMf=" + versionMf);
-                       // getLog().info("buildId=" + buildId);
                        getLog().info("newVersionMf=" + newVersionMf);
                }
 
@@ -253,6 +236,37 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
                return pom.toString();
        }
 
+       protected Manifest readManifest(File file) throws IOException {
+               Manifest manifest = new Manifest();
+               FileInputStream in = new FileInputStream(file);
+               manifest.read(in);
+               in.close();
+               return manifest;
+       }
+
+       protected int snapshotIndex() {
+               return project.getModel().getVersion().lastIndexOf("-SNAPSHOT");
+       }
+
+       protected String versionMain(int sIndex) {
+               return project.getVersion().substring(0, sIndex);
+       }
+
+       protected File getBundleDirectory() {
+               return bundlesDirectory;
+       }
+
+       protected FileFilter bundleFileFilter() {
+               return new FileFilter() {
+                       public boolean accept(File file) {
+                               if (!file.isDirectory())
+                                       return false;
+
+                               return manifestFileFromDir(file).exists();
+                       }
+               };
+       }
+
        protected static class BundlePackage {
                private final Artifact artifact;
                private final File bundleDir;
@@ -291,13 +305,11 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
                public File getPomFile() {
                        return new File(getPackageFile().getPath() + ".pom.xml");
                }
-       }
 
-       protected Manifest readManifest(File file) throws IOException {
-               Manifest manifest = new Manifest();
-               FileInputStream in = new FileInputStream(file);
-               manifest.read(in);
-               in.close();
-               return manifest;
+               public String toString() {
+                       return "Bundle: " + bundleDir;
+               }
+
        }
+
 }
index 063444db8a89ea89c73277390d1c97ba866a0c69..be64d975430450c1189a2580bad7cc777b4e18dc 100644 (file)
@@ -10,6 +10,8 @@ import org.apache.maven.project.MavenProject;
  * Factorize common configuration
  */
 public abstract class AbstractOsgiMojo extends AbstractMojo {
+       public final static String PACKAGING_BUNDLE = "bundle";
+
        /**
         * The maven project.
         * 
index ef849a5d6e1a7132cfd9e2dad2dc01b815675411..1289b55861bffd7f316756582c7db329d0ea3c0c 100644 (file)
@@ -134,9 +134,9 @@ public class EquinoxExecMojo extends AbstractOsgiMojo {
        protected long pause;
 
        public void execute() throws MojoExecutionException, MojoFailureException {
-               if ("bundles".equals(project.getArtifact().getType())) {
-                       System.out.println("Skip artifact of type bundles "
-                                       + artifactToString(project.getArtifact()));
+               if (PACKAGING_BUNDLE.equals(project.getArtifact().getType())) {
+                       System.out.println("Skip artifact of type " + PACKAGING_BUNDLE
+                                       + " " + artifactToString(project.getArtifact()));
                        return;
                }
 
index 80074cb2e51d4ea81be9570c26ceebeb268e94c7..2e3030812dd4cdd941c192a7dd96306b3c641cd1 100644 (file)
@@ -2,7 +2,6 @@ package org.argeo.slc.maven.plugins.osgi;
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
@@ -15,7 +14,7 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.util.DefaultFileSet;
 
-/**
+/** Build the bundle jars
  * @goal package-bundles
  * @phase package
  * @author mbaudier
diff --git a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/UpdateManifestsMojo.java b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/UpdateManifestsMojo.java
new file mode 100644 (file)
index 0000000..f48a28b
--- /dev/null
@@ -0,0 +1,66 @@
+package org.argeo.slc.maven.plugins.osgi;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+/**
+ * Update the manifests based on the POM
+ * 
+ * @goal update-manifests
+ * @phase package
+ * @author mbaudier
+ * 
+ */
+public class UpdateManifestsMojo extends AbstractBundlesPackagerMojo {
+
+       public void execute() throws MojoExecutionException, MojoFailureException {
+               if (!PACKAGING_BUNDLE.equals(project.getPackaging())) {
+                       getLog().info(
+                                       "Project is not of packaging type " + PACKAGING_BUNDLE
+                                                       + ", skipping...");
+               }
+
+               int sIndex = snapshotIndex();
+               String versionMf = null;
+               if (sIndex >= 0) {// SNAPSHOT
+                       versionMf = versionMain(sIndex) + ".SNAPSHOT";
+               } else {
+                       throw new MojoExecutionException("Can only modify on SNAPSHOT");
+               }
+
+               File[] bundleDirs = getBundleDirectory().listFiles(bundleFileFilter());
+               for (int i = 0; i < bundleDirs.length; i++) {
+                       OutputStream out = null;
+                       try {
+                               File dir = bundleDirs[i];
+                               File originalMf = manifestFileFromDir(dir);
+                               Manifest manifest = readManifest(originalMf);
+                               manifest.getMainAttributes().putValue("Bundle-Version",
+                                               versionMf);
+                               manifest.getMainAttributes().put(
+                                               Attributes.Name.MANIFEST_VERSION, "1.0");
+
+                               out = new FileOutputStream(originalMf);
+                               manifest.write(out);
+                               getLog().info(
+                                               "Update MANIFEST of bundle " + dir + " with version "
+                                                               + versionMf);
+                       } catch (IOException e) {
+                               throw new MojoExecutionException(
+                                               "Could not modify manifets. WARNING: some manifests may already have been modified! Check your sources.",
+                                               e);
+                       } finally {
+                               IOUtils.closeQuietly(out);
+                       }
+               }
+
+       }
+}