use of org.codehaus.plexus.util.cli.CommandLineException in project maven-plugins by apache.
the class AbstractJModMojo method executeCommand.
protected void executeCommand(Commandline cmd, File outputDirectory) throws MojoExecutionException {
if (getLog().isDebugEnabled()) {
// no quoted arguments ???
getLog().debug(CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", ""));
}
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
try {
int exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
String output = (StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim());
if (exitCode != 0) {
if (StringUtils.isNotEmpty(output)) {
// Reconsider to use WARN / ERROR ?
getLog().info(output);
}
StringBuilder msg = new StringBuilder("\nExit code: ");
msg.append(exitCode);
if (StringUtils.isNotEmpty(err.getOutput())) {
msg.append(" - ").append(err.getOutput());
}
msg.append('\n');
msg.append("Command line was: ").append(cmd).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
if (StringUtils.isNotEmpty(output)) {
String[] splitLines = StringUtils.split(output, "\n");
for (String outputLine : splitLines) {
getLog().info(outputLine);
}
}
} catch (CommandLineException e) {
throw new MojoExecutionException("Unable to execute jmod command: " + e.getMessage(), e);
}
}
use of org.codehaus.plexus.util.cli.CommandLineException in project maven-plugins by apache.
the class GpgSigner method generateSignatureForFile.
/**
* {@inheritDoc}
*/
@Override
protected void generateSignatureForFile(File file, File signature) throws MojoExecutionException {
// ----------------------------------------------------------------------------
// Set up the command line
// ----------------------------------------------------------------------------
Commandline cmd = new Commandline();
if (StringUtils.isNotEmpty(executable)) {
cmd.setExecutable(executable);
} else {
cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
}
if (args != null) {
for (String arg : args) {
cmd.createArg().setValue(arg);
}
}
if (homeDir != null) {
cmd.createArg().setValue("--homedir");
cmd.createArg().setFile(homeDir);
}
if (useAgent) {
cmd.createArg().setValue("--use-agent");
} else {
cmd.createArg().setValue("--no-use-agent");
}
InputStream in = null;
if (null != passphrase) {
// make --passphrase-fd effective in gpg2
cmd.createArg().setValue("--batch");
cmd.createArg().setValue("--passphrase-fd");
cmd.createArg().setValue("0");
// Prepare the input stream which will be used to pass the passphrase to the executable
in = new ByteArrayInputStream(passphrase.getBytes());
}
if (null != keyname) {
cmd.createArg().setValue("--local-user");
cmd.createArg().setValue(keyname);
}
cmd.createArg().setValue("--armor");
cmd.createArg().setValue("--detach-sign");
if (!isInteractive) {
cmd.createArg().setValue("--no-tty");
}
if (!defaultKeyring) {
cmd.createArg().setValue("--no-default-keyring");
}
if (StringUtils.isNotEmpty(secretKeyring)) {
cmd.createArg().setValue("--secret-keyring");
cmd.createArg().setValue(secretKeyring);
}
if (StringUtils.isNotEmpty(publicKeyring)) {
cmd.createArg().setValue("--keyring");
cmd.createArg().setValue(publicKeyring);
}
if ("once".equalsIgnoreCase(lockMode)) {
cmd.createArg().setValue("--lock-once");
} else if ("multiple".equalsIgnoreCase(lockMode)) {
cmd.createArg().setValue("--lock-multiple");
} else if ("never".equalsIgnoreCase(lockMode)) {
cmd.createArg().setValue("--lock-never");
}
cmd.createArg().setValue("--output");
cmd.createArg().setFile(signature);
cmd.createArg().setFile(file);
try {
int exitCode = CommandLineUtils.executeCommandLine(cmd, in, new DefaultConsumer(), new DefaultConsumer());
if (exitCode != 0) {
throw new MojoExecutionException("Exit code: " + exitCode);
}
} catch (CommandLineException e) {
throw new MojoExecutionException("Unable to execute gpg command", e);
}
}
use of org.codehaus.plexus.util.cli.CommandLineException in project maven-plugins by apache.
the class AbstractJavadocMojo method executeJavadocCommandLine.
/**
* Execute the Javadoc command line
*
* @param cmd not null
* @param javadocOutputDirectory not null
* @throws MavenReportException if any errors occur
*/
private void executeJavadocCommandLine(Commandline cmd, File javadocOutputDirectory) throws MavenReportException {
if (getLog().isDebugEnabled()) {
// no quoted arguments
getLog().debug(CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", ""));
}
String cmdLine = null;
if (debug) {
cmdLine = CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", "");
cmdLine = JavadocUtil.hideProxyPassword(cmdLine, settings);
writeDebugJavadocScript(cmdLine, javadocOutputDirectory);
}
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
try {
int exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
String output = (StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim());
if (exitCode != 0) {
if (cmdLine == null) {
cmdLine = CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", "");
cmdLine = JavadocUtil.hideProxyPassword(cmdLine, settings);
}
writeDebugJavadocScript(cmdLine, javadocOutputDirectory);
if (StringUtils.isNotEmpty(output) && StringUtils.isEmpty(err.getOutput()) && isJavadocVMInitError(output)) {
throw new MavenReportException(output + '\n' + '\n' + JavadocUtil.ERROR_INIT_VM + '\n' + "Or, try to reduce the Java heap size for the Javadoc goal using " + "-Dminmemory=<size> and -Dmaxmemory=<size>." + '\n' + '\n' + "Command line was: " + cmdLine + '\n' + '\n' + "Refer to the generated Javadoc files in '" + javadocOutputDirectory + "' dir.\n");
}
if (StringUtils.isNotEmpty(output)) {
getLog().info(output);
}
StringBuilder msg = new StringBuilder("\nExit code: ");
msg.append(exitCode);
if (StringUtils.isNotEmpty(err.getOutput())) {
msg.append(" - ").append(err.getOutput());
}
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
msg.append("Refer to the generated Javadoc files in '").append(javadocOutputDirectory).append("' dir.\n");
throw new MavenReportException(msg.toString());
}
if (StringUtils.isNotEmpty(output)) {
getLog().info(output);
}
} catch (CommandLineException e) {
throw new MavenReportException("Unable to execute javadoc command: " + e.getMessage(), e);
}
if (StringUtils.isNotEmpty(err.getOutput()) && getLog().isWarnEnabled()) {
getLog().warn("Javadoc Warnings");
StringTokenizer token = new StringTokenizer(err.getOutput(), "\n");
while (token.hasMoreTokens()) {
String current = token.nextToken().trim();
getLog().warn(current);
}
}
}
use of org.codehaus.plexus.util.cli.CommandLineException in project cxf by apache.
the class AbstractCodeGeneratorMojo method runForked.
private void runForked(Set<URI> classPath, Class<?> cls, String[] args) throws MojoExecutionException {
getLog().info("Running wadl2java in fork mode...");
Commandline cmd = new Commandline();
// for JVM args
cmd.getShell().setQuotedArgumentsEnabled(true);
cmd.setWorkingDirectory(project.getBuild().getDirectory());
try {
cmd.setExecutable(getJavaExecutable().getAbsolutePath());
} catch (IOException e) {
getLog().debug(e);
throw new MojoExecutionException(e.getMessage(), e);
}
cmd.createArg().setLine(additionalJvmArgs);
final File file;
try {
// file = new File("/tmp/test.jar");
file = FileUtils.createTempFile("cxf-codegen", ".jar");
JarArchiver jar = new JarArchiver();
jar.setDestFile(file.getAbsoluteFile());
Manifest manifest = new Manifest();
Attribute attr = new Attribute();
attr.setName("Class-Path");
StringBuilder b = new StringBuilder(8000);
for (URI cp : classPath) {
b.append(cp.toURL().toExternalForm()).append(' ');
}
attr.setValue(b.toString());
manifest.getMainSection().addConfiguredAttribute(attr);
attr = new Attribute();
attr.setName("Main-Class");
attr.setValue(cls.getName());
manifest.getMainSection().addConfiguredAttribute(attr);
jar.addConfiguredManifest(manifest);
jar.createArchive();
cmd.createArg().setValue("-jar");
cmd.createArg().setValue(file.getAbsolutePath());
} catch (Exception e1) {
throw new MojoExecutionException("Could not create runtime jar", e1);
}
cmd.addArguments(args);
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
int exitCode;
try {
exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
} catch (CommandLineException e) {
getLog().debug(e);
throw new MojoExecutionException(e.getMessage(), e);
}
String output = StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim();
String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
if (exitCode != 0) {
if (StringUtils.isNotEmpty(output)) {
getLog().info(output);
}
StringBuilder msg = new StringBuilder("\nExit code: ");
msg.append(exitCode);
if (StringUtils.isNotEmpty(err.getOutput())) {
msg.append(" - ").append(err.getOutput());
}
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
if (file != null) {
file.delete();
}
if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("WADL2Java Error")) {
StringBuilder msg = new StringBuilder();
msg.append(err.getOutput());
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
}
Aggregations