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

https://bitbucket.org/mmeinhold/amps · Java · 103 lines · 84 code · 11 blank · 8 comment · 9 complexity · 425af0018c1d1d0856773d582c2f1e91 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-test-manifest")
  18. public class ValidateTestManifestMojo 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> testInstructions = new HashMap<String, String>();
  31. public void execute() throws MojoExecutionException, MojoFailureException
  32. {
  33. if(shouldBuildTestPlugin())
  34. {
  35. final File mfile = file(getMavenContext().getProject().getBuild().getTestOutputDirectory(), "META-INF", "MANIFEST.MF");
  36. // Only valid if the manifest exists
  37. if (!skipManifestValidation && mfile.exists())
  38. {
  39. getLog().info("Manifest found, validating...");
  40. InputStream mfin = null;
  41. try
  42. {
  43. checkManifestEndsWithNewLine(mfile);
  44. mfin = new FileInputStream(mfile);
  45. Manifest mf = new Manifest(mfin);
  46. if (testInstructions.containsKey(Constants.IMPORT_PACKAGE))
  47. {
  48. PackageImportVersionValidator validator = new PackageImportVersionValidator(getMavenContext().getProject(),
  49. getMavenContext().getLog(), getPluginInformation().getId());
  50. validator.validate(mf.getMainAttributes().getValue(Constants.IMPORT_PACKAGE));
  51. }
  52. }
  53. catch (IOException e)
  54. {
  55. throw new MojoExecutionException("Unable to read manifest", e);
  56. }
  57. finally
  58. {
  59. IOUtils.closeQuietly(mfin);
  60. }
  61. getLog().info("Manifest validated");
  62. }
  63. else
  64. {
  65. getLog().info("No manifest found or validation skip flag specified, skipping validation");
  66. }
  67. }
  68. }
  69. private void checkManifestEndsWithNewLine(final File mfile)
  70. throws IOException, MojoExecutionException, MojoFailureException
  71. {
  72. InputStream is = null;
  73. try
  74. {
  75. is = new FileInputStream(mfile);
  76. final long bytesToSkip = mfile.length() - 1;
  77. long bytesSkipped = is.skip(bytesToSkip);
  78. if (bytesSkipped != bytesToSkip)
  79. {
  80. throw new MojoExecutionException("Could not skip " + bytesToSkip + " bytes reading " + mfile.getAbsolutePath());
  81. }
  82. else if (is.read() != '\n')
  83. {
  84. throw new MojoFailureException("Manifests must end with a new line. " + mfile.getAbsolutePath() + " doesn't.");
  85. }
  86. }
  87. finally
  88. {
  89. IOUtils.closeQuietly(is);
  90. }
  91. }
  92. }