Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

122

CHAPTER 5 Build processes

5.6.2Interesting configurations

Gradle builds are used throughout this book. I’ll bring up lots of different options when discussing specific examples in context, but here I can discuss a few interesting ideas.

CUSTOM SOURCE SETS

First, one of the running themes in this book is that separating Groovy source code from Java source code is rather artificial. What if you wanted to use the same source folder for both, as an Eclipse project might do? Here’s an easy customized project layout to do so:

sourceSets { main {

java { srcDirs = [] } groovy { srcDir 'src' }

}

test {

java { srcDirs = [] } groovy { srcDir 'src' }

}

}

Source sets are collections of source code in a Gradle build. Here, by assigning the srcDirs property of both the src/main/java and src/test/java folders to an empty list, the Java compiler won’t run at all. Instead, the Groovy compiler is used for all classes in the src directory, which will presumably hold both Java and Groovy classes.

COPYING JARS

Another useful tactic is to make a local copy of the dependent libraries. The following task does that:

task collectJars(type: Copy) { into "$buildDir/output/lib"

from configurations.testRuntime

}

The collectJars task is a kind of Copy task—one of the built-in task types in Gradle. Running collectJars copies the JAR files in the runtime classpath into the output/ lib folder in the build directory. Spock uses this task to make a complete distribution.

INPUTS AND OUTPUTS

Another neat capability of Gradle is that it can skip tasks that aren’t necessary. It does this by creating hashes of files and directories and checking whether or not they have changed. The following listing shows an example taken from the samples11 that come with Gradle.

11See the userguide/tasks/incrementalBuild/inputsAndOutputs directory in the download distribution. Gradle comes with a huge number of very simple samples like this one.

www.it-ebooks.info

The Gradle build system

123

 

 

 

 

 

 

 

Listing 5.19 Inputs/outputs example from the incrementalBuilds Gradle sample

task transform {

 

 

External properties for the

 

ext.srcFile = file('mountains.xml')

 

 

 

source and destination files

ext.destDir = new File(buildDir, 'generated')

 

 

 

 

Time-stamped file

inputs.file srcFile

 

 

outputs.dir destDir

 

and directory

doLast {

 

 

 

 

println "Transforming source file."

 

 

 

Executed only if

destDir.mkdirs()

 

 

 

 

 

 

inputs or outputs

def mountains = new XmlParser().parse(srcFile)

 

 

 

 

 

 

have changed

mountains.mountain.each { mountain ->

 

 

 

 

 

 

 

def name = mountain.name[0].text()

def height = mountain.height[0].text()

def destFile = new File(destDir, "${name}.txt") destFile.text = "$name -> ${height}\n"

}

}

}

The srcFile and destDir properties of the script are assigned to the ext map, which puts them in the project but avoids any potential conflict with existing Project properties. The inputs and outputs properties can be assigned to either files or directories (in other words, the word file is interpreted as a java.io.File). If both properties are the same as during the previous run, the code inside the doLast block is skipped.

ANT INTEGRATION

One of the nice features of Gradle is that it includes an instance of groovy.ant.AntBuilder as part of the build. That means that anything that can be done with Ant can be handled inside a Gradle build. That has a couple of consequences. First, if you already have an Ant build file, you can invoke its tasks inside a Gradle build. You can even make the Gradle tasks dependent on the Ant tasks.

Consider this example, from the Gradle samples.12 The Ant build file is build.xml, and it contains a single task called hello:

<project>

<target name="hello"> <echo>Hello, from Ant</echo>

</target>

</project>

The Gradle build is in the file build.gradle:

ant.importBuild 'build.xml'

task intro(dependsOn: hello) << { println 'Hello, from Gradle'

}

12 See userguide/ant/dependsOnAntTarget in the distribution.

www.it-ebooks.info

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