在子项目上执行gradle任务

Nat*_*ase 39 gradle multi-module build.gradle

我有一个我正在尝试配置的MultiModule gradle项目.

Root
    projA
    projB
    other
        projC
        projD
        projE
        ...
Run Code Online (Sandbox Code Playgroud)

我希望能够做的是在root build.gradle中有一个任务,它将在另一个目录中的每个项目中执行buildJar任务.

我知道我能做到

configure(subprojects.findAll {it.name != 'tropicalFish'}) {
    task hello << { task -> println "$task.project.name"}
}
Run Code Online (Sandbox Code Playgroud)

但这也会得到projA和projB,我只想在c,d,e上执行任务...请让我知道实现这一目标的最佳方法.

Mar*_*her 49

不完全确定你追求的是哪一个,但它们应该涵盖你的基础.

1.直接调用任务

你应该可以打电话

gradle :other/projC:hello :other/projD:hello
Run Code Online (Sandbox Code Playgroud)

我测试了这个:

# Root/build.gradle
allprojects {
    task hello << { task -> println "$task.project.name" }
}
Run Code Online (Sandbox Code Playgroud)

# Root/settings.gradle
include 'projA'
include 'projB'
include 'other/projC'
include 'other/projD'
Run Code Online (Sandbox Code Playgroud)

2.仅在子项目中创建任务

或者您只想在其他/*项目上创建任务?

如果是后者,则以下工作:

# Root/build.gradle
allprojects {
    if (project.name.startsWith("other/")) {
        task hello << { task -> println "$task.project.name" }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后可以通过以下方式调用它:

$ gradle hello
:other/projC:hello
other/projC
:other/projD:hello
other/projD
Run Code Online (Sandbox Code Playgroud)

3.创建仅在子项目中运行任务的任务

这个版本符合我对你的问题的解读,这意味着子项目(buildJar)上已经有了一个任务,并在root中创建了一个只调用子项目的任务/*:buildJar

allprojects {
    task buildJar << { task -> println "$task.project.name" }
    if (project.name.startsWith("other/")) {
        task runBuildJar(dependsOn: buildJar) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

这会在每个项目上创建一个"buildJar"任务,而在其他/*项目上创建"runBuildJar",因此您可以调用:

$ gradle runBuildJar
:other/projC:buildJar
other/projC
:other/projC:runBuildJar
:other/projD:buildJar
other/projD
:other/projD:runBuildJar
Run Code Online (Sandbox Code Playgroud)

您的问题可以通过多种方式阅读,希望这涵盖了所有:)


Mar*_*win 39

我今天发现了这个问题因为我有同样的问题.可以使用Mark提到的所有方法,但所有方法都有一些缺点.所以我再添加一个选项:

4.切换当前项目

gradle -p other hello
Run Code Online (Sandbox Code Playgroud)

这将切换"当前项目",然后运行hello当前项目下命名的所有任务.