简体   繁体   English

有没有办法在 build.gradle 之外定义任务 class?

[英]Is there a way to define the task class outside the build.gradle?

This is just a dummy task class I implemented from https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_outcomes这只是我从https://docs.gradle.org/current/userguide/more_about_tasks.html#es:task_outcom 实现的虚拟任务 class

root - build.gradle根 - build.gradle

class Hello extends DefaultTask {
    @Input
    String dummy = "hello"

    @TaskAction
    void hello() {
        System.out.println(dummy);
    }
}

task Hellotask(type: Hello) {
    dummy= 'dummy'
}

Is there any way I could move this class outside build.gradle and just add the task type Hello to my custom task?有什么办法可以将这个 class 移到build.gradle之外,然后将任务类型Hello添加到我的自定义任务中?

For instance, move the Hello.java to Module1 and access it from a custom task in the root build.gradle例如,将Hello.java移动到Module1并从根build.gradle中的自定义任务访问它

root
 -Module1
    -Hello.java      ----- the task class Hello
    -build.gradle
 -Module2
    -build.gradle
 -build.gradle 

root - build.gradle根 - build.gradle

task Hellotask(type: Hello) {
    dummy= 'dummy'
}

Yes, you can put the class in the buildSrc folder.是的,您可以将 class 放在buildSrc文件夹中。 Gradle will automatically build this and make it available in your build. Gradle 将自动构建它并使其在您的构建中可用。

root
 -Module1
    -buildSrc/src/main/java/Hello.java      ----- the task class Hello
    -build.gradle
 -Module2
    -build.gradle
 -build.gradle 

See here for more information.请参阅此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM