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; import java.util.jar.Attributes; import org.apache.commons.io.FileUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.archiver.util.DefaultFileSet; /** * @goal package-bundles * @phase package * @author mbaudier * */ public class PackageBundlesMojo extends AbstractBundlesPackagerMojo { public void execute() throws MojoExecutionException, MojoFailureException { StringBuffer bundlesPom = createPomFileHeader(project .getParentArtifact().getGroupId(), project.getParentArtifact() .getArtifactId(), project.getParentArtifact().getBaseVersion(), project.getGroupId(), bundlesPomArtifactId, "pom"); bundlesPom.append("\t\n"); List bundlePackages = analyze(true); for (int i = 0; i < bundlePackages.size(); i++) { AbstractBundlesPackagerMojo.BundlePackage bundlePackage = (BundlePackage) bundlePackages .get(i); // Package as jar JarArchiver jarArchiver = new JarArchiver(); jarArchiver.setDestFile(bundlePackage.getPackageFile()); DefaultFileSet fileSet = new DefaultFileSet(); fileSet.setDirectory(bundlePackage.getBundleDir()); String[] includes = { "**/*" }; String[] excludes = { "**/.svn", "**/.svn/**" }; fileSet.setIncludes(includes); fileSet.setExcludes(excludes); try { File manifestFile = bundlePackage.getManifestFile(); jarArchiver.addFileSet(fileSet); // Write manifest FileOutputStream out = new FileOutputStream(manifestFile); System.out.println("\n# BUNDLE " + bundlePackage.getArtifact().getArtifactId()); Attributes mainAttrs = bundlePackage.getManifest() .getMainAttributes(); for (Iterator it = mainAttrs.keySet().iterator(); it.hasNext();) { Object key = it.next(); Object value = mainAttrs.get(key); System.out.println(key + ": " + value); } bundlePackage.getManifest().write(out); out.close(); jarArchiver.setManifest(manifestFile); jarArchiver.createArchive(); } catch (Exception e) { throw new MojoExecutionException("Could not package bundle " + bundlePackage.getBundleDir(), e); } // Write bundle POM File pomFile = bundlePackage.getPomFile(); StringBuffer pomBuf = createPomFileHeader(project .getParentArtifact().getGroupId(), project .getParentArtifact().getArtifactId(), project .getParentArtifact().getBaseVersion(), bundlePackage .getArtifact().getGroupId(), bundlePackage.getArtifact() .getArtifactId(), "jar"); String pomStr = closePomFile(pomBuf); try { FileUtils.writeStringToFile(pomFile, pomStr); } catch (IOException e) { throw new MojoExecutionException( "Could not write pom for bundle " + bundlePackage.getArtifact().getArtifactId(), e); } // update dependencies POM bundlesPom.append("\t\t\n"); bundlesPom .append("\t\t\t" + bundlePackage.getArtifact().getGroupId() + "\n"); bundlesPom.append("\t\t\t" + bundlePackage.getArtifact().getArtifactId() + "\n"); bundlesPom .append("\t\t\t" + bundlePackage.getArtifact().getVersion() + "\n"); bundlesPom.append("\t\t\n"); } bundlesPom.append("\t\n"); String bundlePomStr = closePomFile(bundlesPom); try { FileUtils.writeStringToFile(bundlesPomFile(), bundlePomStr); } catch (IOException e) { throw new MojoExecutionException("Could not write dependency pom", e); } } }