From 65c666c9952bf653f497b1730cf52bd062163e50 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Sun, 14 Jun 2009 21:38:51 +0000 Subject: [PATCH] Introduce feature generation (not complete yet) Build number in MANIFEST git-svn-id: https://svn.argeo.org/maven-plugins/trunk@2551 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../osgi/AbstractBundlesPackagerMojo.java | 13 +- .../argeo/slc/maven/plugins/osgi/Feature.java | 34 ++++ .../plugins/osgi/FeatureDescriptorMojo.java | 177 ++++++++++++++++++ .../argeo/slc/maven/plugins/osgi/Plugin.java | 32 ++++ .../resources/META-INF/plexus/components.xml | 6 +- 5 files changed, 257 insertions(+), 5 deletions(-) create mode 100644 maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Feature.java create mode 100644 maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/FeatureDescriptorMojo.java create mode 100644 maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Plugin.java diff --git a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java index 42ee07e11..b6f1f98a8 100644 --- a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java +++ b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java @@ -73,6 +73,14 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo { */ protected boolean strictSymbolicName; + /** + * Build number (provided by the build number plugin in general). + * + * @parameter expression="${buildNumber}" + * @required + */ + protected String buildNumber; + private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); protected List analyze(boolean willGenerate) throws MojoExecutionException { @@ -138,12 +146,13 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo { if (sIndex >= 0) {// SNAPSHOT versionMain = versionMain(sIndex); // buildId = "D_" + sdf.format(new Date());// D for dev - buildId = "SNAPSHOT"; + buildId = "SNAPSHOT-r" + buildNumber; isSnapshot = true; } else { versionMain = project.getVersion(); // buildId = "R_" + sdf.format(new Date());// R for release - buildId = "R" + sdf.format(new Date()); + //buildId = "R" + sdf.format(new Date()); + buildId = "r" + buildNumber; } if (!versionMain.equals(versionMfMain)) { diff --git a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Feature.java b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Feature.java new file mode 100644 index 000000000..e0d8e931b --- /dev/null +++ b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Feature.java @@ -0,0 +1,34 @@ +package org.argeo.slc.maven.plugins.osgi; + +import java.util.List; + +public class Feature { + private String updateSite; + private String copyright; + private List plugins; + + public String getUpdateSite() { + return updateSite; + } + + public void setUpdateSite(String license) { + this.updateSite = license; + } + + public String getCopyright() { + return copyright; + } + + public void setCopyright(String copyright) { + this.copyright = copyright; + } + + public List getPlugins() { + return plugins; + } + + public void setPlugins(List plugins) { + this.plugins = plugins; + } + +} diff --git a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/FeatureDescriptorMojo.java b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/FeatureDescriptorMojo.java new file mode 100644 index 000000000..9507f6963 --- /dev/null +++ b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/FeatureDescriptorMojo.java @@ -0,0 +1,177 @@ +package org.argeo.slc.maven.plugins.osgi; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.jar.JarInputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.License; +import org.apache.maven.model.Organization; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.argeo.slc.maven.plugin.MavenDependencyManager; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; + +/** + * Generates a feature descriptor based on the pom + * + * @goal featureDescriptor + * @phase process-resources + */ +public class FeatureDescriptorMojo extends AbstractOsgiMojo { + /** @component */ + private MavenDependencyManager mavenDependencyManager; + + /** + * The Maven project. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * The directory for the pom + * + * @parameter expression="${basedir}" + * @required + */ + private File baseDir; + + /** + * Information about the feature + * + * @parameter expression="${argeo-pde.feature}" + */ + private Feature feature; + + public void execute() throws MojoExecutionException { + File featureDesc = new File(baseDir, "feature.xml"); + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(featureDesc); + PrettyPrintXMLWriter xmlWriter = new PrettyPrintXMLWriter( + fileWriter); + xmlWriter.startElement("feature"); + xmlWriter.addAttribute("id", project.getArtifactId()); + xmlWriter.addAttribute("label", project.getName()); + + // Version + String projectVersion = project.getVersion(); + int indexSnapshot = projectVersion.indexOf("-SNAPSHOT"); + if (indexSnapshot > -1) + projectVersion = projectVersion.substring(0, indexSnapshot); + projectVersion = projectVersion + ".qualifier"; + + // project. + xmlWriter.addAttribute("version", projectVersion); + + Organization organization = project.getOrganization(); + if (organization != null && organization.getName() != null) + xmlWriter.addAttribute("provider-name", organization.getName()); + + if (project.getDescription() != null || project.getUrl() != null) { + xmlWriter.startElement("description"); + if (project.getUrl() != null) + xmlWriter.addAttribute("url", project.getUrl()); + if (project.getDescription() != null) + xmlWriter.writeText(project.getDescription()); + xmlWriter.endElement();// description + } + + if (feature != null && feature.getCopyright() != null + || (organization != null && organization.getUrl() != null)) { + xmlWriter.startElement("copyright"); + if (organization != null && organization.getUrl() != null) + xmlWriter.addAttribute("url", organization.getUrl()); + if (feature.getCopyright() != null) + xmlWriter.writeText(feature.getCopyright()); + xmlWriter.endElement();// copyright + } + + if (feature != null && feature.getUpdateSite() != null) { + xmlWriter.startElement("url"); + xmlWriter.startElement("update"); + xmlWriter.addAttribute("url", feature.getUpdateSite()); + xmlWriter.endElement();// update + xmlWriter.endElement();// url + } + + List licenses = project.getLicenses(); + if (licenses.size() > 0) { + // take the first one + License license = (License) licenses.get(0); + xmlWriter.startElement("license"); + + if (license.getUrl() != null) + xmlWriter.addAttribute("url", license.getUrl()); + if (license.getComments() != null) + xmlWriter.writeText(license.getComments()); + else if (license.getName() != null) + xmlWriter.writeText(license.getName()); + xmlWriter.endElement();// license + } + + // deploymentRepository.pathOf(null); + Set dependencies = mavenDependencyManager + .getTransitiveProjectDependencies(project, remoteRepos, + local); + for (Iterator it = dependencies.iterator(); it.hasNext();) { + Artifact depArtifact = (Artifact) it.next(); + + String symbolicName = null; + String version = null; + File artifactFile = depArtifact.getFile(); + JarInputStream jarInputStream = null; + try { + jarInputStream = new JarInputStream(new FileInputStream( + artifactFile)); + symbolicName = jarInputStream.getManifest() + .getMainAttributes() + .getValue("Bundle-SymbolicName"); + version = jarInputStream.getManifest().getMainAttributes() + .getValue("Bundle-Version"); + } catch (Exception e) { + // probably not a jar, skipping + if (getLog().isDebugEnabled()) + getLog().debug( + "Skipping artifact " + depArtifact + + " because of " + e.getMessage()); + continue; + } finally { + IOUtils.closeQuietly(jarInputStream); + } + + if (symbolicName != null && version != null) { + xmlWriter.startElement("plugin"); + xmlWriter.addAttribute("id", symbolicName); + xmlWriter.addAttribute("version", version); + xmlWriter.addAttribute("unpack", "false"); + xmlWriter.endElement();// plugin + } + } + /* + * List plugins = feature.getPlugins(); for (int i = 0; i < + * plugins.size(); i++) { Plugin plugin = (Plugin) plugins.get(i); + * xmlWriter.startElement("plugin"); xmlWriter.addAttribute("id", + * plugin.getId()); xmlWriter.addAttribute("version", + * plugin.getVersion()); xmlWriter.addAttribute("unpack", + * plugin.getUnpack()); xmlWriter.endElement();// plugin } + */ + xmlWriter.endElement();// feature + } catch (Exception e) { + throw new MojoExecutionException("Cannot write feature descriptor", + e); + } + IOUtil.close(fileWriter); + getLog().info("FeatureDescriptorMojo done"); + } +} diff --git a/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Plugin.java b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Plugin.java new file mode 100644 index 000000000..adcbcc955 --- /dev/null +++ b/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/Plugin.java @@ -0,0 +1,32 @@ +package org.argeo.slc.maven.plugins.osgi; + +public class Plugin { + private String id; + private String version; + private String unpack = "false"; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getUnpack() { + return unpack; + } + + public void setUnpack(String unpack) { + this.unpack = unpack; + } + +} diff --git a/maven-argeo-osgi-plugin/src/main/resources/META-INF/plexus/components.xml b/maven-argeo-osgi-plugin/src/main/resources/META-INF/plexus/components.xml index d9ad3c56d..c4a392937 100644 --- a/maven-argeo-osgi-plugin/src/main/resources/META-INF/plexus/components.xml +++ b/maven-argeo-osgi-plugin/src/main/resources/META-INF/plexus/components.xml @@ -10,13 +10,13 @@ - org.argeo.slc.maven:maven-argeo-osgi-plugin:package-bundles + org.argeo.maven.plugins:maven-argeo-osgi-plugin:package-bundles - org.argeo.slc.maven:maven-argeo-osgi-plugin:install-bundles + org.argeo.maven.plugins:maven-argeo-osgi-plugin:install-bundles - org.argeo.slc.maven:maven-argeo-osgi-plugin:deploy-bundles + org.argeo.maven.plugins:maven-argeo-osgi-plugin:deploy-bundles -- 2.39.2