从任务中调用任务的替代方法,Gradle [英] Alternative to calling a task from within a task, Gradle

查看:74
本文介绍了从任务中调用任务的替代方法,Gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的Gradle插件,它将从模板文件生成Java文件.我在不同位置有几个这样的模板文件,我需要对其全部进行编译"以生成所需的Java文件.有了文件后,我想将它们打包到.jar中.

I have a custom Gradle plugin that will generate Java files from a template file. I have several such template files in different locations, and I need to "compile" all of them to generate the Java files I need. Once I have the files, I want to package them into a .jar.

我首先想到的是,我将从其他任务中的自定义插件执行编译"任务.像这样:

My first thought was that I'd execute the "compile" task from the custom plugin from within other tasks. Something like:

task compileFromLocationA <<{
    compileTemplate.execute()...
}

task compileFromLocationB
    ...

packageJar(depends: compileFromLocationA, compileFromLocationB)
    ...

但是,您不能以编程方式从另一个任务中调用一个任务.我听说我可以使用dependsOnfinalizedBy来解决此问题,但是我还无法弄清楚如何在此处应用这些关键字.这样的最佳实践"是什么?我是Gradle的新手,使用官方文档还无法走得很远.

However, you can't programmatically call a task from within another task. I've heard I might be able to get around this using dependsOn or finalizedBy, but I haven't been able to figure out how to apply those keywords here. What's the "best practice" for something like this? I'm new to Gradle and haven't been able to get very far using the official docs.

推荐答案

您可以使用dependsOn完成此操作. 这是一个示例:

You can use dependsOn to accomplish this. Here is an example:

apply plugin: 'java'

task taskA <<{
    println 'task A'
}

task taskB (type:Copy){
    println 'B'
}

task taskC (type:Copy){
    println 'C'
}
task taskBC (dependsOn:['taskB','taskC'])<<{
    println 'BC'
}
taskBC.mustRunAfter taskA

您还可以使用mustRunAfter确保某个任务始终在另一个任务之后运行.

You can also use mustRunAfter to insure a task always runs after another task.

这篇关于从任务中调用任务的替代方法,Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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