de.smartics.maven.plugin.buildmetadata.util.ManifestHelper Maven / Gradle / Ivy
/*
* Copyright 2006-2019 smartics, Kronseder & Reiner GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package de.smartics.maven.plugin.buildmetadata.util;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.ObjectUtils;
import org.codehaus.plexus.util.StringUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
/**
* Helper to write the Manifest to.
*/
public class ManifestHelper {
// ********************************* Fields *********************************
// --- constants ------------------------------------------------------------
// --- members --------------------------------------------------------------
/**
* The maximum length of a key in a Manifest file.
*/
private static final int MANIFEST_KEY_MAX_LENGTH = 70;
/**
* The location to write the manifest to.
*/
private final File manifestFile;
/**
* The section to add the keys in the manifest.
*/
private final String manifestSection;
// ****************************** Initializer *******************************
// ****************************** Constructors ******************************
/**
* Default constructor.
*
* @param manifestFile the location to write the manifest to.
* @param manifestSection the section to add the keys in the manifest.
*/
public ManifestHelper(final File manifestFile, final String manifestSection) {
this.manifestFile = manifestFile;
this.manifestSection = manifestSection;
}
// ****************************** Inner Classes *****************************
// ********************************* Methods ********************************
// --- init -----------------------------------------------------------------
// --- get&set --------------------------------------------------------------
// --- business -------------------------------------------------------------
/**
* Creates a Manifest file based on the given properties.
*
* @param buildMetaDataProperties the properties to add to the Manifest file
* to be written.
* @throws IOException on any problem writing the file.
*/
public void createManifest(final Properties buildMetaDataProperties)
throws IOException {
final Manifest manifest = createManifestInstance(buildMetaDataProperties);
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(manifestFile));
manifest.write(out);
} finally {
IOUtils.closeQuietly(out);
}
}
private Manifest createManifestInstance(
final Properties buildMetaDataProperties) {
final Manifest manifest = new Manifest();
final Attributes attributes = fetchAttributes(manifest);
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
for (final Map.Entry