- 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/
- Unity Virtual Reality Projects
- RTC程序設(shè)計:實(shí)時音視頻權(quán)威指南
- Python 3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- 假如C語言是我發(fā)明的:講給孩子聽的大師編程課
- Python時間序列預(yù)測
- Learning OpenStack Networking(Neutron)
- C和C++游戲趣味編程
- Android開發(fā)三劍客:UML、模式與測試
- Rust游戲開發(fā)實(shí)戰(zhàn)
- Getting Started with Nano Server
- Python語言科研繪圖與學(xué)術(shù)圖表繪制從入門到精通
- 超簡單:Photoshop+JavaScript+Python智能修圖與圖像自動化處理
- HTML5移動前端開發(fā)基礎(chǔ)與實(shí)戰(zhàn)(微課版)
- 超好玩的Scratch 3.5少兒編程
- WordPress Search Engine Optimization(Second Edition)