PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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