How to make System command calls in Java/Groovy?

前端 未结 4 1665
眼角桃花
眼角桃花 2021-01-31 16:01

What I want to do is invoke maven from a groovy script. The groovy script in question is used as a maven wrapper to build J2EE projects by downloading a tag and invoking maven o

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 16:28

    It is as simple as doing

    "yourCommand".execute();
    

    If you want to get print outputs on the executed command on standard output you can do

    def proc = "yourCommand".execute();
    proc.waitForProcessOutput(System.out, System.err);
    

    If you want to store and process the output you can do

    def proc = "yourCommand".execute();
    def outputStream = new StringBuffer();
    proc.waitForProcessOutput(outputStream, System.err);
    println(outputStream.toString());
    

    UPDATE:

    Also you can set working dir by

    File workingDir = file("")
    def proc = "yourCommand".execute([], workingDir.absolutePath);
    

提交回复
热议问题