/maven-amps-plugin/src/main/java/com/atlassian/maven/plugins/amps/osgi/GenerateManifestMojo.java

https://bitbucket.org/mmeinhold/amps · Java · 94 lines · 75 code · 14 blank · 5 comment · 5 complexity · a6220c021c5f073630fac012803d4bc6 MD5 · raw file

  1. package com.atlassian.maven.plugins.amps.osgi;
  2. import java.io.File;
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import com.atlassian.maven.plugins.amps.AbstractAmpsMojo;
  9. import com.google.common.collect.ImmutableMap;
  10. import org.apache.maven.plugin.MojoExecutionException;
  11. import org.apache.maven.plugin.MojoFailureException;
  12. import org.apache.maven.plugins.annotations.Mojo;
  13. import org.apache.maven.plugins.annotations.Parameter;
  14. import org.apache.maven.project.MavenProject;
  15. import aQute.lib.osgi.Constants;
  16. import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
  17. @Mojo(name = "generate-manifest")
  18. public class GenerateManifestMojo extends AbstractAmpsMojo
  19. {
  20. private static final String BUILD_DATE_ATTRIBUTE = "Atlassian-Build-Date";
  21. private static final DateFormat BUILD_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
  22. /**
  23. * The BND instructions for the bundle.
  24. */
  25. @Parameter
  26. private Map<String, String> instructions = new HashMap<String, String>();
  27. public void execute() throws MojoExecutionException, MojoFailureException
  28. {
  29. final MavenProject project = getMavenContext().getProject();
  30. // The Atlassian-Build-Date manifest attribute is used by the Atlassian licensing framework to determine
  31. // chronological order of bundle versions.
  32. final String buildDateStr = String.valueOf(BUILD_DATE_FORMAT.format(new Date()));
  33. final Map<String, String> basicAttributes = ImmutableMap.of(BUILD_DATE_ATTRIBUTE, buildDateStr);
  34. if (!instructions.isEmpty())
  35. {
  36. getLog().info("Generating a manifest for this plugin");
  37. if (!instructions.containsKey(Constants.EXPORT_PACKAGE))
  38. {
  39. instructions.put(Constants.EXPORT_PACKAGE, "");
  40. }
  41. File metainfLib = file(project.getBuild().getOutputDirectory(), "META-INF", "lib");
  42. if (metainfLib.exists())
  43. {
  44. StringBuilder sb = new StringBuilder(".");
  45. for (File lib : metainfLib.listFiles())
  46. {
  47. sb.append(",").append("META-INF/lib/" + lib.getName());
  48. }
  49. instructions.put(Constants.BUNDLE_CLASSPATH, sb.toString());
  50. }
  51. getMavenGoals().generateBundleManifest(instructions, basicAttributes);
  52. }
  53. else
  54. {
  55. if (OsgiHelper.isAtlassianPlugin(project))
  56. {
  57. getLog().warn("Atlassian plugin detected as the organisation name includes the string 'Atlassian'. If " +
  58. "this is meant for production, you should add bundle " +
  59. "instructions specifically configuring what packages are imported and exported. This " +
  60. "helps catch manifest generation bugs during the build rather than upon install. The " +
  61. "bundle generation configuration can be specified " +
  62. "via the <instructions> element in the maven-" + getPluginInformation().getId()+"-plugin configuration. For example:\n" +
  63. " <configuration>\n" +
  64. " <Import-Package>\n" +
  65. " com.atlassian.myplugin*,\n" +
  66. " com.library.optional.*;resolution:=optional,\n" +
  67. " *\n" +
  68. " </Import-Package>\n" +
  69. " </configuration>\n\n" +
  70. "See the Maven bundle plugin (which is used under the covers) for more info: " +
  71. "http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html#ApacheFelixMavenBundlePlugin%28BND%29-Instructions");
  72. }
  73. else
  74. {
  75. getLog().info("No manifest instructions found, adding only non-OSGi manifest attributes");
  76. }
  77. getMavenGoals().generateMinimalManifest(basicAttributes);
  78. }
  79. }
  80. }