From 499fab874b29bb31c063f4841a3a5d2610e24b39 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 22 Oct 2008 00:27:56 +0000 Subject: Add a mojo to build a local distro and configure the target platform for Eclipse git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@706819 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/tools/bundle/plugin/BundleUtil.java | 50 ++++-- .../bundle/plugin/ThirdPartyBundleBuildMojo.java | 4 +- .../bundle/plugin/ThirdPartyBundleDistroMojo.java | 200 +++++++++++++++++++++ 3 files changed, 233 insertions(+), 21 deletions(-) create mode 100644 branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleDistroMojo.java (limited to 'branches/sca-equinox') diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java index de28a99446..d145f0c7b2 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/BundleUtil.java @@ -49,7 +49,7 @@ import java.util.zip.ZipInputStream; * @version $Rev$ $Date$ */ final class BundleUtil { - + /** * Returns the name of a bundle, or null if the given file is not a bundle. * @@ -57,7 +57,7 @@ final class BundleUtil { * @return * @throws IOException */ - static String getBundleName(File file) throws IOException { + static String getBundleSymbolicName(File file) throws IOException { if (!file.exists()) { return null; } @@ -83,7 +83,7 @@ final class BundleUtil { } return bundleName; } - + /** * Generate a Bundle manifest for a set of JAR files. * @@ -91,10 +91,11 @@ final class BundleUtil { * @param name * @param symbolicName * @param version + * @param dir * @return * @throws IllegalStateException */ - static Manifest libraryManifest(Set jarFiles, String name, String symbolicName, String version) + static Manifest libraryManifest(Set jarFiles, String name, String symbolicName, String version, String dir) throws IllegalStateException { try { @@ -103,7 +104,9 @@ final class BundleUtil { Set exportedPackages = new HashSet(); for (File jarFile : jarFiles) { addPackages(jarFile, exportedPackages); - classpath.append("lib/"); + if (dir != null) { + classpath.append(dir).append("/"); + } classpath.append(jarFile.getName()); classpath.append(","); } @@ -113,7 +116,7 @@ final class BundleUtil { StringBuffer imports = new StringBuffer(); Set importedPackages = new HashSet(); for (String export : exportedPackages) { - + // Add export declaration exports.append(export); exports.append(','); @@ -136,9 +139,15 @@ final class BundleUtil { attributes.putValue(BUNDLE_NAME, name); attributes.putValue(BUNDLE_VERSION, version); attributes.putValue(DYNAMICIMPORT_PACKAGE, "*"); - attributes.putValue(EXPORT_PACKAGE, exports.substring(0, exports.length() - 1)); - attributes.putValue(IMPORT_PACKAGE, imports.substring(0, imports.length() - 1)); - attributes.putValue(BUNDLE_CLASSPATH, classpath.substring(0, classpath.length() - 1)); + if (exports.length() > 1) { + attributes.putValue(EXPORT_PACKAGE, exports.substring(0, exports.length() - 1)); + } + if (imports.length() > 1) { + attributes.putValue(IMPORT_PACKAGE, imports.substring(0, imports.length() - 1)); + } + if (classpath.length() > 1) { + attributes.putValue(BUNDLE_CLASSPATH, classpath.substring(0, classpath.length() - 1)); + } return manifest; } catch (IOException e) { @@ -176,7 +185,7 @@ final class BundleUtil { * @throws IOException */ private static void addPackages(File jarFile, Set packages) throws IOException { - if (getBundleName(jarFile) == null) { + if (getBundleSymbolicName(jarFile) == null) { String version = ";version=" + version(jarFile.getPath()); addAllPackages(jarFile, packages, version); } else { @@ -193,10 +202,13 @@ final class BundleUtil { * @throws IOException */ private static void write(Attributes attributes, String key, DataOutputStream dos) throws IOException { + String value = attributes.getValue(key); + if (value == null) { + return; + } StringBuffer line = new StringBuffer(); line.append(key); line.append(": "); - String value = attributes.getValue(key); line.append(new String(value.getBytes("UTF8"))); line.append("\r\n"); int l = line.length(); @@ -224,7 +236,7 @@ final class BundleUtil { String base = export.substring(0, sc); int v = export.indexOf("version="); if (v != -1) { - sc = export.indexOf(';', v+1); + sc = export.indexOf(';', v + 1); if (sc != -1) { return base + ";" + export.substring(v, sc); } else { @@ -275,7 +287,7 @@ final class BundleUtil { } return export; } - + /** * Add the packages exported by a bundle. * @@ -288,7 +300,7 @@ final class BundleUtil { if (!file.exists()) { return; } - + // Read the export-package declaration and get a list of the packages available in a JAR Set existingPackages = null; String exports = null; @@ -309,18 +321,18 @@ final class BundleUtil { if (exports == null) { return; } - + // Parse the export-package declaration, and extract the individual packages StringBuffer buffer = new StringBuffer(); boolean q = false; - for (int i =0, n = exports.length(); i ids = new HashSet(); + String projectGroupId = project.getGroupId(); + for (Object o : project.getArtifacts()) { + Artifact artifact = (Artifact)o; + + if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact + .getScope()))) { + if (log.isDebugEnabled()) { + log.debug("Skipping artifact: " + artifact); + } + continue; + } + if (!"jar".equals(artifact.getType())) { + continue; + } + if (projectGroupId.equals(artifact.getGroupId())) { + continue; + } + + if (log.isDebugEnabled()) { + log.debug("Artifact: " + artifact); + } + String bundleName = null; + try { + bundleName = BundleUtil.getBundleSymbolicName(artifact.getFile()); + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + File artifactFile = artifact.getFile(); + if (!artifactFile.exists()) { + log.warn("Artifact doesn't exist: " + artifact); + continue; + } + + if (bundleName != null) { + log.info("Adding third party bundle: " + artifact); + copyFile(artifactFile, root); + ids.add(bundleName); + } else { + log.info("Adding third party jar: " + artifact); + String version = artifact.getVersion(); + if (version.endsWith(Artifact.SNAPSHOT_VERSION)) { + version = version.substring(0, version.length() - Artifact.SNAPSHOT_VERSION.length() - 1); + } + + Set jarFiles = new HashSet(); + jarFiles.add(artifactFile); + String symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId()).replace('-', '.'); + Manifest mf = + BundleUtil.libraryManifest(jarFiles, symbolicName + "_" + version, symbolicName, version, null); + File dir = new File(root, artifactFile.getName().substring(0, artifactFile.getName().length() - 4)); + File file = new File(dir, "META-INF"); + file.mkdirs(); + file = new File(file, "MANIFEST.MF"); + + FileOutputStream fos = new FileOutputStream(file); + write(mf, fos); + fos.close(); + copyFile(artifactFile, dir); + ids.add(symbolicName); + } + } + + File target = new File(project.getBasedir(), "tuscany.target"); + FileOutputStream targetFile = new FileOutputStream(target); + writeTarget(new PrintStream(targetFile), ids); + targetFile.close(); + + File pluginxml = new File(project.getBasedir(), "plugin.xml"); + FileOutputStream pluginXMLFile = new FileOutputStream(pluginxml); + writePluginXML(new PrintStream(pluginXMLFile)); + pluginXMLFile.close(); + + + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } + + } + + private static void copyFile(File jar, File dir) throws FileNotFoundException, IOException { + byte[] buf = new byte[4096]; + File jarFile = new File(dir, jar.getName()); + FileInputStream in = new FileInputStream(jar); + FileOutputStream out = new FileOutputStream(jarFile); + for (;;) { + int len = in.read(buf); + if (len > 0) { + out.write(buf, 0, len); + } else { + break; + } + } + in.close(); + out.close(); + } + + private static void writeTarget(PrintStream ps, Set ids) { + ps.println(""); + ps.println(""); + + ps.println(""); + ps.println(""); + + ps.println(""); + ps.println(""); + for (String id : ids) { + ps.println(""); + } + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + + ps.println(""); + + } + + private static void writePluginXML(PrintStream ps) { + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + ps.println(""); + } +} -- cgit v1.2.3