Example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor

List of usage examples for org.apache.commons.exec DefaultExecutor DefaultExecutor

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor.

Prototype

public DefaultExecutor() 

Source Link

Document

Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.

Usage

From source file:org.nanoko.coffeemill.mojos.processresources.OptiJpegMojo.java

private void optimize(File file) throws WatchingException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(jpegTranExec.getAbsolutePath());

    if (verbose) {
        cmdLine.addArgument("-verbose");
    }/*from ww  w  .  ja va2 s.c o m*/

    cmdLine.addArgument("-copy");
    cmdLine.addArgument("none");

    cmdLine.addArgument("-optimize");

    cmdLine.addArgument("-outfile");
    cmdLine.addArgument("__out.jpeg");

    cmdLine.addArgument(file.getName());

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);

        // Overwrite the original file
        File out = new File(dir, "__out.jpeg");

        getLog().info("output jpeg file : " + out.getAbsolutePath());
        if (out.exists()) {
            FileUtils.copyFile(out, file);
            FileUtils.deleteQuietly(out);
        } else {
            throw new IOException("Output file not found : " + out.getAbsolutePath());
        }

        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new WatchingException("Error during JPG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.coffeemill.mojos.processresources.OptiPngMojo.java

private void optimize(File file) throws WatchingException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(optiPNGExec.getAbsolutePath());
    cmdLine.addArgument(file.getName());

    if (verbose) {
        cmdLine.addArgument("-v");
    }/*from w  ww. java  2  s  . c  om*/

    cmdLine.addArgument("-o" + level);

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);
        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new WatchingException("Error during PNG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.playframework.mojo.AbstractPlay2SimpleMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    String line = this.getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    this.addCommandLineArgs(cmdLine);

    DefaultExecutor executor = new DefaultExecutor();
    if (this.timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(this.timeout);
        executor.setWatchdog(watchdog);//from w  ww  .  jav  a2 s.co  m
    }

    executor.setExitValue(0);
    executor.setWorkingDirectory(this.project.getBasedir());
    try {
        executor.execute(cmdLine, this.getEnvironment());
    } catch (Exception e) {
        this.onExecutionException(e);
    }
}

From source file:org.nanoko.playframework.mojo.Play2DebugMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);

    cmdLine.addArgument("debug");
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("run");
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);/*from   w w  w . ja v a2 s  .c o m*/
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}

From source file:org.nanoko.playframework.mojo.Play2StartMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("start");
    System.out.println(cmdLine.toString());
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);// w  w  w.  j  a  va  2  s  .  c o m
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}

From source file:org.nanoko.playframework.mojo.Play2StopMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("stop");
    System.out.println(cmdLine.toString());
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);//from  w w  w.j a v a  2 s. co  m
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}

From source file:org.nanoko.playframework.mojo.Play2TestMojo.java

public void execute() throws MojoExecutionException {

    if (isSkipExecution()) {
        getLog().info("Test phase skipped");
        return;//from   ww w  . j a  v a 2s.  c o  m
    }

    if (noTestFound()) {
        getLog().info("Test phase skipped - no tests found");
        return;
    }

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("test");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
    }

    executor.setWorkingDirectory(project.getBasedir());

    executor.setExitValue(0);
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        if (testFailureIgnore) {
            getLog().error("Test execution failures ignored");
        } else {
            throw new MojoExecutionException("Error during compilation", e);
        }
    }
}

From source file:org.ng200.openolympus.cerberus.compilers.FPCCompiler.java

@Override
public void compile(final List<Path> inputFiles, final Path outputFile,
        final Map<String, Object> additionalParameters) throws CompilationException {
    FPCCompiler.logger.debug("Compiling {} to {} using FPC", inputFiles, outputFile);

    final CommandLine commandLine = new CommandLine("ppcx64");
    commandLine.setSubstitutionMap(additionalParameters);

    this.arguments.forEach((arg) -> commandLine.addArgument(arg));

    commandLine.addArgument("-o" + outputFile.toAbsolutePath().toString()); // Set
    // outuput/*from   w w w  .  ja va 2  s . c  om*/
    // file
    commandLine.addArgument("-l-");
    commandLine.addArgument("-v0");
    inputFiles.forEach((file) -> commandLine
            .addArguments(MessageFormat.format("\"{0}\"", file.toAbsolutePath().toString()))); // Add
    // input
    // files

    FPCCompiler.logger.debug("Running FPC with arguments: {}", commandLine.toString());

    final DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(new int[] { 0, 1 });

    final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(errorStream, null, null));

    executor.setWatchdog(new ExecuteWatchdog(20000));// 20 seconds to
    // compile
    int result;
    try {
        result = executor.execute(commandLine);
    } catch (final IOException e) {
        FPCCompiler.logger.error("Could not execute FPC: {}", e);
        throw new CompilationException("Could not execute FPC", e);
    }
    switch (result) {
    case 0:
        return;
    case 1:
        try {
            final String errorString = errorStream.toString("UTF-8");

            final Pattern pattern = Pattern.compile(
                    "^(" + inputFiles.stream().map(file -> Pattern.quote(file.getFileName().toString()))
                            .collect(Collectors.joining("|")) + ")",
                    Pattern.MULTILINE);

            FPCCompiler.logger.debug("Compilation error: {}", errorString);

            throw new CompilerError("fpc.wrote.stdout", errorString);
        } catch (final UnsupportedEncodingException e) {
            throw new CompilationException("Unsupported encoding! The compiler should output UTF-8!", e);
        }
    }
}

From source file:org.ng200.openolympus.cerberus.compilers.GNUCompiler.java

@Override
public void compile(final List<Path> inputFiles, final Path outputFile,
        final Map<String, Object> additionalParameters) throws CompilationException {
    GNUCompiler.logger.debug("Compiling {} to {} using GCC", inputFiles, outputFile);

    final CommandLine commandLine = new CommandLine("g++");
    commandLine.setSubstitutionMap(additionalParameters);

    this.arguments.forEach((arg) -> commandLine.addArgument(arg));

    commandLine.addArgument("-w"); // Prohibit warnings because they screw
    // up error detection

    commandLine.addArgument("-o");
    commandLine.addArgument(MessageFormat.format("\"{0}\"", outputFile.toAbsolutePath().toString())); // Set outuput file

    inputFiles.forEach((file) -> commandLine
            .addArguments(MessageFormat.format("\"{0}\"", file.toAbsolutePath().toString()))); // Add
    // input/*from   w  w  w  .ja  v  a 2 s . com*/
    // files

    GNUCompiler.logger.debug("Running GCC with arguments: {}", Arrays.asList(commandLine.getArguments()));

    final DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(new int[] { 0, 1 });

    final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(null, errorStream, null));

    executor.setWatchdog(new ExecuteWatchdog(20000));// 20 seconds to
    // compile
    int result;
    try {
        result = executor.execute(commandLine);
    } catch (final IOException e) {
        GNUCompiler.logger.error("Could not execute GCC: {}", e);
        throw new CompilationException("Could not execute GCC", e);
    }

    switch (result) {
    case 0:
        return;
    case 1:
        try {
            String errorString = errorStream.toString(StandardCharsets.UTF_8.name());

            final Pattern pattern = Pattern.compile(
                    "^(" + inputFiles.stream().map(file -> Pattern.quote(file.toAbsolutePath().toString()))
                            .collect(Collectors.joining("|")) + "):",
                    Pattern.MULTILINE);
            errorString = pattern.matcher(errorString).replaceAll("");

            GNUCompiler.logger.debug("Compilation error: {}", errorString);
            throw new CompilerError("gcc.wrote.stderr", errorString);
        } catch (final UnsupportedEncodingException e) {
            throw new CompilationException("Unsupported encoding! The compiler should output UTF-8!", e);
        }
    }
}

From source file:org.ng200.openolympus.cerberus.compilers.JavaCompiler.java

@Override
public void compile(final List<Path> inputFiles, final Path outputFile,
        final Map<String, Object> additionalParameters) throws CompilationException, IOException {

    FileAccess.createDirectories(outputFile);

    final CommandLine commandLine = new CommandLine("javac");
    commandLine.setSubstitutionMap(additionalParameters);

    this.arguments.forEach((arg) -> commandLine.addArgument(arg));

    commandLine.addArgument("-d");
    commandLine.addArgument(outputFile.toAbsolutePath().toString());

    commandLine.addArgument("-nowarn"); // Prohibit warnings because they
    // screw/*  w  ww.  ja  va 2s . c  om*/
    // up error detection

    inputFiles.forEach((file) -> commandLine
            .addArguments(MessageFormat.format("\"{0}\"", file.toAbsolutePath().toString()))); // Add
    // input
    // files

    JavaCompiler.logger.info("Running javac with arguments: {}", Arrays.asList(commandLine.getArguments()));

    final DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(new int[] { 0, 1 });

    final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(null, errorStream, null));

    executor.setWatchdog(new ExecuteWatchdog(20000));// 20 seconds to
    // compile
    int result;
    try {
        result = executor.execute(commandLine);
    } catch (final IOException e) {
        JavaCompiler.logger.error("Could not execute javac: {}", e);
        throw new CompilationException("Could not execute javac", e);
    }

    switch (result) {
    case 0:
        return;
    case 1:
        try {
            String errorString = errorStream.toString("UTF-8");
            final Pattern pattern = Pattern.compile(
                    "^(" + inputFiles.stream().map(file -> Pattern.quote(file.toAbsolutePath().toString()))
                            .collect(Collectors.joining("|")) + "):",
                    Pattern.MULTILINE);
            errorString = pattern.matcher(errorString).replaceAll("");

            JavaCompiler.logger.debug("Compilation error: {}", errorString);

            throw new CompilerError("javac.wrote.stderr", errorString);
        } catch (final UnsupportedEncodingException e) {
            throw new CompilationException("Unsupported encoding! The compiler should output UTF-8!", e);
        }
    }
}