X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=maven%2Fmaven-argeo-osgi-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fmaven%2Fplugins%2Fosgi%2FAbstractBundlesPackagerMojo.java;h=42ee07e11bacf79f633f8a0049f609632375c9af;hb=059c8746edde7073bcb18af43da7195bd8958d81;hp=f3b15fe768b618211b21aaba3db547727e582d76;hpb=dabf4d12e20271de3b705b91eeda442396305d7d;p=gpl%2Fargeo-slc.git diff --git a/maven/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java b/maven/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java index f3b15fe76..42ee07e11 100644 --- a/maven/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java +++ b/maven/maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java @@ -4,18 +4,20 @@ import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.jar.Attributes; import java.util.jar.Manifest; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; /** + * Base class for MoJo analyzing a set of bundles directories. + * * @author mbaudier * */ @@ -46,99 +48,140 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo { */ protected String bundlesPomArtifactId; - protected List analyze() throws MojoExecutionException { - List list = new ArrayList(); + /** + * Whether should fail if MANIFEST version are not in line with pom version. + * + * @parameter expression="${strictManifestVersion}" default-value="false" + * @required + */ + protected boolean strictManifestVersion; - File[] bundleDirs = bundlesDirectory.listFiles(new FileFilter() { - public boolean accept(File file) { - if (!file.isDirectory()) - return false; + /** + * Whether the manifest should be updated with the release version. + * + * @parameter expression="${updateManifestWhenReleasing}" + * default-value="true" + * @required + */ + protected boolean updateManifestWhenReleasing; - return manifestFileFromDir(file).exists(); - } - }); + /** + * Whether should fail if symbolic name does not match artifact id. + * + * @parameter expression="${strictSymbolicName}" default-value="false" + * @required + */ + protected boolean strictSymbolicName; + + private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); + + protected List analyze(boolean willGenerate) throws MojoExecutionException { + List list = new ArrayList(); + + File[] bundleDirs = bundlesDirectory.listFiles(bundleFileFilter()); for (int i = 0; i < bundleDirs.length; i++) { + File dir = bundleDirs[i]; - File manifestFile = manifestFileFromDir(dir); - String artifactId = dir.getName(); - File destFile = new File(packagedBundlesDir.getPath() - + File.separator + artifactId + ".jar"); + BundlePackage bundlePackage; try { - String manifestStr = FileUtils.readFileToString(manifestFile); - char lastChar = manifestStr.charAt(manifestStr.length() - 1); - if (lastChar != '\n') - throw new RuntimeException( - "Manifest " - + manifestFile - + " is not valid, it does not end with and endline character."); - - Manifest manifest = readManifest(manifestFile); - // Symbolic name - String symbolicNameMf = manifest.getMainAttributes().getValue( - "Bundle-SymbolicName"); - if (!artifactId.equals(symbolicNameMf)) - getLog().warn( - "Symbolic name " + symbolicNameMf - + " does not match with directory name " - + artifactId); - - // Version - String versionMf = manifest.getMainAttributes().getValue( - "Bundle-Version"); - int qIndex = versionMf.lastIndexOf(".qualifier"); - String versionMfMain; - if (qIndex >= 0) - versionMfMain = versionMf.substring(0, qIndex); - else - versionMfMain = versionMf; - - int sIndex = project.getVersion().lastIndexOf("-SNAPSHOT"); - String versionMain; - boolean isSnapshot = false; - if (sIndex >= 0) {// SNAPSHOT - versionMain = project.getVersion().substring(0, sIndex); - isSnapshot = true; - } else { - versionMain = project.getVersion(); - } - - if (!versionMain.equals(versionMfMain)) - getLog() - .warn( - "Main manifest version " - + versionMfMain - + " of bundle " - + artifactId - + " do not match with main project version " - + versionMain); - - String newVersionMf; - String newVersionArt; - if (isSnapshot) { - newVersionMf = versionMfMain + ".SNAPSHOT"; - newVersionArt = versionMfMain + "-SNAPSHOT"; - } else { - newVersionMf = versionMfMain; - newVersionArt = versionMfMain; - } - - manifest.getMainAttributes().putValue("Bundle-Version", - newVersionMf); - manifest.getMainAttributes().put( - Attributes.Name.MANIFEST_VERSION, "1.0"); - - Artifact artifact = artifactFactory.createBuildArtifact(project - .getGroupId(), artifactId, newVersionArt, "jar"); - BundlePackage bundlePackage = new BundlePackage(artifact, dir, - new Manifest(manifest), destFile); - list.add(bundlePackage); + bundlePackage = processBundleDir(dir, willGenerate); } catch (Exception e) { throw new MojoExecutionException("Could not analyze " + dir, e); } + list.add(bundlePackage); + } return list; } + protected BundlePackage processBundleDir(File dir, boolean willGenerate) + throws Exception { + File manifestFile = manifestFileFromDir(dir); + String artifactId = dir.getName(); + File destFile = new File(packagedBundlesDir.getPath() + File.separator + + artifactId + ".jar"); + + String manifestStr = FileUtils.readFileToString(manifestFile); + char lastChar = manifestStr.charAt(manifestStr.length() - 1); + if (lastChar != '\n') + throw new RuntimeException("Manifest " + manifestFile + + " is not valid," + + " it does not end with and endline character."); + + Manifest manifest = readManifest(manifestFile); + // Symbolic name + String symbolicNameMf = manifest.getMainAttributes().getValue( + "Bundle-SymbolicName"); + if (!artifactId.equals(symbolicNameMf)) { + String msg = "Symbolic name " + symbolicNameMf + + " does not match with directory name " + artifactId; + if (strictSymbolicName) + throw new RuntimeException(msg); + else + getLog().warn(msg); + } + + // Version + String versionMf = manifest.getMainAttributes().getValue( + "Bundle-Version"); + int qIndex = versionMf.lastIndexOf(".SNAPSHOT"); + String versionMfMain; + if (qIndex >= 0) + versionMfMain = versionMf.substring(0, qIndex); + else + versionMfMain = versionMf; + + int sIndex = snapshotIndex(); + String versionMain; + String buildId; + boolean isSnapshot = false; + if (sIndex >= 0) {// SNAPSHOT + versionMain = versionMain(sIndex); + // buildId = "D_" + sdf.format(new Date());// D for dev + buildId = "SNAPSHOT"; + isSnapshot = true; + } else { + versionMain = project.getVersion(); + // buildId = "R_" + sdf.format(new Date());// R for release + buildId = "R" + sdf.format(new Date()); + } + + if (!versionMain.equals(versionMfMain)) { + String msg = "Main manifest version " + versionMfMain + + " of bundle " + artifactId + + " do not match with main project version " + versionMain; + if (strictManifestVersion) + throw new RuntimeException(msg); + else + getLog().warn(msg); + } + + String newVersionMf = versionMfMain + "." + buildId; + String newVersionArt; + if (isSnapshot) { + newVersionArt = versionMfMain + "-SNAPSHOT"; + } else { + newVersionArt = versionMfMain; + } + + // boolean debug = true; + boolean debug = getLog().isDebugEnabled(); + if (debug && willGenerate) { + getLog().info("\n## " + artifactId); + getLog().info("project.getVersion()=" + project.getVersion()); + getLog().info("newVersionMf=" + newVersionMf); + } + + manifest.getMainAttributes().putValue("Bundle-Version", newVersionMf); + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, + "1.0"); + + Artifact artifact = artifactFactory.createBuildArtifact(project + .getGroupId(), artifactId, newVersionArt, "jar"); + return new BundlePackage(artifact, dir, new Manifest(manifest), + destFile); + } + protected File manifestFileFromDir(File dir) { return new File(dir + File.separator + "META-INF" + File.separator + "MANIFEST.MF"); @@ -153,6 +196,77 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo { bundlesPomArtifactId, project.getVersion(), "pom"); } + protected StringBuffer createPomFileHeader(String parentGroupId, + String parentArtifactId, String parentBaseVersion, String groupId, + String artifactId, String packaging) { + StringBuffer pom = new StringBuffer(); + // not using append() systematically for the sake of clarity + pom.append("\n"); + pom.append("\t4.0.0\n"); + pom.append("\t\n"); + pom.append("\t\t" + parentGroupId + "\n"); + pom.append("\t\t" + parentArtifactId + "\n"); + pom.append("\t\t" + parentBaseVersion + "\n"); + pom.append("\t\n"); + pom.append("\t" + groupId + "\n"); + pom.append("\t" + artifactId + "\n"); + pom.append("\t" + packaging + "\n"); + return pom; + + // TODO: use the Model object e.g.: (from install plugin) + // Model model = new Model(); + // model.setModelVersion( "4.0.0" ); + // model.setGroupId( groupId ); + // model.setArtifactId( artifactId ); + // model.setVersion( version ); + // model.setPackaging( packaging ); + // model.setDescription( "POM was created from install:install-file" ); + // fw = new FileWriter( tempFile ); + // tempFile.deleteOnExit(); + // new MavenXpp3Writer().write( fw, model ); + // ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, + // tempFile ); + // artifact.addMetadata( metadata ); + + } + + /** Simple close the project tag */ + protected String closePomFile(StringBuffer pom) { + pom.append("\n"); + return pom.toString(); + } + + protected Manifest readManifest(File file) throws IOException { + Manifest manifest = new Manifest(); + FileInputStream in = new FileInputStream(file); + manifest.read(in); + in.close(); + return manifest; + } + + protected int snapshotIndex() { + return project.getModel().getVersion().lastIndexOf("-SNAPSHOT"); + } + + protected String versionMain(int sIndex) { + return project.getVersion().substring(0, sIndex); + } + + protected File getBundleDirectory() { + return bundlesDirectory; + } + + protected FileFilter bundleFileFilter() { + return new FileFilter() { + public boolean accept(File file) { + if (!file.isDirectory()) + return false; + + return manifestFileFromDir(file).exists(); + } + }; + } + protected static class BundlePackage { private final Artifact artifact; private final File bundleDir; @@ -183,13 +297,19 @@ public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo { public Manifest getManifest() { return manifest; } - } - protected Manifest readManifest(File file) throws IOException { - Manifest manifest = new Manifest(); - FileInputStream in = new FileInputStream(file); - manifest.read(in); - in.close(); - return manifest; + public File getManifestFile() { + return new File(getPackageFile().getPath() + ".MF"); + } + + public File getPomFile() { + return new File(getPackageFile().getPath() + ".pom.xml"); + } + + public String toString() { + return "Bundle: " + bundleDir; + } + } + }