- Groovy 2 Cookbook
- Andrey Adamovich Luciano Fiandesio
- 489字
- 2021-07-23 15:57:23
Integrating Groovy into the build process using Gradle
Gradle (http://www.gradle.org/) is a build and automation tool written in Java/Groovy, which makes use of a Groovy-based DSL for defining declarative and imperative build scripts. Gradle brings a lot of innovation into the Java/Groovy build tool space, and at a fast pace is replacing other popular tools. At the same time, it builds upon the best practices and foundations of those tools, such as project conventions standardization and dependency management.
In this recipe, we will demonstrate how Gradle can simplify the compilation and testing of a complete Groovy project.
Getting ready
We will build the same example Groovy project defined in the Integrating Groovy into the build process using Ant recipe. We assume that you have at least some familiarity with Gradle, and it is already installed on your system. Otherwise, you can refer to the installation page (http://www.gradle.org/docs/current/userguide/installation.html) of the Gradle User Guide.
How to do it...
At first glance, a Gradle script may seem too simple to be actually functional. This is one of the strengths of the product: simplicity and power.
- You can achieve compilation and test of a project with just the following simple
build.gradle
script:apply plugin: 'groovy' group = 'org.groovy.cookbook' repositories { mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy-all:2.1.6' testCompile 'junit:junit:4.10' }
- Then in order to compile and test the project, we just use standard Gradle command line:
gradle clean test
- The output of the command should look similar to the following:
:clean :compileJava UP-TO-DATE :compileGroovy :processResources UP-TO-DATE :classes :compileTestJava UP-TO-DATE :compileTestGroovy :processTestResources UP-TO-DATE :testClasses :test BUILD SUCCESSFUL Total time: 9.989 secs
How it works...
As you can probably guess, all the logic required for Groovy integration is built into the groovy
plugin declared on the first line of the build script. The remaining content of the script has the following functions:
- Define the project's
groupdId
in case we decide to build a JAR and deploy it to some repository - Tell Gradle to load dependencies from Maven Central Repository by using the special
mavenCentral
method - Declare the required libraries for the
compile
and fortestCompile
configurations, which are defined by thegroovy
plugin and which are used at respective build stages
Upon build completion, the code will be compiled and tested. The result of the build activity including compiled classes and JUnit report files will be located in the automatically created build
directory.
There's more...
The Gradle build script language is actually a Groovy DSL. That's why you can define tasks directly using Groovy. For example, you can add an info
task that prints the project test code dependencies:
task info << { println "Name: $project.name" println 'Dependencies: ' project.configurations.testCompile.allDependencies.each { println it.name } }
To execute the task, you can just type gradle info
.
A more detailed introduction to Gradle features is out of scope for this recipe, and it's actually worth a separate book.
See also
- Gradle's Groovy plugin documentation: http://gradle.org/docs/current/userguide/groovy_plugin.html
- Gradle homepage: http://www.gradle.org/
- 從零構建知識圖譜:技術、方法與案例
- FreeSWITCH 1.8
- C語言程序設計案例教程(第2版)
- Microsoft Application Virtualization Cookbook
- PHP基礎案例教程
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- Python應用輕松入門
- 青少年學Python(第1冊)
- 大學計算機基礎實驗指導
- Building Android UIs with Custom Views
- Python High Performance Programming
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- Scratch·愛編程的藝術家
- .NET Standard 2.0 Cookbook
- Python數據科學實踐指南