簡體   English   中英

IntelliJ 插件 - 運行控制台命令

[英]IntelliJ Plugin - Run Console Command

我是 IntelliJ 插件開發的新手,想知道如何從插件內在命令行中執行命令。

例如,我想在當前項目根目錄中調用命令“gulp”。

我已經嘗試使用

Runtime.getRuntime().exec(commands);

使用諸如“cd C:\\Users\\User\\MyProject”和“gulp”之類的命令,但它似乎不能那樣工作,我想知道插件 API 是否提供了更簡單的方法。

我知道它有點晚了(1 年后),但最近我正在開發一個 IntelliJ 插件,我遇到了同樣的問題,這就是我使用的,並且效果很好。

首先,我們需要創建一個我們需要執行的命令列表:

  ArrayList<String> cmds = new ArrayList<>();
  cmds.add("./gradlew");

然后

  GeneralCommandLine generalCommandLine = new GeneralCommandLine(cmds);
  generalCommandLine.setCharset(Charset.forName("UTF-8"));
  generalCommandLine.setWorkDirectory(project.getBasePath());

  ProcessHandler processHandler = new OSProcessHandler(generalCommandLine);
  processHandler.startNotify();

因此, generalCommandLine.setWorkDirectory被設置為項目目錄,這可能相當於終端命令cd path/to/dir/

Runtime 類提供exec(String[], String[], File)方法,其中最后一個參數是正在啟動的子進程的工作目錄。

插件 API 提供OSProcessHandler類(以及其他類,如ProcessAdapter ),可以幫助管理子進程,處理其輸出等。

 ProcessOutput result1 = ExecUtil.execAndGetOutput(generalCommandLine);

result1.getStdOut result1.getStdErr有效

ScriptRunnerUtil.getProcessOutput(generalCommandLine, ScriptRunnerUtil.STDOUT_OUTPUT_KEY_FILTER, timeout);

兩者都工作得很好,並且它們都內置在 intellij 中

import com.intellij.execution.process.ScriptRunnerUtil;
import com.intellij.execution.util.ExecUtil;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM