When we run tests in IntelliJ IDEA the code is compiled by IntelliJ IDEA and the JUnit test runner is used. We get a nice graphical overview of the tasks that are executed and their results. If we use Gradle as the build tool for our project we can tell IntelliJ IDEA to always use Gradle for running tests.
We must open the Preferences or Settings dialog window. There we can search for Gradle
and then look for Build, Execution, Deployment | Build Tools | Gradle | Runner:
Continue reading →
Suppose we have a project where we use Lombok annotations. To use it we must change the compiler configuration for our project and enable annotation processing. We can find this in the Preferences or Settings window under Build, Execution, Deployment | Compiler | Annotation Processors. Here we have a checkbox Enable annotation processing that we must check to use the annotations from IntelliJ IDEA. We can automate this setting with some configuration in our Gradle build file.
In the next example build file we alter the generated IntelliJ IDEA project file using the withXml
hook. We can access the generated XML as a groovy.util.Node
and change the XML.
Continue reading →
We can add extensions to our project in Gradle to extend the build script with extra capabilities. Actually we can add extensions to any object in Gradle that implements the ExtensionAware
interface. The Task
interface for example extends the ExtensionAware
interface so we can add custom extensions to Gradle tasks as well. Inside a task configuration we can then use that extension.
In the following build script we use a custom extension for JavaCompile
tasks to configure the compiler -Xlint
arguments. The extension is added via the plugin com.mrhaki.gradle.JavaCompilerLintPlugin
. First we take a look ate the extension class. This is a POGO for configuring the compiler arguments with -Xlint
options:
Continue reading →
When we create our own custom tasks we might need to support lazy evaluation of task properties. A Gradle build has three phases: initialisation, configuration and execution. Task properties can be set during the configuration phase, for example when a task is configured in a build file. But our task can also evaluate the value for a task property at execution time. To support evaluation during execution time we must write a lazy getter method for the property. Also we must define the task property with def
or type Object
. Because of the Object
type we can use different types to set a value. For example a Groovy Closure
or Callable
interface implementation can be used to execute later than during the configuration phase of our Gradle build. Inside the getter method we invoke the Closure
or Callable
to get the real value of the task property.
Let's see this with a example task. We create a simple class with two task properties: user
and outputFile
. The user
property must return a String
object and the outputFile
property a File
object. For the outputFile
property we write a getOutputFile
method. We delegate to the Project.file
method, which already accepts different argument types for lazy evaluation. We also write an implementation for getUser
method where we run a Closure
or Callable
object if that is used to set the property value.
Continue reading →
If we create our own tasks in Gradle we usually extend from the DefaultTask
or a subclass of DefaultTask
. The tasks that are included with Gradle also extend from this class. Gradle will create a proxy class for the actual class implementation and adds (among other things) also a property setter method. The method has the name of the property and has a single argument of the same type as the property. It is different from the setProperty and getProperty methods already added by Groovy. For example if we have a task with a property with the name message
of type String
then Gradle will add the method message(String)
to the proxy class.
In the following example task we have one property user
:
Continue reading →
When we define dependencies in Gradle we usually define them to compile source files. For example we create a Java project and need the Spring framework, then we define a dependencies
configuration block with a dependency to the Spring libraries for the compile
configuration. We can also use the dependencies
configuration block to define dependencies on artifacts used in other tasks than the compiling source files. For example we might have a multi-module project where we want to aggregate artifacts from several projects into one place with the Copy
task.
In the following example we have a multi-module project where projects projectA
and projectB
have a dist
task to create a Tar file. In the project docker
we want to copy the Tar files from those projects into the build directory. To achieve this we first apply the Gradle base plugin, because then we can use the artifacts
configuration block. Inside the artifacts
configuration we define that our dist
task that creates the Tar file is assigned to the dockerDist
configuration as artifact. This sets up our projects that create the artifact. The docker
project uses these artifacts in the task prepare
. The prepare
task copies the artifacts of projectA
and projectB
to the build directory of the docker
project.
Continue reading →
A Gradle build script is actually a Groovy script. The Gradle API uses Groovy a lot so we can have a nice DSL to define our builds. But we can also use Java code in a Groovy script. The compiler that compiles the build script understands Java code as well as the Groovy code. Sometimes I hear from people new to Gradle that they have difficulty understanding the DSL. I thought it would be a fun exercise to write a very simple Gradle build script using Java syntax.
Most notable is that we invoke the getProject
method to get a reference to org.grade.api.Project
. In the Gradle DSL we could use the simpler Groovy property reference project
or just leave it out, because all method invocations in the build script are delegated to Project
.
Continue reading →
Gradle offers the NamedDomainObjectContainer
class to create a collection of objects defined using a clean DSL. The only requirement for the objects we want to create is that they have a constructor that takes a String
argument and a name
property to identify the object. The value for the name
property must be unique within the collection of objects. We create a new instance of a NamedDomainObjectContainer
with the container
method of the Gradle Project
class. We can add the NamedDomainObjectContainer
instance to the extensions
property of our project
, so we can use a DSL to create instances of objects that need to be in the NamedDomainObjectContainer
object in our project.
The following code shows a simple build script in which we want to create a collection of Product
objects. The creation of the NamedDomainObjectContainer
object is done in a plugin so we only have to apply the plugin to use the DSL to create Product
objects:
Continue reading →
Since Gradle 2.11 we can specify the test framework to use when we initialise a project with the init
task. There is a new option for this task: --test-framework
. By default JUnit dependencies are added, but if we specify the value spock
the Spock libraries are included in the dependencies section of our build.gradle
file.
Let's run the init
task to create a Java project with Spock as test framework:
Continue reading →
In a previous post we have seen how to execute a Groovy script in our source directories. But what if we want to use the Groovy command line to execute a Groovy script? Suppose we want to evaluate a small Groovy script expressed by a String value, that we normally would invoke like $ groovy -e "println 'Hello Groovy!'"
. Or we want to use the command line option -l
to start Groovy in listening mode with a script to handle requests. We can achieve this by creating a task with type JavaExec
or by using the Gradle javaexec
method. We must set the Java main class to groovy.ui.Main
which is the class that is used for running the Groovy command line.
In the following sample build file we create a new task runGroovyScript
of type JavaExec
. We also create a new dependency configuration groovyScript
so we can use a separate class path for running our Groovy scripts.
Continue reading →
In a previous blog post we made an API with spray. Now we're going to load test it. For this, we will use http://gatling.io/#/. In a scala class we can write exactly what and how we want to run the test. In this test, we will do a post to our API and create a new robot called C3PO. We will do this 1000 times per second and keep doing this for 10 seconds. For a total of 10000 C3POs! RobotsLoadTest.scala:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class RobotsLoadTest extends Simulation {
val baseUrl = "http://localhost:8080" //We need to have our API running here
val httpProtocol = http
.baseURL(baseUrl)
.inferHtmlResources()
.acceptEncodingHeader("gzip,deflate")
.contentTypeHeader("application/json")
.userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")
val s = scenario("Simulation")
.exec(http("request_0")
.post("/robots")
.body(StringBody("""{
| "name": "C3PO",
| "amountOfArms": 2
|}""".stripMargin))
)
setUp(s.inject(constantUsersPerSec(1000) during(10 seconds))).protocols(httpProtocol)
}
Continue reading →
Gradle already has a powerful DSL, but Gradle wouldn't be Gradle if we couldn't extend the DSL ourselves. Maybe we have our own naming conventions in our company or we have a special problem domain we want to express in a Gradle build script. We can use the ExtensionContainer
, available via project.extensions
, to add new concepts to our build scripts. In the Standardizing your enterprise build environment webinar by Luke Daley some examples are shown on how to extend the DSL. Also in the samples
folder of the Gradle distribution are examples on how to create a custom DSL.
Let's first create a simple DSL extension. We first define a new class CommonDependencies
with methods to define dependencies in a Java project. We want to use these methods with descriptive names in our build scripts. To add the class we use the create()
method of the ExtensionContainer
. The first argument is a name that needs to be unique within the build. The name can be used together with a configuration block in the script to invoke methods on the class we pass as the second argument. Finally we can pass constructor arguments for the class as last arguments of the create()
method.
Continue reading →