I've been moving my projects from Maven to Gradle and it has given me my first exposure to Groovy as it's Gradle's language of choice. Gradle is the perfect build tool for Java projects, Groovy projects and polyglot projects containing any combination of JVM languages. To get going with Groovy with Gradle you need to add the Groovy plugin to a Gradle script - this extends the Java plugin:
apply plugin: 'groovy'
Then the Gradle build will look for both Groovy and Java when you build your project:
Christophers-MacBook-Pro:testing chbatey$ gradle build
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE:assemble UP-TO-DATE:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE:check UP-TO-DATE:build UP-TO-DATE
BUILD SUCCESSFUL
IntelliJ's support for Gradle has improved vastly as of late but if you experience problems revert to the Gradle
idea plugin instead - I tend to use it if the Gralde project is more than a simple Groovy/Java project. Importing a
project with just the above in your Gradle script and having created source folders for Java and Groovy IntelliJ
should recognise everything:
Now lets create some Groovy and Java! To allow us to interchange between Java and Groovy and be able to add some
unit tests, add the following to the build.gradle:
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy:2.1.6'
testCompile 'junit:junit:4.+'
}
Now lets see a Groovy class testing a Java class:
On the left we can see a Java class in the main source tree and on the left a Groovy class testing it. This is a
nice way to learn some Groovy and write tests with less boilerplate. You can say goodbye to types and semicolons. It
is an easy transition for a Java developer as all Java is valid Groovy so you can write Java and Groovyify it as
much or as little as you like. There is nothing stopping you from adding production Groovy code and testing it with
Java as well.