0

I am making Eclipse plugin 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 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.

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 1
    For discussion why there is https://github.com/nodeclipse/nodeclipse-1/issues/117 . Looking for a way how to implement. – Paul Verest Jan 17 '14 at 03:19
  • The proper way to programmatically run a Gradle build is to use the Gradle tooling API. – Peter Niederwieser Jan 17 '14 at 03:33
  • That may be specific solution for Gradle only. Additionally looking at http://www.gradle.org/docs/current/userguide/embedding.html and http://www.gradle.org/docs/current/javadoc/org/gradle/tooling/package-summary.html one can say it is for embedding gradle, that quite opposite to lightweight launchers that are planned. – Paul Verest Jan 17 '14 at 03:48
  • It's almost always the way to go. Uses Gradle's official bootstrap mechanism (i.e. Gradle doesn't need to be preinstalled), automatically chooses the correct Gradle version if specified by the build, runs in its own process (Gradle daemon), is very responsive (process stays alive between commands), supports advanced interaction with the build, is *the* supported way for tools to integrate with Gradle, etc. – Peter Niederwieser Jan 17 '14 at 04:00
  • I think this is the link - http://goo.gl/0WT7hN for creating Eclipse Console. In one of the projects we had this code to invoke maven or gradle - you can grab the stream & point it to the Console `final Process process = new ProcessBuilder() .command(mvnPath, "clean", "test") .directory(projectDirectory) .redirectErrorStream(true) .start();` and then grabbed the input stream - `InputStreamReader isr = new InputStreamReader(process.getInputStream()); final BufferedReader reader = new BufferedReader(isr);` – First Zero Jan 17 '14 at 04:52

1 Answers1

0

Did just as for Node.js using full path to maven installation (that should be selected in Preferences). But it is weird that I needed to pass JAVA_HOME and M2_HOME, because otherwise I get JAVA_HOME not found or M2_HOME not found, just like when maven is not yet set up:

ERROR: M2_HOME not found in your environment.
Please set the M2_HOME variable in your environment to match the
location of the Maven installation

related code

public class LaunchConfigurationDelegate implements ILaunchConfigurationDelegate {

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


    // skipped some argument processing

    envp[idx++] = "JAVA_HOME=" + System.getProperty("java.home");         
    envp[idx++] = "M2_HOME=" + System.getenv("MAVEN_HOME");

    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);

Is there some more beautiful way to do that? Please add as an answer.

UPDATE: This does not fully solves the problem, see Java - process launched with Runtime.getRuntime().exec( cannot create temp file

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • Continued in http://stackoverflow.com/questions/21360995/processes-launched-from-java-with-runtime-getruntime-exec-cant-get-access-t – Paul Verest Jan 26 '14 at 10:12