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

com.marvelution.utils.maven.cli.MavenCommandLineParser Maven / Gradle / Ivy

/*
 * Copyright 2008 Marvelution.com
 *
 * 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 com.marvelution.utils.maven.cli;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.codehaus.plexus.util.cli.CommandLineUtils;

/**
 * Helper class to parse a Maven 2.x Command
 * 
 * @author Mark Rekveld
 */
public final class MavenCommandLineParser {

	/**
	 * List of possible Maven 2.x command options
	 */
	private static final Options OPTIONS = new Options();

	/**
	 * Set System property option
	 */
	public static final char SET_SYSTEM_PROPERTY = 'D';

	/**
	 * Work off-line option
	 */
	public static final char OFFLINE = 'o';

	/**
	 * Project Reactor option
	 */
	public static final char REACTOR = 'r';

	/**
	 * Quit execution option
	 */
	public static final char QUIET = 'q';

	/**
	 * Show Debug information option
	 */
	public static final char DEBUG = 'X';

	/**
	 * Show Error information option
	 */
	public static final char ERRORS = 'e';

	/**
	 * Batch-Mode option
	 */
	public static final char BATCH_MODE = 'B';

	/**
	 * Do not recurse into sub-projects option
	 */
	public static final char NON_RECURSIVE = 'N';

	/**
	 * Update Snapshots option
	 */
	public static final char UPDATE_SNAPSHOTS = 'U';

	/**
	 * Activate profiles option
	 */
	public static final char ACTIVATE_PROFILES = 'P';

	/**
	 * Force plugin update option
	 */
	public static final String FORCE_PLUGIN_UPDATES = "cpu";

	/**
	 * Synonym for {@link MavenCommandLineParser#FORCE_PLUGIN_UPDATES}
	 */
	public static final String FORCE_PLUGIN_UPDATES2 = "up";

	/**
	 * Suppres plugin update check option
	 */
	public static final String SUPPRESS_PLUGIN_UPDATES = "npu";

	/**
	 * Suppres use of ~/.m2/plugin-registery.xml option
	 */
	public static final String SUPPRESS_PLUGIN_REGISTRY = "npr";

	/**
	 * Checksum failure policy option
	 */
	public static final char CHECKSUM_FAILURE_POLICY = 'C';

	/**
	 * Checksum waring politcy option
	 */
	public static final char CHECKSUM_WARNING_POLICY = 'c';

	/**
	 * Alternate user settings file option
	 */
	public static final char ALTERNATE_USER_SETTINGS = 's';

	/**
	 * Alternate POM file option
	 */
	public static final char ALTERNATE_POM_FILE = 'f';

	/**
	 * Fail Fast option
	 */
	public static final String FAIL_FAST = "ff";

	/**
	 * Fail at End option
	 */
	public static final String FAIL_AT_END = "fae";

	/**
	 * Fail Never option
	 */
	public static final String FAIL_NEVER = "fn";

	/**
	 * Show Version option
	 */
	public static final char VERSION = 'v';

	/**
	 * Show Help option
	 */
	public static final char HELP = 'h';

	static {
		OptionBuilder.withLongOpt("file");
		OptionBuilder.hasArg();
		OptionBuilder.withDescription("Force the use of an alternate POM file.");
		OPTIONS.addOption(OptionBuilder.create(ALTERNATE_POM_FILE));

		OptionBuilder.withLongOpt("help");
		OptionBuilder.withDescription("Display help information");
		OPTIONS.addOption(OptionBuilder.create(HELP));

		OptionBuilder.withLongOpt("version");
		OptionBuilder.withDescription("Display version information");
		OPTIONS.addOption(OptionBuilder.create(VERSION));

		OptionBuilder.withLongOpt("define");
		OptionBuilder.hasArg();
		OptionBuilder.withDescription("Define a system property");
		OPTIONS.addOption(OptionBuilder.create(SET_SYSTEM_PROPERTY));

		OptionBuilder.withLongOpt("offline");
		OptionBuilder.withDescription("Work offline");
		OPTIONS.addOption(OptionBuilder.create(OFFLINE));

		OptionBuilder.withLongOpt("quiet");
		OptionBuilder.withDescription("Quiet output - only show errors");
		OPTIONS.addOption(OptionBuilder.create(QUIET));

		OptionBuilder.withLongOpt("debug");
		OptionBuilder.withDescription("Produce execution debug output");
		OPTIONS.addOption(OptionBuilder.create(DEBUG));

		OptionBuilder.withLongOpt("errors");
		OptionBuilder.withDescription("Produce execution error messages");
		OPTIONS.addOption(OptionBuilder.create(ERRORS));

		OptionBuilder.withLongOpt("reactor");
		OptionBuilder.withDescription("Execute goals for project found in the reactor");
		OPTIONS.addOption(OptionBuilder.create(REACTOR));

		OptionBuilder.withLongOpt("non-recursive");
		OptionBuilder.withDescription("Do not recurse into sub-projects");
		OPTIONS.addOption(OptionBuilder.create(NON_RECURSIVE));

		OptionBuilder.withLongOpt("update-snapshots");
		OptionBuilder.withDescription("Forces a check for updated releases and snapshots on remote repositories");
		OPTIONS.addOption(OptionBuilder.create(UPDATE_SNAPSHOTS));

		OptionBuilder.withLongOpt("activate-profiles");
		OptionBuilder.withDescription("Comma-delimited list of profiles to activate");
		OptionBuilder.hasArg();
		OPTIONS.addOption(OptionBuilder.create(ACTIVATE_PROFILES));

		OptionBuilder.withLongOpt("batch-mode");
		OptionBuilder.withDescription("Run in non-interactive (batch) mode");
		OPTIONS.addOption(OptionBuilder.create(BATCH_MODE));

		OptionBuilder.withLongOpt("check-plugin-updates");
		OptionBuilder.withDescription("Force upToDate check for any relevant registered plugins");
		OPTIONS.addOption(OptionBuilder.create(FORCE_PLUGIN_UPDATES));

		OptionBuilder.withLongOpt("update-plugins");
		OptionBuilder.withDescription("Synonym for cpu");
		OPTIONS.addOption(OptionBuilder.create(FORCE_PLUGIN_UPDATES2));

		OptionBuilder.withLongOpt("no-plugin-updates");
		OptionBuilder.withDescription("Suppress upToDate check for any relevant registered plugins");
		OPTIONS.addOption(OptionBuilder.create(SUPPRESS_PLUGIN_UPDATES));

		OptionBuilder.withLongOpt("no-plugin-registry");
		OptionBuilder.withDescription("Don't use ~/.m2/plugin-registry.xml for plugin versions");
		OPTIONS.addOption(OptionBuilder.create(SUPPRESS_PLUGIN_REGISTRY));

		OptionBuilder.withLongOpt("strict-checksums");
		OptionBuilder.withDescription("Fail the build if checksums don't match");
		OPTIONS.addOption(OptionBuilder.create(CHECKSUM_FAILURE_POLICY));

		OptionBuilder.withLongOpt("lax-checksums");
		OptionBuilder.withDescription("Warn if checksums don't match");
		OPTIONS.addOption(OptionBuilder.create(CHECKSUM_WARNING_POLICY));

		OptionBuilder.withLongOpt("settings");
		OptionBuilder.withDescription("Alternate path for the user settings file");
		OptionBuilder.hasArg();
		OPTIONS.addOption(OptionBuilder.create(ALTERNATE_USER_SETTINGS));

		OptionBuilder.withLongOpt("fail-fast");
		OptionBuilder.withDescription("Stop at first failure in reactorized builds");
		OPTIONS.addOption(OptionBuilder.create(FAIL_FAST));

		OptionBuilder.withLongOpt("fail-at-end");
		OptionBuilder.withDescription("Only fail the build afterwards; allow all non-impacted builds to continue");
		OPTIONS.addOption(OptionBuilder.create(FAIL_AT_END));

		OptionBuilder.withLongOpt("fail-never");
		OptionBuilder.withDescription("NEVER fail the build, regardless of project result");
		OPTIONS.addOption(OptionBuilder.create(FAIL_NEVER));
	}

	/**
	 * Parse the given arguments {@link String} into a {@link CommandLine}
	 * 
	 * @param arguments the arguments {@link String} to parse
	 * @return the parsed {@link CommandLine}
	 * @throws ParseException in case of parse exceptions
	 */
	public static CommandLine parse(String arguments) throws ParseException {
		try {
			final String[] args = CommandLineUtils.translateCommandline(arguments);
			return parse(args);
		} catch (Exception e) {
			throw new ParseException(e.getMessage());
		}
	}

	/**
	 * Parse the given arguments array into a {@link CommandLine}
	 * 
	 * @param arguments the argumants array to parse
	 * @return the parsed {@link CommandLine}
	 * @throws ParseException in case of parse exceptions
	 */
	public static CommandLine parse(String[] arguments) throws ParseException {
		final String[] cleanArgs = cleanArgs(arguments);
		final CommandLineParser parser = new GnuParser();
		return parser.parse(OPTIONS, cleanArgs);
	}

	/**
	 * Clean-up the arguments
	 * 
	 * @param args the arguments to clean-up
	 * @return the cleaned-up arguments
	 */
	private static String[] cleanArgs(String[] args) {
		final List cleaned = new ArrayList();
		StringBuffer currentArg = null;
		for (int i = 0; i < args.length; ++i) {
			final String arg = args[i];
			boolean addedToBuffer = false;
			if (arg.startsWith("\"")) {
				if (currentArg != null) {
					cleaned.add(currentArg.toString());
				}
				currentArg = new StringBuffer(arg.substring(1));
				addedToBuffer = true;
			}
			if (arg.endsWith("\"")) {
				final String cleanArgPart = arg.substring(0, arg.length() - 1);
				if (currentArg != null) {
					if (addedToBuffer) {
						currentArg.setLength(currentArg.length() - 1);
					} else {
						currentArg.append(' ').append(cleanArgPart);
					}
					cleaned.add(currentArg.toString());
				} else {
					cleaned.add(cleanArgPart);
				}
				currentArg = null;
			} else if (!(addedToBuffer)) {
				if (currentArg != null) {
					currentArg.append(' ').append(arg);
				} else {
					cleaned.add(arg);
				}
			}
		}
		if (currentArg != null) {
			cleaned.add(currentArg.toString());
		}
		final int cleanedSz = cleaned.size();
		String[] cleanArgs = null;
		if (cleanedSz == 0) {
			cleanArgs = args;
		} else {
			cleanArgs = (String[]) (String[]) cleaned.toArray(new String[cleanedSz]);
		}
		return cleanArgs;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy