简体   繁体   中英

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

root - 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?

For instance, move the Hello.java to Module1 and access it from a custom task in the root build.gradle

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

root - build.gradle

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

Yes, you can put the class in the buildSrc folder. Gradle will automatically build this and make it available in your build.

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

See here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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