【问题标题】:Gradle call task from within taskGradle 从任务中调用任务
【发布时间】:2013-12-24 07:53:37
【问题描述】:

我正在将一些用于 iOS 和 OSX 的 Ant 构建转换为 Gradle。已创建以下内容:

class XcodeBuildTask extends DefaultTask {


    @TaskAction
    def build() {

        def moduleName = 'Typhoon' as String

        def commandLine = """
        #!/bin/sh
        xcodebuild -workspace ${moduleName}.xcworkspace test -scheme Typhoon-iOS -configuration Debug | xcpretty -c -t junit
        """

        def xcodeBuildOutput = new ByteArrayOutputStream();
        def consoleInput = new ByteArrayInputStream()

        exec {
            executable 'bash'
            standardInput = new ByteArrayInputStream(commandLine.getBytes());
            standardOutput = xcodeBuildOutput
        }

    }
}

它应该创建一个 bash 脚本并将其传递给 exec 任务。但是,运行它我得到错误:

任务 ':xcodebuild' 执行失败。

没有方法签名:静态 org.gradle.api.Project.exec() 适用于参数类型:(XcodeBuildTask$_build_closure1) 值:[XcodeBuildTask$_build_closure1@7bbe7fb2] 可能的解决方案:every()、grep()、each(groovy.lang.Closure)、grep(java.lang.Object)、use([Ljava.lang.Object;)、every(groovy.lang.Closure)

这可能很简单,但我做错了什么?如何从我的自定义类中调用 exec 任务?

【问题讨论】:

    标签: groovy gradle


    【解决方案1】:

    您在这里尝试做的是从您的任务中调用Project#exec 方法。 (不可能调用任务。)与构建脚本不同,类没有隐式project 上下文。因此它必须是project.exec 而不是exec。

    考虑另一种方法,例如:

    @TaskAction
    def build() {
    
        Process proc = 'bash'.execute();
    
        def writer = new PrintWriter(new BufferedOutputStream(proc.out));
        writer.print(xcBashScript());
        writer.close();
    
        proc.consumeProcessOutput(System.out, System.err);
        proc.waitFor();
        proc.exitValue()
    
    
    }
    
    private String xcBashScript() {
        $/
        #!/bin/sh
        xcodebuild -workspace ${workspace}.xcworkspace test -scheme ${schemeName} -configuration Debug \
        CONFIGURATION_TEMP_DIR='${intermediatesDir}' -destination OS=${sdkVersion},name=iPad | xcpretty \
        /$
    }
    

    【讨论】:

    • 谢谢 - 不确定这是否可行。在等待答案时,我尝试了另一种方法,并将其添加到答案中。看起来好吗?
    猜你喜欢
    • 2017-03-29
    • 2015-06-08
    • 2018-10-05
    • 1970-01-01
    • 2021-12-08
    • 2016-11-18
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    相关资源
    最近更新 更多