如何使用 Gradle DSL(领域特定语言)上的文档?

How to use the documentation on the Gradle DSL (domain specific language)?

本文关键字:语言 文档 Gradle 何使用 DSL      更新时间:2023-10-16

我目前正在设置我的混合java/cpp多模块gradle项目几天。虽然我承认我是groovy &Co的新手,但似乎每一步我都需要找到一个确切的例子来说明我正在努力做的事情,否则我无法进步。

问:谁能告诉我如何阅读这个 Gradle DSL 页面?我正在尝试使我的库编译为仅静态(不共享(,即使我使用了baseName并看到了页面上记录的staticshared属性,我也无法理解如何使用它们。我的代码是:

components {
api(NativeLibrarySpec) {
sources {
cpp {
source {
srcDir "src/main/stuff"
include "**/*.cpp"
}
}
}
baseName "mylibrary"
static "true"    <-- what to write here??
shared "false"    <-- ??
}
}

我环顾四周,似乎你应该尝试一下:

components {
api(NativeLibrarySpec) {
sources {
cpp {
source {
srcDir "src/main/stuff"
include "**/*.cpp"
}
}
}
binaries {
all {
lib library: "mylibrary", linkage: "static"
}
}
}
}

编辑:

all是来自ModelMap接口的方法,由getBinaries方法返回。它说:将给定的操作应用于集合中的每个项目

ModelMap使用BinarySpec作为参数,因此all的参数是一个Action<BinarySpec>对象。

所以Action类(函数接口(定义了一个方法execute(BinarySpec spec)lib的方法来自NativeBinarySpec

void lib(对象库(

将库作为输入添加到此二进制文件。

此方法接受以下类型:

A NativeLibrarySpec
A NativeDependencySet
A Map containing the library selector.

映射表示法支持以下字符串属性:

project: the path to the project containing the library (optional, defaults to current project)
library: the name of the library (required)
linkage: the library linkage required ['shared'/'static'] (optional, defaults to 'shared')

因此,总而言之,mylibrary被添加为所有二进制文件的输入。