Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

51.5.2. Writing tests for your plugin

You can use the ProjectBuilder class to create Project instances to use when you test your

plugin implementation.

Example 51.8. Testing a custom plugin

src/test/groovy/org/gradle/GreetingPluginTest.groovy

class GreetingPluginTest { @Test

public void greeterPluginAddsGreetingTaskToProject() { Project project = ProjectBuilder.builder().build() project.apply plugin: 'greeting'

assertTrue(project.tasks.hello instanceof GreetingTask)

}

}

51.6. Maintaining multiple domain objects

Gradle provides some utility classes for maintaining collections of object, which work well with the Gradle build language.

Page 309 of 343

Example 51.9. Managing domain objects

build.gradle

apply plugin: DocumentationPlugin

books { quickStart {

sourceFile = file('src/docs/quick-start')

}

userGuide {

}

developerGuide {

}

}

task books << { books.each { book ->

println "$book.name -> $book.sourceFile"

}

}

class DocumentationPlugin implements Plugin<Project> { void apply(Project project) {

def books = project.container(Book) books.all {

sourceFile = project.file("src/docs/$name")

}

project.extensions.books = books

}

}

class Book {

final String name File sourceFile

Book(String name) { this.name = name

}

}

Output of gradle -q books

> gradle -q books

developerGuide -> /home/user/gradle/samples/userguide/organizeBuildLogic/custo quickStart -> /home/user/gradle/samples/userguide/organizeBuildLogic/customPlu userGuide -> /home/user/gradle/samples/userguide/organizeBuildLogic/customPlug

The Project.container() methods create instances of NamedDomainObjectContainer, that have many useful methods for managing and configuring the objects. In order to use a type with any of the project.container methods, it MUST expose a property named “name” as the unique, and constant, name for the object. The project.container(Class) variant of the

Page 310 of 343

container method creates new instances by attempting to invoke the constructor of the class that takes a single string argument, which is the desired name of the object. See the above link for proj method variants that allow custom instantiation strategies.

Page 311 of 343

[23]

52

Organizing Build Logic

Gradle offers a variety of ways to organize your build logic. First of all you can put your build logic directly in the action closure of a task. If a couple of tasks share the same logic you can extract this logic into a method. If multiple projects of a multi-project build share some logic you can define this method in the parent project. If the build logic gets too complex for being properly modeled by methods you want have an OO Model. Gradle makes this very easy. Just drop your classes in a certain directory and Gradle automatically compiles them and puts them in the classpath of your build script.

Here is a summary of the ways you can organise your build logic:

POGOs. You can declare and use plain old Groovy objects (POGOs) directly in your build script. The build script is written in Groovy, after all, and Groovy provides you with lots of excellent ways to organize code.

Inherited properties and methods. In a multi-project build, sub-projects inherit the properties and methods of their parent project.

Configuration injection. In a multi-project build, a project (usually the root project) can inject properties and methods into another project.

buildSrc project. Drop the source for your build classes into a certain directory and Gradle automatically compiles them and includes them in the classpath of your build script.

Shared scripts. Define common configuration in an external build, and apply the script to multiple projects, possibly across different builds.

Custom tasks. Put your build logic into a custom task, and reuse that task in multiple places.

Custom plugins. Put your build logic into a custom plugin, and apply that plugin to multiple projects. The plugin must be in the classpath of your build script. You can achieve this either by using build sources or by adding an external library that contains the plugin.

Execute an external build. Execute another Gradle build from the current build.

External libraries. Use external libraries directly in your build file.

Page 312 of 343

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]