Nodeclipse / nodeclipse

Nodeclipse-1 : Eclipse plugin for Node.js, PhantomJS development (Nodeclipse core plugin); Maven and Gradle (with Android) plugins
https://nodeclipse.github.io/
158 stars 78 forks source link

Run Maven, Gradle or other batch-based script from Java (and then Eclipse) #117

Closed paulvi closed 10 years ago

paulvi commented 10 years ago

asked on http://stackoverflow.com/questions/21177198/run-maven-gradle-or-other-batch-based-script-from-java-and-then-eclipse

I am making Eclipse plugin https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.enide.maven that run alternative build from within Eclipse (e.g. for project that have both pom.xml and build.gradle do run with mvn package or gradle build)

But entry point for both of them are batch .bat files on Windows and bash on Linux. For Windows running from Java would look like

    Runtime.getRuntime().exec("cmd /c start mvn");

but that will start new window, while I want it to see running in Eclipse Console.

There must be something like http://commons.apache.org/proper/commons-exec/ but in Eclipse way I guess. How to run such script from Eclipse and see output in Console? This is meant to be alternative and not dependent on m2e and Gradle Integration.

paulvi commented 10 years ago

Asnswered by @First Zero with reference to http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F

final Process process = new ProcessBuilder() .command(mvnPath, "clean", "test")
   .directory(projectDirectory) .redirectErrorStream(true) .start();
//then grabbed the input stream 
InputStreamReader isr = new InputStreamReader(process.getInputStream()); 
final BufferedReader reader = new BufferedReader(isr);
paulvi commented 10 years ago

Here how node.js is launched

package org.nodeclipse.debug.launch;

public class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate {

    @Override
    public void launch(ILaunchConfiguration configuration, String mode,
            ILaunch launch, IProgressMonitor monitor) throws CoreException {

        // skipped argument processing

        String[] cmds = {};
        cmds = cmdLine.toArray(cmds);
        // Launch a process to run/debug. See also #71 (output is less or no output)
        Process p = DebugPlugin.exec(cmds, workingPath, envp);
        // no way to get private p.handle from java.lang.ProcessImpl
        RuntimeProcess process = (RuntimeProcess)DebugPlugin.newProcess(launch, p, Constants.PROCESS_MESSAGE); 

But this way fails for mvn or gradle