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

https://bitbucket.org/mmeinhold/amps · Java · 100 lines · 81 code · 11 blank · 8 comment · 8 complexity · 49411a4ab3bfee47e13e551799c0b090 MD5 · raw file

  1. package com.atlassian.maven.plugins.amps.osgi;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.jar.Manifest;
  9. import com.atlassian.maven.plugins.amps.AbstractAmpsMojo;
  10. import org.apache.commons.io.IOUtils;
  11. import org.apache.maven.plugin.MojoExecutionException;
  12. import org.apache.maven.plugin.MojoFailureException;
  13. import org.apache.maven.plugins.annotations.Mojo;
  14. import org.apache.maven.plugins.annotations.Parameter;
  15. import aQute.lib.osgi.Constants;
  16. import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
  17. @Mojo(name = "validate-manifest")
  18. public class ValidateManifestMojo extends AbstractAmpsMojo
  19. {
  20. /**
  21. * Whether to skip validation or not
  22. */
  23. @Parameter(property = "manifest.validation.skip")
  24. protected boolean skipManifestValidation = false;
  25. /**
  26. * The BND instructions for the bundle. We'll only validate the import versions if there was an
  27. * explicit Import-Package list, not if we auto-generated the imports.
  28. */
  29. @Parameter
  30. private Map<String, String> instructions = new HashMap<String, String>();
  31. public void execute() throws MojoExecutionException, MojoFailureException
  32. {
  33. final File mfile = file(getMavenContext().getProject().getBuild().getOutputDirectory(), "META-INF", "MANIFEST.MF");
  34. // Only valid if the manifest exists
  35. if (!skipManifestValidation && mfile.exists())
  36. {
  37. getLog().info("Manifest found, validating...");
  38. InputStream mfin = null;
  39. try
  40. {
  41. checkManifestEndsWithNewLine(mfile);
  42. mfin = new FileInputStream(mfile);
  43. Manifest mf = new Manifest(mfin);
  44. if (instructions.containsKey(Constants.IMPORT_PACKAGE))
  45. {
  46. PackageImportVersionValidator validator = new PackageImportVersionValidator(getMavenContext().getProject(),
  47. getMavenContext().getLog(), getPluginInformation().getId());
  48. validator.validate(mf.getMainAttributes().getValue(Constants.IMPORT_PACKAGE));
  49. }
  50. }
  51. catch (IOException e)
  52. {
  53. throw new MojoExecutionException("Unable to read manifest", e);
  54. }
  55. finally
  56. {
  57. IOUtils.closeQuietly(mfin);
  58. }
  59. getLog().info("Manifest validated");
  60. }
  61. else
  62. {
  63. getLog().info("No manifest found or validation skip flag specified, skipping validation");
  64. }
  65. }
  66. private void checkManifestEndsWithNewLine(final File mfile)
  67. throws IOException, MojoExecutionException, MojoFailureException
  68. {
  69. InputStream is = null;
  70. try
  71. {
  72. is = new FileInputStream(mfile);
  73. final long bytesToSkip = mfile.length() - 1;
  74. long bytesSkipped = is.skip(bytesToSkip);
  75. if (bytesSkipped != bytesToSkip)
  76. {
  77. throw new MojoExecutionException("Could not skip " + bytesToSkip + " bytes reading " + mfile.getAbsolutePath());
  78. }
  79. else if (is.read() != '\n')
  80. {
  81. throw new MojoFailureException("Manifests must end with a new line. " + mfile.getAbsolutePath() + " doesn't.");
  82. }
  83. }
  84. finally
  85. {
  86. IOUtils.closeQuietly(is);
  87. }
  88. }
  89. }