2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.odlparent.filter.manifest.plugin;
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;
27 * Mojo processing filtering an input Manifest file into an output, retaining only selected entries.
29 @Mojo(name = "filter-manifest", threadSafe = true)
30 public class FilterManifestMojo extends AbstractMojo {
31 private static final Logger LOG = LoggerFactory.getLogger(FilterManifestMojo.class);
34 * Input {@code MANIFEST.MF} file.
36 @Parameter(required = true)
37 private File inputFile;
40 * Output {@code MANIFEST.MF} file.
42 @Parameter(required = true)
43 private File outputFile;
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
50 @Parameter(required = true)
51 private List<String> retainedAttributes;
54 * Default constructor.
56 public FilterManifestMojo() {
57 // to fix javadoc warnings
61 public void execute() throws MojoExecutionException {
62 LOG.debug("Filtering {} to {} retaining {}", inputFile, outputFile, retainedAttributes);
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);
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());
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));
80 for (String attr : retainedAttributes) {
81 final String value = inputAttrs.getValue(attr);
83 throw new MojoExecutionException("Attribute " + attr + " is not present in " + inputFile);
86 LOG.debug("Propagating attribute {} value {}", attr, value);
87 outputAttrs.putValue(attr, value);
89 LOG.debug("Output manifest has attributes {}", outputAttrs.keySet());
91 try (OutputStream os = Files.newOutputStream(outputFile.toPath())) {
93 } catch (IOException e) {
94 throw new MojoExecutionException("Failed to write output " + outputFile, e);