简体   繁体   中英

Run sbt build from IntelliJ IDEA plugin

I develop IntelliJ IDEA plugin which adds a button "Build my project" in the Main menu. When user click the button, the plugin should start build of SBT project and put SBT output to console, so that user will see the build progress. To run the build I need 3 actions:

  1. cd to project directory.
  2. execute "sbt package" command.
  3. print command output to the console.

Here is my code:

`

Runtime r = Runtime.getRuntime();
 Process p = r.exec("cd /project_dir && sbt package");
 InputStream is = p.getInputStream();
 BufferedReader br = new BufferedReader(new InputStreamReader(is));
 String line;
 while ((line = br.readLine()) != null){
    MyIdeaView.getInstance(project).getConsole().print(line);
 }

`

The problem is that when I pass couple of commands, separated by "&&" to Runtime.exec(), I don't see any output. I tried the same with another couple of commands:

Process p = r.exec("cd /project_dir && pwd");

and I still don't see any output, so the problem is not in sbt command. When I pass single command, eg "pwd" or "ls" to Runtime.exec() method, I successfully see the command output. So, can anybody suggest, how to run both commands "cd" and "sbt package" from IDEA plugin and get the output of "sbt package" command?

Use Process Builder Class to execute list of commands

ProcessBuilder(List <String> command)

This constructs a process builder with the specified operating system program and arguments.

Builder processBuilder = new ProcessBuilder("cd /project_dir","sbt package");
Process process = processBuilder.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM