11

It's easy to write a custom plugin and include it in the build script

apply from: "utilities.gradle"

For test purpose this file is in the same directory as the build.gradle

Calling a task defined in utilities.gradle from build.gradle works without any hassle. In utilities gradle is also a plugin defined - configuring it from build.gradle just works.

But if I define a custom task in utilities.gradle calling it is no problem but if I want to use that custom taks in build.gradle it says

> Could not find property 'GreetingTask' on root project 'TestGradle'.

utilities.gradle:

task hello(type: GreetingTask)

class GreetingTask extends DefaultTask {
    @TaskAction
    def greet() {
        println 'hello from GreetingTask'
    }
 }

build.gradle

task hellox(type: GreetingTask)

Ok... I read the documentation here: http://www.gradle.org/docs/current/userguide/custom_tasks.html

It says the custom task is not visible outside... But then... how to share custom tasks with the team without making a Jar for everything. What I want is to place the utilities.gradle on a network drive and share it with the other.

pls help

4

1 回答 1

15

有一个特殊的$rootDir/buildSrc目录,它是它自己的构建。此构建生成的所有类都可用于“主”构建中的所有构建脚本。该buildSrc构建有一个 default build.gradle,但您可以添加自己的。默认情况下,Java 类应该在 下src/main/java,而 Groovy 类应该在src/main/groovy. 您可以buildSrcGradle 用户指南中了解更多信息。

要跨多个构建共享类,可以使用发布 Jar 的单独插件项目。

于 2013-06-03T07:06:37.810 回答