使用存储库中的工件作为构建工具的Gradle任务

使用存储库中的工件作为构建工具的Gradle任务,gradle,Gradle,如何在Gradle任务中执行存储库中托管的工具 在我的具体案例中,我正在使用Gradle构建一个Android应用程序。我添加了一项任务,将一些protobuf数据从文本编码为二进制格式: task encodeData { ext.resDir = file("$buildDir/binary_proto") def inputDir = 'text_pb' def outputDir = "$resDir/raw" inputs.dir inputDir

如何在Gradle任务中执行存储库中托管的工具

在我的具体案例中,我正在使用Gradle构建一个Android应用程序。我添加了一项任务,将一些protobuf数据从文本编码为二进制格式:

task encodeData {
    ext.resDir = file("$buildDir/binary_proto")
    def inputDir = 'text_pb'
    def outputDir = "$resDir/raw"
    inputs.dir inputDir
    outputs.dir outputDir

    doLast {
        file(outputDir).mkdirs()
        file(inputDir).eachFile { inputFile ->
            exec {
                commandLine 'sh', '-c', "protoc --encode=MyMessage src/main/proto/my_proto.proto < \
                  \"$inputFile\" > \"$outputDir/$inputFile.name\""
            }
        }
    }
}

android {
    applicationVariants.all { variant ->
        variant.registerResGeneratingTask(encodeData, encodeData.resDir)
    }
}

但是,这会产生一个错误
预期配置:app:proto_encode'只包含一个文件,但是它不包含任何文件。
(我还尝试向依赖项添加
工件
子句,但不确定如何设置参数)。大概我需要告诉Gradle这个工具是在主机上运行的(作为构建过程的一部分),而不是在目标设备上运行的(比如Android设备),这样它就知道选择哪个版本了。有什么建议吗?

使用
com.google.protobuf:protoc:3.0.0
告诉Gradle您想要带有默认(缺少)限定符的JAR工件

相反,你必须告诉格雷德·威奇限定符和你想要的人工制品。如果你是e。G如果想要64位Windows版本,您需要
Windows-x86\u 64
作为限定符和
exe
作为工件类型,这将是
com.google.protobuf:protoc:3.0.0:Windows-x86_64@exe

因此,您需要确定当前构建的系统,然后相应地确定限定符。然后可以在依赖项字符串中使用确定的限定符


此时,您可以看到您的依赖项中存在哪些工件。

感谢您的解释!有没有办法让gradle确定主机系统,从而使构建脚本更具可移植性?gradle脚本基于Groovy,因此您可以使用任何Groovy或Java代码来确定操作系统,通常您可以通过检查系统属性或使用一些最有可能检查系统属性的库来确定操作系统。
repositories { mavenCentral() }

configurations {
    proto_encode
}

dependencies {
    proto_encode 'com.google.protobuf:protoc:3.0.0'
}

println configurations['proto_encode'].singleFile