import java.util.List;
import java.util.jar.Attributes;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
public class PackageBundlesMojo extends AbstractBundlesPackagerMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
- StringBuffer bundlesPom = new StringBuffer();
- // not using append() systematically for the sake of clarity
- bundlesPom.append("<project>\n");
- bundlesPom.append("\t<modelVersion>4.0.0</modelVersion>\n");
- bundlesPom.append("\t<parent>\n");
- bundlesPom.append("\t\t<groupId>"
- + project.getParentArtifact().getGroupId() + "</groupId>\n");
- bundlesPom.append("\t\t<artifactId>"
- + project.getParentArtifact().getArtifactId()
- + "</artifactId>\n");
- bundlesPom.append("\t\t<version>"
- + project.getParentArtifact().getVersion() + "</version>\n");
- bundlesPom.append("\t</parent>\n");
- bundlesPom
- .append("\t<groupId>" + project.getGroupId() + "</groupId>\n");
- bundlesPom.append("\t<artifactId>" + bundlesPomArtifactId
- + "</artifactId>\n");
- bundlesPom.append("\t<packaging>pom</packaging>\n");
+ StringBuffer bundlesPom = createPomFileHeader(project
+ .getParentArtifact().getGroupId(), project.getParentArtifact()
+ .getArtifactId(), project.getParentArtifact().getBaseVersion(),
+ project.getGroupId(), bundlesPomArtifactId, "pom");
bundlesPom.append("\t<dependencies>\n");
- List bundlePackages = analyze();
+ List bundlePackages = analyze(true);
for (int i = 0; i < bundlePackages.size(); i++) {
AbstractBundlesPackagerMojo.BundlePackage bundlePackage = (BundlePackage) bundlePackages
.get(i);
- File manifestFile = new File(bundlePackage.getPackageFile()
- .getPath()
- + ".MF");
-
// Package as jar
JarArchiver jarArchiver = new JarArchiver();
jarArchiver.setDestFile(bundlePackage.getPackageFile());
String[] excludes = { "**/.svn", "**/.svn/**" };
fileSet.setIncludes(includes);
fileSet.setExcludes(excludes);
+
+ FileOutputStream manifestOut = null;
try {
+ File manifestFile = bundlePackage.getManifestFile();
jarArchiver.addFileSet(fileSet);
// Write manifest
- FileOutputStream out = new FileOutputStream(manifestFile);
- bundlePackage.getManifest().getMainAttributes().put(
- Attributes.Name.MANIFEST_VERSION, "1.0");
+ manifestOut = new FileOutputStream(manifestFile);
- System.out.println("# BUNDLE "
+ System.out.println("\n# BUNDLE "
+ bundlePackage.getArtifact().getArtifactId());
Attributes mainAttrs = bundlePackage.getManifest()
.getMainAttributes();
System.out.println(key + ": " + value);
}
- bundlePackage.getManifest().write(out);
- out.close();
- jarArchiver.setManifest(manifestFile);
+ bundlePackage.getManifest().write(manifestOut);
+ // Write jar
+ jarArchiver.setManifest(manifestFile);
jarArchiver.createArchive();
} catch (Exception e) {
throw new MojoExecutionException("Could not package bundle "
+ bundlePackage.getBundleDir(), e);
+ }finally{
+ IOUtils.closeQuietly(manifestOut);
+ }
+
+ // 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</dependencies>\n");
- bundlesPom.append("</project>\n");
+ String bundlePomStr = closePomFile(bundlesPom);
try {
- FileWriter writer = new FileWriter(bundlesPomFile());
- writer.write(bundlesPom.toString());
- writer.close();
+ FileUtils.writeStringToFile(bundlesPomFile(), bundlePomStr);
} catch (IOException e) {
throw new MojoExecutionException("Could not write dependency pom",
e);
}
}
-
}