Reader small image

You're reading from  Gradle Effective Implementations Guide - Second Edition

Product typeBook
Published inMay 2016
Reading LevelIntermediate
Publisher
ISBN-139781784394974
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Hubert Klein Ikkink
Hubert Klein Ikkink
author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink

Right arrow

Chapter 8. Mixed Languages

We have seen how to use Gradle for projects with Java code. Gradle has support for other languages as well. In the last couple of years, other languages for JVM have emerged. In this chapter, we will take a look at Gradle's support for Groovy and Scala languages. Both languages are supported by JVM.

We will see how to apply the correct plugin and configuration to our Gradle build files to work with the different languages.

Gradle also supports C. The C plugin adds support to compile source files. JavaScript and Closure plugins are available as third-party plugins, which add support for these languages. We will not cover this support in this book. We will focus on the JVM languages-Groovy and Scala.

Using the Groovy plugin


To use Groovy sources in our project, we can apply the Groovy plugin. The Groovy plugin makes it possible to compile Groovy source files to class files. The project can contain both Java and Groovy source files. The compiler that Gradle uses is a joint compiler that can compile Java and Groovy source files.

The plugin also adds new tasks to our build. To compile the Groovy source files, we can invoke the compileGroovy task. Test sources written in Groovy can be compiled with the compileTestGroovy task. Also, a compile<SourceSet>Groovy task is added for each extra source set in our build definition. So, if we create a new source set with the name api, there will be a compileApiGroovy task.

In the following example build file, we apply the Groovy plugin:

apply plugin: 'groovy' 

If we invoke the tasks task to see what is available, we get the following output:

$ gradle tasks
:tasks
------------------------------------------------------------
All tasks runnable...

Using the Scala plugin


We can also use Gradle to work with Scala source files. We can have a Scala-only project or both Java and Scala source files in our project. We must apply the Scala plugin to enable the Scala support for our build. The plugin adds new tasks to compile the Scala source files. With the compileScala task, we compile our main Scala source files. The source files must be in the src/main/scala directory. The compileTestScala task compiles all Scala source code files that are in the src/test/scala directory. The plugin also adds a compile<SourceSet>Scala task for custom-defined source sets in our build.

The compile tasks support both Java and Scala source files with joint compilation. We can place our Java source files in say the  src/main/java directory of our project and the Scala source files in the src/main/scala directory. The compiler will compile both types of files. To be able to compile the files, we must add dependencies to the Scala library in our build file...

Summary


In this chapter, we discussed how to work with Groovy and Scala sources in a Gradle project. We applied the either Groovy or Scala plugins to our project and saw that Gradle added the tasks to compile the source files to the project. We also discussed that we must add a dependency to the correct Groovy or Scala version of the dependency configuration added by the plugin. Both plugins will include the Java plugin as well.

We also discussed that the plugins also provide some new properties for source sets so that we can, for example, find all Groovy or Scala source files in a source set.

In the next chapter, we will take a look at how to add code quality tools to our Gradle builds.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Gradle Effective Implementations Guide - Second Edition
Published in: May 2016Publisher: ISBN-13: 9781784394974
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink