All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jlgrock.javascriptframework.jsar.JsarPropertiesUtil Maven / Gradle / Ivy

The newest version!
package com.github.jlgrock.javascriptframework.jsar;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.maven.archiver.PomPropertiesUtil;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.util.IOUtil;

/**
 * Creates the Archive properties to inject.
 */
public class JsarPropertiesUtil extends PomPropertiesUtil {
	/**
	 * The Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(JsarPropertiesUtil.class);

	/**
	 * Creates the tag "Generated by Maven" so that there is no confusion about where it comes from.
	 */
	private static final String GENERATED_BY_MAVEN = "Generated by Maven";

	/**
	 * Checks to see if it has the same contents in the properties file.
	 * @param props the properties to compare
	 * @param file the properties file to check
	 * @return true if they are the same, false otherwise
	 * @throws IOException if there is a problem creating the directory or reading the input stream
	 */
	private boolean sameContents(final Properties props, final File file)
			throws IOException {
		if (!file.isFile()) {
			return false;
		}
		Properties fileProps = new Properties();
		InputStream istream = null;
		try {
			istream = new FileInputStream(file);
			fileProps.load(istream);
			istream.close();
			istream = null;
			return fileProps.equals(props);
		} catch (IOException e) {
			return false;
		} finally {
			IOUtil.close(istream);
		}
	}

	/**
	 * Creates a property file for insertion into the javascript archive (jsar).
	 * @param properties the properties to store
	 * @param outputFile the output file to create
	 * @param forceCreation whether or not to force creation of this file (if it already exists)
	 * @throws IOException if there is a problem creating the directory
	 */
	private void createPropertyFile(final Properties properties, final File outputFile,
			final boolean forceCreation) throws IOException {
		File outputDir = outputFile.getParentFile();
		if (outputDir != null && !outputDir.isDirectory()
				&& !outputDir.mkdirs()) {
			throw new IOException("Failed to create directory: " + outputDir);
		}
		if (!forceCreation && sameContents(properties, outputFile)) {
			return;
		}
		OutputStream os = new FileOutputStream(outputFile);
		try {
			properties.store(os, GENERATED_BY_MAVEN);
			os.close(); // stream is flushed but not closed by
						// Properties.store()
			os = null;
		} finally {
			IOUtil.close(os);
		}
	}

	/**
	 * Creates the pom.properties file.
	 * @param project the maven project to access the maven properties
	 * @param archiver the archiver to use for creating archives
	 * @param pomPropertiesFile the properties file to save
	 * @param forceCreation whether or not to force creation of the properties file
	 * @param debugFilename the name of the debug file
	 * @throws ArchiverException if the archiver had a problem archiving
	 * @throws IOException if there was a problem writing or reading files
	 */
	public final void createPomProperties(final MavenProject project, final Archiver archiver,
			final File pomPropertiesFile, final boolean forceCreation, final String debugFilename)
			throws ArchiverException, IOException {
		final String artifactId = project.getArtifactId();
		final String groupId = project.getGroupId();

		Properties p = new Properties();

		p.setProperty("groupId", project.getGroupId());

		p.setProperty("artifactId", project.getArtifactId());

		p.setProperty("version", project.getVersion());

		LOGGER.debug("debugFilename: " + debugFilename);
		String[] splitName = debugFilename.split("\\.");
		String artifactSpecificName = "";
		for (int i = 0; i < splitName.length; i++) {

			if (i == splitName.length - 1) {
				artifactSpecificName += "[" + project.getArtifactId() + "].";
			}

			artifactSpecificName += splitName[i];
		}

		p.setProperty("debugFilename", artifactSpecificName);

		createPropertyFile(p, pomPropertiesFile, forceCreation);

		archiver.addFile(pomPropertiesFile, "META-INF/maven/" + groupId + "/"
				+ artifactId + "/pom.properties");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy