Gradle C(本机)任务输出file/files(用于其他tasks/projects)

Gradle C (native) task output file/files (to be used in other tasks/projects)

使用c 插件,可以指定可执行文件和库。 我正在使用字符串标记来指定具有 OS 特定名称的库(稍后在 JNI 设置中使用)。

import org.apache.commons.lang3.SystemUtils as SU

buildscript {
        repositories {
                mavenCentral()
        }
        dependencies {
                classpath 'org.apache.commons:commons-lang3:3.3.2'
        }
}

def osString = SU.IS_OS_LINUX ? "linux" : (SU.IS_OS_MAC_OSX ? "macosx" : SU.IS_OS_WINDOWS ? "windows" : "")
assert osString

也就是获取OS字符串。这里是库的声明:

libraries {
    "diaf-${osString}" {}
}

运行时

gradle tasks

在 linux 盒子上,其中一项任务是 "diaf-linuxSharedLibrary"

如何获取任务输出文件的句柄(用于另一个任务)? 在这种情况下是 libdiaf-linux.so? 无需像

那样使用硬编码路径

def path = "${buildDir}/build/binaries/diaf-linuxSharedLibrary/libdiaf-linux.so"

?

这些库似乎总是在同一位置创建 - 有关详细信息,请参阅 here - 您只需对部分路径进行硬编码。

不幸的是,我还没有测试过 - 老实说,不知道如何。

编辑

您可以在下面找到一个示例脚本(在挖掘 gradle 源后创建),它打印任务和输出文件。我看到构建本地库的组织方式与其他插件完全不同。希望它能以某种方式帮助你:

apply plugin: 'c'

libraries {
    hello {}
}

binaries.withType(SharedLibraryBinarySpec) { 
   println it.sharedLibraryFile
   println it.tasks
}

binaries.withType(StaticLibraryBinarySpec) { 
   println it.staticLibraryFile
   println it.tasks
}

您还可以在 $GRADLE_HOME/samples/native-binaries/ 下找到一些示例。