diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-22 00:27:56 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-22 00:27:56 +0000 |
commit | 499fab874b29bb31c063f4841a3a5d2610e24b39 (patch) | |
tree | 270255850a3a118c5d63fb1917e9370a453da0a1 /branches/sca-equinox | |
parent | a88471bf854ab9e297d4e17c3a9558e5a274e051 (diff) |
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
Diffstat (limited to 'branches/sca-equinox')
3 files changed, 233 insertions, 21 deletions
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<File> jarFiles, String name, String symbolicName, String version) + static Manifest libraryManifest(Set<File> jarFiles, String name, String symbolicName, String version, String dir) throws IllegalStateException { try { @@ -103,7 +104,9 @@ final class BundleUtil { Set<String> exportedPackages = new HashSet<String>(); 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<String> importedPackages = new HashSet<String>(); 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<String> 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<String> 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 <n; i++) { + for (int i = 0, n = exports.length(); i < n; i++) { char c = exports.charAt(i); if (c == '\"') { q = !q; } if (!q) { if (c == ',') { - + // Add the exported package to the set, after making sure it really exists in // the JAR String export = buffer.toString(); @@ -334,7 +346,7 @@ final class BundleUtil { buffer.append(c); } if (buffer.length() != 0) { - + // Add the exported package to the set, after making sure it really exists in // the JAR String export = buffer.toString(); @@ -362,7 +374,7 @@ final class BundleUtil { } else { matcher = pattern.matcher(jarFile); found = matcher.find(); - } + } if (found) { version = matcher.group(); if (version.endsWith(".")) { diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java index 4dd42967ae..45135b4748 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleBuildMojo.java @@ -88,7 +88,7 @@ public class ThirdPartyBundleBuildMojo extends AbstractMojo { } String bundleName = null; try { - bundleName = BundleUtil.getBundleName(artifact.getFile()); + bundleName = BundleUtil.getBundleSymbolicName(artifact.getFile()); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } @@ -108,7 +108,7 @@ public class ThirdPartyBundleBuildMojo extends AbstractMojo { version = version.substring(0, version.length() - Artifact.SNAPSHOT_VERSION.length() - 1); } - Manifest mf = BundleUtil.libraryManifest(jarFiles, project.getName(), symbolicName, version); + Manifest mf = BundleUtil.libraryManifest(jarFiles, project.getName(), symbolicName, version, "lib"); File file = new File(project.getBasedir(), "META-INF"); file.mkdir(); file= new File(file, "MANIFEST.MF"); diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleDistroMojo.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleDistroMojo.java new file mode 100644 index 0000000000..c607a57ad8 --- /dev/null +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ThirdPartyBundleDistroMojo.java @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.tools.bundle.plugin; + +import static org.apache.tuscany.sca.tools.bundle.plugin.BundleUtil.write; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.HashSet; +import java.util.Set; +import java.util.jar.Manifest; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; + +/** + * @version $Rev$ $Date$ + * @goal build-thirdparty-distro + * @phase generate-resources + * @requiresDependencyResolution test + * @description Build an OSGi bundle for third party dependencies + */ +public class ThirdPartyBundleDistroMojo extends AbstractMojo { + /** + * The project to create a build for. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + public void execute() throws MojoExecutionException { + Log log = getLog(); + + if (project.getPackaging().equals("pom")) { + return; + } + try { + + File root = new File(project.getBasedir(), "eclipse/plugins/"); + root.mkdirs(); + + Set<String> ids = new HashSet<String>(); + 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<File> jarFiles = new HashSet<File>(); + 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<String> ids) { + ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + ps.println("<?pde version=\"3.2\"?>"); + + ps.println("<target name=\"Apache Tuscany Eclipse Target\">"); + ps.println("<location path=\"${project_loc}/eclipse\"/>"); + + ps.println("<content>"); + ps.println("<plugins>"); + for (String id : ids) { + ps.println("<plugin id=\"" + id + "\"/>"); + } + ps.println("</plugins>"); + ps.println("<features>"); + ps.println("</features>"); + ps.println("<extraLocations>"); + ps.println("<location path=\"${eclipse_home}\"/>"); + ps.println("</extraLocations>"); + ps.println("</content>"); + + ps.println("</target>"); + + } + + private static void writePluginXML(PrintStream ps) { + ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + ps.println("<?pde version=\"3.2\"?>"); + ps.println("<plugin>"); + ps.println("<extension point = \"org.eclipse.pde.core.targets\">"); + ps.println("<target"); + ps.println("id=\"org.apache.tuscany.sca.target\""); + ps.println("name=\"Apache Tuscany Eclipse Target\""); + ps.println("path=\"tuscany.target\"/>"); + ps.println("</extension>"); + ps.println("</plugin>"); + } +} |