diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-24 06:42:51 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-10-24 06:42:51 +0000 |
commit | fe85e4d6a072ecbe175e9e602d05b6ad345226ab (patch) | |
tree | a6293357252c7cdb6304c7162be47cb79936a927 /branches/sca-equinox/tools | |
parent | 12af73bcbdfa19ed75672143849e41cd5ddd04de (diff) |
Derive the OSGi version from maven version
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@707562 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-equinox/tools')
3 files changed, 72 insertions, 62 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 216d4a3c60..f104225483 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 @@ -38,11 +38,13 @@ import java.util.Set; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.osgi.framework.Version; + /** * Common functions used by the plugin. * @@ -103,7 +105,7 @@ final class BundleUtil { StringBuffer classpath = new StringBuffer(); Set<String> exportedPackages = new HashSet<String>(); for (File jarFile : jarFiles) { - addPackages(jarFile, exportedPackages); + addPackages(jarFile, exportedPackages, version); if (dir != null) { classpath.append(dir).append("/"); } @@ -184,10 +186,10 @@ final class BundleUtil { * @param packages * @throws IOException */ - private static void addPackages(File jarFile, Set<String> packages) throws IOException { + private static void addPackages(File jarFile, Set<String> packages, String version) throws IOException { if (getBundleSymbolicName(jarFile) == null) { - String version = ";version=" + version(jarFile.getPath()); - addAllPackages(jarFile, packages, version); + String ver = ";version=" + version; + addAllPackages(jarFile, packages, ver); } else { addExportedPackages(jarFile, packages); } @@ -356,33 +358,29 @@ final class BundleUtil { } } - static private Pattern pattern = Pattern.compile("-([0-9.]+)"); - static private Pattern pattern2 = Pattern.compile("_([0-9.]+)"); - /** - * Derive a bundle version from the given JAR file name. - * - * @param jarFile + * Convert the maven version into OSGi version + * @param mavenVersion * @return */ - static String version(String jarFile) { - String version = "1.0.0"; - boolean found = false; - Matcher matcher = pattern2.matcher(jarFile); - if (matcher.find()) { - found = true; - } else { - matcher = pattern.matcher(jarFile); - found = matcher.find(); - } - if (found) { - version = matcher.group(); - if (version.endsWith(".")) { - version = version.substring(1, version.length() - 1); - } else { - version = version.substring(1); + static String osgiVersion(String mavenVersion) { + ArtifactVersion ver = new DefaultArtifactVersion(mavenVersion); + String qualifer = ver.getQualifier(); + if (qualifer != null) { + StringBuffer buf = new StringBuffer(qualifer); + for (int i = 0; i < buf.length(); i++) { + char c = buf.charAt(i); + if (Character.isLetterOrDigit(c) || c == '-' || c == '_') { + // Keep as-is + } else { + buf.setCharAt(i, '_'); + } } + qualifer = buf.toString(); } + Version osgiVersion = + new Version(ver.getMajorVersion(), ver.getMinorVersion(), ver.getIncrementalVersion(), qualifer); + String version = osgiVersion.toString(); return version; } diff --git a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ModuleBundlesBuildMojo.java b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ModuleBundlesBuildMojo.java index 73394ec0b0..f8bd7b91ab 100644 --- a/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ModuleBundlesBuildMojo.java +++ b/branches/sca-equinox/tools/maven/maven-bundle-plugin/src/main/java/org/apache/tuscany/sca/tools/bundle/plugin/ModuleBundlesBuildMojo.java @@ -46,7 +46,7 @@ import org.apache.maven.project.MavenProject; * @description Generate a modules directory containing OSGi bundles for all the project's module dependencies. */ public class ModuleBundlesBuildMojo extends AbstractMojo { - + /** * The project to create a distribution for. * @@ -62,7 +62,7 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { * @parameter */ private File targetDirectory; - + /** * Directories containing artifacts to exclude. * @@ -92,6 +92,12 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { private boolean generateTargetPlatform; /** + * A list of Eclipse features to be added to the target definition + * @parameter + */ + private String[] eclipseFeatures; + + /** * Set to true to generate a plugin.xml. * * @parameter @@ -102,7 +108,7 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { Log log = getLog(); try { - + // Create the target directory File root; if (targetDirectory == null) { @@ -111,9 +117,9 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { root = targetDirectory; } root.mkdirs(); - + // Build sets of exclude directories and included/excluded/groupids - Set<String> excludedFileNames = new HashSet<String>(); + Set<String> excludedFileNames = new HashSet<String>(); if (excludeDirectories != null) { for (File f: excludeDirectories) { if (f.isDirectory()) { @@ -131,7 +137,7 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { } Set<String> excludedGroupIds = new HashSet<String>(); if (excludeGroupIds != null) { - for (String g: excludeGroupIds) { + for (String g : excludeGroupIds) { excludedGroupIds.add(g); } } @@ -142,18 +148,19 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { Artifact artifact = (Artifact)o; // Only consider Compile and Runtime dependencies - if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) { + if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact + .getScope()))) { if (log.isDebugEnabled()) { log.debug("Skipping artifact: " + artifact); } continue; } - + // Only consider JAR and WAR files if (!"jar".equals(artifact.getType()) && !"war".equals(artifact.getType())) { continue; } - + // Exclude artifact if its groupId is excluded or if it's not included if (excludedGroupIds.contains(artifact.getGroupId())) { log.debug("Artifact groupId is excluded: " + artifact); @@ -171,11 +178,11 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { log.warn("Artifact doesn't exist: " + artifact); continue; } - + if (log.isDebugEnabled()) { log.debug("Processing artifact: " + artifact); } - + // Get the bundle name if the artifact is an OSGi bundle String bundleName = null; try { @@ -183,9 +190,9 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } - + if (bundleName != null) { - + // Exclude artifact if its file name is excluded if (excludedFileNames.contains(artifactFile.getName())) { log.debug("Artifact file is excluded: " + artifact); @@ -196,9 +203,9 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { log.info("Adding OSGi bundle artifact: " + artifact); copyFile(artifactFile, root); bundleSymbolicNames.add(bundleName); - + } else if ("war".equals(artifact.getType())) { - + // Exclude artifact if its file name is excluded if (excludedFileNames.contains(artifactFile.getName())) { log.debug("Artifact file is excluded: " + artifact); @@ -209,9 +216,9 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { log.info("Adding WAR artifact: " + artifact); copyFile(artifactFile, root); bundleSymbolicNames.add(bundleName); - + } else { - + File dir = new File(root, artifactFile.getName().substring(0, artifactFile.getName().length() - 4)); // Exclude artifact if its file name is excluded @@ -222,12 +229,13 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { // Create a bundle directory for a non-OSGi JAR log.info("Adding JAR artifact: " + artifact); - String version = BundleUtil.version(artifactFile.getPath()); + String version = BundleUtil.osgiVersion(artifact.getVersion()); 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); + String symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId()); + Manifest mf = + BundleUtil.libraryManifest(jarFiles, symbolicName, symbolicName, version, null); File file = new File(dir, "META-INF"); file.mkdirs(); file = new File(file, "MANIFEST.MF"); @@ -239,15 +247,15 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { bundleSymbolicNames.add(symbolicName); } } - + // Generate a PDE target if (generateTargetPlatform) { File target = new File(project.getBasedir(), "tuscany.target"); FileOutputStream targetFile = new FileOutputStream(target); - writeTarget(new PrintStream(targetFile), bundleSymbolicNames); + writeTarget(new PrintStream(targetFile), bundleSymbolicNames, eclipseFeatures); targetFile.close(); } - + // Generate a plugin.xml referencing the PDE target if (generatePlugin) { File pluginxml = new File(project.getBasedir(), "plugin.xml"); @@ -279,13 +287,14 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { out.close(); } - private static void writeTarget(PrintStream ps, Set<String> ids) { + private static void writeTarget(PrintStream ps, Set<String> ids, String[] features) { 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("<location useDefault=\"true\"/>"); + // ps.println("<content useAllPlugins=\"true\">"); ps.println("<content>"); ps.println("<plugins>"); for (String id : ids) { @@ -293,10 +302,15 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { } ps.println("</plugins>"); ps.println("<features>"); + if (features != null) { + for (String f : features) { + ps.println("<feature id=\"" + f + "\"/>"); + } + } ps.println("</features>"); ps.println("<extraLocations>"); // Not sure why the extra path needs to the plugins folder - ps.println("<location path=\"${eclipse_home}/plugins\"/>"); + ps.println("<location path=\"${project_loc}/target/plugins\"/>"); ps.println("</extraLocations>"); ps.println("</content>"); 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 bcbd6ed7b3..e183e7e197 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 @@ -44,7 +44,7 @@ import org.apache.maven.project.MavenProject; * @description Build an OSGi bundle for the project's third party dependencies */ public class ThirdPartyBundleBuildMojo extends AbstractMojo { - + /** * The project to build the bundle for. * @@ -60,7 +60,7 @@ public class ThirdPartyBundleBuildMojo extends AbstractMojo { * @parameter */ private String symbolicName; - + public void execute() throws MojoExecutionException { Log log = getLog(); @@ -69,7 +69,8 @@ public class ThirdPartyBundleBuildMojo extends AbstractMojo { for (Object o : project.getArtifacts()) { Artifact artifact = (Artifact)o; - if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) { + if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact + .getScope()))) { if (log.isDebugEnabled()) { log.debug("Skipping artifact: " + artifact); } @@ -102,15 +103,12 @@ public class ThirdPartyBundleBuildMojo extends AbstractMojo { } try { - String version = project.getVersion(); - if (version.endsWith(Artifact.SNAPSHOT_VERSION)) { - version = version.substring(0, version.length() - Artifact.SNAPSHOT_VERSION.length() - 1); - } + String version = BundleUtil.osgiVersion(project.getVersion()); 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"); + file = new File(file, "MANIFEST.MF"); if (log.isDebugEnabled()) { log.debug("Generating " + file); } |