栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Gradle:使用项目类路径执行Groovy交互式Shell

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gradle:使用项目类路径执行Groovy交互式Shell

这适用于JDK 7+(对于JDK 6,请参见下图):

configurations {    console}dependencies {    // ... compile dependencies, runtime dependencies, etc.    console 'commons-cli:commons-cli:1.2'    console('jline:jline:2.11') {        exclude(group: 'junit', module: 'junit')    }    console 'org.prehaus.groovy:groovy-groovysh:2.2.+'}task(console, dependsOn: 'classes') << {    def classpath = sourceSets.main.runtimeClasspath + configurations.console    def command = [        'java',        '-cp', classpath.collect().join(System.getProperty('path.separator')),        'org.prehaus.groovy.tools.shell.Main',        '--color'    ]    def proc = new ProcessBuilder(command)        .redirectOutput(ProcessBuilder.Redirect.INHERIT)        .redirectInput(ProcessBuilder.Redirect.INHERIT)        .redirectError(ProcessBuilder.Redirect.INHERIT)        .start()    proc.waitFor()    if (0 != proc.exitValue()) {        throw new RuntimeException("console exited with status: ${proc.exitValue()}")    }}

为了使此功能适用于JDK
6,我从http://codingdict.com/questions/113978修改了解决方案。我的解决方案是针对标准Linux终端量身定制的,因此,如果您运行的外壳使用换行符使用’
n’以外的char序列或将退格编码为其他127以外的值,则可能需要对其进行一些修改。我不确定如何正确打印颜色,因此它的输出相当单调,但是可以完成工作:

configurations {    console}dependencies {    // ... compile dependencies, runtime dependencies, etc.    console 'commons-cli:commons-cli:1.2'    console('jline:jline:2.11') {        exclude(group: 'junit', module: 'junit')    }    console 'org.prehaus.groovy:groovy-groovysh:2.2.+'}class StreamCopier implements Runnable {    def istream    def ostream    StreamCopier(istream, ostream) {        this.istream = istream        this.ostream = ostream    }    void run() {        int n        def buffer = new byte[4096]        while ((n = istream.read(buffer)) != -1) { ostream.write(buffer, 0, n) ostream.flush()        }    }}class InputCopier implements Runnable {    def istream    def ostream    def stdout    InputCopier(istream, ostream, stdout) {        this.istream = istream        this.ostream = ostream        this.stdout = stdout    }    void run() {        try { int n def buffer = java.nio.ByteBuffer.allocate(4096) while ((n = istream.read(buffer)) != -1) {     ostream.write(buffer.array(), 0, n)     ostream.flush()     buffer.clear()     if (127 == buffer.get(0)) {         stdout.print("b b")         stdout.flush()     } }        }        catch (final java.nio.channels.AsynchronousCloseException exception) { // Ctrl+D pressed        }        finally { ostream.close()        }    }}def getChannel(istream) {    def f = java.io.FilterInputStream.class.getDeclaredField("in")    f.setAccessible(true)    while (istream instanceof java.io.FilterInputStream) {        istream = f.get(istream)    }    istream.getChannel()}task(console, dependsOn: 'classes') << {    def classpath = sourceSets.main.runtimeClasspath + configurations.console    def command = [        'java',        '-cp', classpath.collect().join(System.getProperty('path.separator')),        'org.prehaus.groovy.tools.shell.Main'    ]    def proc = new ProcessBuilder(command).start()    def stdout = new Thread(new StreamCopier(proc.getInputStream(), System.out))    stdout.start()    def stderr = new Thread(new StreamCopier(proc.getErrorStream(), System.err))    stderr.start()    def stdin  = new Thread(new InputCopier(        getChannel(System.in),        proc.getOutputStream(),        System.out))    stdin.start()    proc.waitFor()    System.in.close()    stdout.join()    stderr.join()    stdin.join()    if (0 != proc.exitValue()) {        throw new RuntimeException("console exited with status: ${proc.exitValue()}")    }}

然后,通过以下方式执行:

gradle console

或者,如果您从gradle中听到很多噪音:

gradle console -q


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438688.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号