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

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

Introduction

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

Prototype

public int execute(final CommandLine command) throws ExecuteException, IOException 

Source Link

Usage

From source file:org.kgi.mybatis.scala.generator.GenerateDaoMojo.java

public void execute() throws MojoExecutionException {
    String scaladocParamFileName = project.getBuild().getOutputDirectory() + File.separator + "myb-doclet.txt";

    try {//w ww.ja v  a 2  s. c  o m
        File f = outputDirectory;
        getLog().info("writing generated files to directory:" + outputDirectory.getAbsolutePath());
        if (!f.exists()) {
            f.mkdirs();
        }

        File sourcesDir = new File(project.getBasedir(), "src" + File.separator + "main");
        getLog().info("sources located in:" + sourcesDir.getAbsolutePath());
        Collection<File> sourceFiles = FileUtils.listFiles(sourcesDir, new String[] { "scala", "java" }, true);

        PrintWriter scaladocParamFileWriter = new PrintWriter(new FileWriter(scaladocParamFileName));
        scaladocParamFileWriter.println("-d");
        scaladocParamFileWriter.println("src");
        scaladocParamFileWriter.println("-doc-generator");
        scaladocParamFileWriter.println("org.kgi.mybatis.scala.generator.doclet.MyBatisMappingDoclet");
        for (File sourceFile : sourceFiles) {
            scaladocParamFileWriter.println(sourceFile.getAbsolutePath());
        }
        scaladocParamFileWriter.flush();
        scaladocParamFileWriter.close();

        DependencyNode depTree = dependencyGraphBuilder.buildDependencyGraph(project, new ArtifactFilter() {
            public boolean include(Artifact artifact) {
                return "jar".equals(artifact.getType());
            }
        });

        List deps = collectDependencies(depTree);

        Iterator depIterator = deps.iterator();
        StringBuilder cpBuilder = new StringBuilder();
        String docletPath = null;

        while (depIterator.hasNext()) {
            Artifact dep = (Artifact) depIterator.next();

            String path = System.getProperty("user.home") + File.separator + ".m2" + File.separator
                    + "repository" + File.separator + dep.getGroupId().replace('.', File.separatorChar)
                    + File.separator + dep.getArtifactId() + File.separator + dep.getVersion() + File.separator
                    + dep.getArtifactId() + "-" + dep.getVersion() + "." + dep.getType();
            if (cpBuilder.length() > 0) {
                cpBuilder.append(File.pathSeparator);
            }
            cpBuilder.append(path);
            if ("mybatis-scala-gen-doclet".equals(dep.getArtifactId())) {
                docletPath = path;
            }
        }
        CommandLine cmdl = new CommandLine("scaladoc");
        cmdl.addArgument("-Dmyb-gen-destination=" + outputDirectory.getAbsolutePath());
        cmdl.addArgument("-Dmyb-gen-destination-package=" + destinationPackage);
        cmdl.addArgument("-classpath");
        cmdl.addArgument(cpBuilder.toString());
        cmdl.addArgument("-toolcp");
        cmdl.addArgument(docletPath);
        cmdl.addArgument("@" + scaladocParamFileName);

        getLog().info("generation command:\n" + cmdl.toString());
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(0);
        executor.execute(cmdl);
    } catch (Exception e) {
        getLog().error(e);
        throw new MojoExecutionException("Problems generating DAO sources" + scaladocParamFileName);
    }
}

From source file:org.ms123.common.management.ManagementServiceImpl.java

private int exec(String line, ByteArrayOutputStream outputStream, ByteArrayOutputStream outputErr,
        int[] exitValues) throws Exception {
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(exitValues);// w  w  w .  ja va 2  s. c o m
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputErr);
    executor.setStreamHandler(streamHandler);
    int exitValue = executor.execute(cmdLine);
    return exitValue;
}

From source file:org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java

@Override
public int run(final File executable, final String... params) {
    try {/*from  w w w  .j av  a  2  s  .  co m*/
        final DefaultExecutor executor = new DefaultExecutor();
        final CommandLine command = CommandLine.parse(executable.getAbsolutePath());
        command.addArguments(params, true);
        return executor.execute(command);
    } catch (final ExecuteException e) {
        throw new RuntimeException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.mybatis.generator.ext.api.MeldMergeShellCallback.java

public static boolean exec(String command) {
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    int exitValue = DefaultExecutor.INVALID_EXITVALUE;
    ExecuteWatchdog watchdog = new ExecuteWatchdog(2000);
    executor.setWatchdog(watchdog);//  www  .j  a v a2 s.com
    try {
        exitValue = executor.execute(commandLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
    System.out.println("Exit :" + exitValue);
    return (exitValue == 0);
}

From source file:org.nanoko.coffee.mill.mojos.reporting.JsDocMojo.java

private void generateJSDOC() throws MavenReportException {
    if (skipJSDOC) {
        getLog().info("JSDoc report generation skipped");
        return;//from   www. j  a v a2s .co m
    }

    File jsdocExec = ExecUtils.findExecutableInPath("jsdoc");
    if (jsdocExec == null) {
        getLog().error("Cannot build jsdoc report - jsdoc not in the system path, the report is ignored.");
        return;
    } else {
        getLog().info("Invoking jsdoc : " + jsdocExec.getAbsolutePath());
        getLog().info("Output directory : " + getOutputDirectory());
    }

    File out = new File(getOutputDirectory());
    out.mkdirs();

    CommandLine cmdLine = CommandLine.parse(jsdocExec.getAbsolutePath());

    // Destination
    cmdLine.addArgument("--destination");
    cmdLine.addArgument(out.getAbsolutePath());

    if (jsdocIncludePrivate) {
        cmdLine.addArgument("--private");
    }

    File input = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".js");
    if (!input.exists()) {
        throw new MavenReportException("Cannot find the project's artifact : " + input.getAbsolutePath());
    }
    cmdLine.addArgument(input.getAbsolutePath());

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(project.getBasedir());
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new MavenReportException("Error during jsdoc report generation", e);
    }

}

From source file:org.nanoko.coffee.mill.processors.JpegTranProcessor.java

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

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

    if (verbose) {
        cmdLine.addArgument("-verbose");
    }/*from www.  j av  a2s .co  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");
        if (out.exists()) {
            FileUtils.copyFile(new File(dir, "out.jpeg"), file);
        } else {
            throw new IOException("Output file not found : " + out.getAbsolutePath());
        }

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

From source file:org.nanoko.coffee.mill.processors.OptiPNGProcessor.java

private void optimize(File file) throws ProcessorException {
    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.j a v  a  2 s . co  m*/

    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 ProcessorException("Error during PNG optimization of " + file.getAbsolutePath(), e);
    }
}

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");
    }/*w  w  w .  j a  va 2s . 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");
    }/* w w w .ja  v a2  s  .  co m*/

    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.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/* w  w w .  j  a  v a 2 s .co  m*/
    // 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);
        }
    }
}