Fix a Javadoc warning
[odlparent.git] / filter-manifest-plugin / src / main / java / org / opendaylight / odlparent / filter / manifest / plugin / FilterManifestMojo.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlparent.filter.manifest.plugin;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.nio.file.Files;
15 import java.util.List;
16 import java.util.jar.Attributes;
17 import java.util.jar.Attributes.Name;
18 import java.util.jar.Manifest;
19 import org.apache.maven.plugin.AbstractMojo;
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugins.annotations.Mojo;
22 import org.apache.maven.plugins.annotations.Parameter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Mojo processing filtering an input Manifest file into an output, retaining only selected entries.
28  */
29 @Mojo(name = "filter-manifest", threadSafe = true)
30 public class FilterManifestMojo extends AbstractMojo {
31     private static final Logger LOG = LoggerFactory.getLogger(FilterManifestMojo.class);
32
33     /**
34      * Input {@code MANIFEST.MF} file.
35      */
36     @Parameter(required = true)
37     private File inputFile;
38
39     /**
40      * Output {@code MANIFEST.MF} file.
41      */
42     @Parameter(required = true)
43     private File outputFile;
44
45     /**
46      * List of main attributes that should be copied from input to output. All other attributes (aside from version)
47      * and entries are ignored. If an attribute is specified here and it does not exist in input, plugin execution will
48      * fail.
49      */
50     @Parameter(required = true)
51     private List<String> retainedAttributes;
52
53     /**
54      * Default constructor.
55      */
56     public FilterManifestMojo() {
57         // to fix javadoc warnings
58     }
59
60     @Override
61     public void execute() throws MojoExecutionException {
62         LOG.debug("Filtering {} to {} retaining {}", inputFile, outputFile, retainedAttributes);
63
64         final Manifest input;
65         try (InputStream is = Files.newInputStream(inputFile.toPath())) {
66             input = new Manifest(is);
67         } catch (IOException e) {
68             throw new MojoExecutionException("Failed to read input " + inputFile, e);
69         }
70
71         // Keep only the entries we are instructed to keep
72         final Attributes inputAttrs = input.getMainAttributes();
73         LOG.debug("Input manifest has attributes {}", inputAttrs.keySet());
74
75         final Manifest output = new Manifest();
76         final Attributes outputAttrs = output.getMainAttributes();
77         // We need to always emit version
78         outputAttrs.putValue(Name.MANIFEST_VERSION.toString(), inputAttrs.getValue(Name.MANIFEST_VERSION));
79
80         for (String attr : retainedAttributes) {
81             final String value = inputAttrs.getValue(attr);
82             if (value == null) {
83                 throw new MojoExecutionException("Attribute " + attr + " is not present in " + inputFile);
84             }
85
86             LOG.debug("Propagating attribute {} value {}", attr, value);
87             outputAttrs.putValue(attr, value);
88         }
89         LOG.debug("Output manifest has attributes {}", outputAttrs.keySet());
90
91         try (OutputStream os = Files.newOutputStream(outputFile.toPath())) {
92             output.write(os);
93         } catch (IOException e) {
94             throw new MojoExecutionException("Failed to write output " + outputFile, e);
95         }
96     }
97 }