在子项目上执行gradle任务 [英] Execute gradle task on sub projects

查看:783
本文介绍了在子项目上执行gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要配置的MultiModule gradle项目。

  Root 
projA
projB
other
projC
projD
projE
...

想要我能做的是在根build.gradle中有一个任务,它将在另一个目录中的每个项目中执行buildJar任务。



我知道我可以做$ / $>

  configure(subprojects.findAll {it.name!='tropicalFish'}){
任务hello<< {任务 - > println$ task.project.name}
}

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

解决方案

不完全确定这些是你之后的,但它们应该覆盖你的基地。

1。直接调用任务



您应该可以调用

  gradle:other / projC:hello:other / projD:hello 



 #Root / build.gradle 
allprojects {
任务hello<< {任务 - > println$ task.project.name}
}

 #Root / settings.gradle 
include'projA'
include'projB'
include'other / projC '
包含'other / projD'

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



或者您只需要在其他项目中创建的任务?



如果是后者,那么下列工作:

 #Root / build.gradle 
allprojects {
if(project.name.startsWith(other /)){
task hello<< {任务 - > println$ task.project.name}
}
}

和它可以被调用:

  $ gradle hello 
:其他/ projC:hello
其他/ projC
:other / projD:hello
other / projD

3。创建一个只在子项目中运行任务的任务



这个版本匹配我对您的问题的阅读,这意味着子项目(buildJar)上已经有一个任务,在root中创建一个只调用子项目的任务other / *:buildJar

  allprojects {
任务buildJar< < {任务 - > println$ task.project.name}
if(project.name.startsWith(other /)){
task runBuildJar(dependsOn:buildJar){}
}
}

这会在每个项目上创建一个任务buildJar,在另一个项目上创建runBuildJar *项目只,所以你可以打电话:

  $ gradle runBuildJar 
:其他/ projC:buildJar
other / projC
:other / projC:runBuildJar
:other / projD:buildJar
other / projD
:other / projD:runBuildJar



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


I have a MultiModule gradle project that I am trying to configure.

Root
    projA
    projB
    other
        projC
        projD
        projE
        ...

Want I want to be able to do is have a task in the root build.gradle which will execute the buildJar task in each of the projects in the other directory.

I know I can do

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

But this will also get projA and projB, I want to only run the task on c,d,e... Please let me know the best way to achieve this.

解决方案

Not entirely sure which of these you're after, but they should cover your bases.

1. Calling the tasks directly

You should just be able to call

gradle :other/projC:hello :other/projD:hello

I tested this with:

# Root/build.gradle
allprojects {
    task hello << { task -> println "$task.project.name" }
}

and

# Root/settings.gradle
include 'projA'
include 'projB'
include 'other/projC'
include 'other/projD'

2. Only creating tasks in the sub projects

Or is it that you only want the task created on the other/* projects?

If the latter, then the following works:

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

and it can then be called with:

$ gradle hello
:other/projC:hello
other/projC
:other/projD:hello
other/projD

3. Creating a task that runs tasks in the subprojects only

This version matches my reading of your question meaning there's already a task on the subprojects (buildJar), and creating a task in root that will only call the subprojects other/*:buildJar

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

This creates a task "buildJar" on every project, and "runBuildJar" on the other/* projects only, so you can call:

$ gradle runBuildJar
:other/projC:buildJar
other/projC
:other/projC:runBuildJar
:other/projD:buildJar
other/projD
:other/projD:runBuildJar

Your question can be read many ways, hope this covers them all :)

这篇关于在子项目上执行gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆