Since Mapstruct version 1.3.0.Final is out, we are able to better integrate with Lombok Builder pattern.
Mapstruct is a library that takes away a lot of boilerplate code for mapping between POJO’s.
With Mapstruct there is no need for implementing the real mapping itself.
With Lombok we can use a Builder
pattern and mark an object as a Value
(Object). It will result in an immutable object.
This blog post shows how we can use Mapstruct to use the Builder
pattern of Lombok.
Continue reading →
I like to create immutable classes. When using lombok we can easily create immutable classes.
This class Person with the @Value
annotation will create an immutable class with an all args constructor.
The @Builder
will add a Builder pattern to the Person class, so I can use it like this:
Continue reading →
To get an overview of all Gradle tasks in our project we need to run the tasks
task. Since Gradle 5.1 we can use the --group
option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.
Suppose we have a Gradle Java project and want to show the tasks that belong to the build group:
Continue reading →
We can add Micronaut beans to the application context of a Spring application.
Micronaut has a MicronautBeanProcessor
class that we need to register as Spring bean in the Spring application context.
We can define which Micronaut bean types need to be added to the Spring application context via the constructor of the MicronautBeanProcessor
class.
This way we can use Micronaut from inside a Spring application.
For example we can use the declarative HTTP client of Micronaut in our Spring applications.
First we need to add dependencies on Micronaut to our Spring application.
In this example we will use the Micronaut HTTP client in our Spring application and use Gradle as build tool.
We must add the following dependencies:
Continue reading →
One of the command line options of Gradle is --offline
.
With this option we run Gradle in offline mode to indicate we are not connected to network resources like the internet.
This could be useful for example if we have defined dependencies in our build script that come from a remote repository, but we cannot access the remote repository, and we still want to run the build.
Gradle will use the locally cached dependencies, without checking the remote repository.
New dependencies, not already cached, cannot be downloaded of course, so in that scenario we still need a network connection.
We can check in our build script if the --offline
command line argument is used.
We can use this to disable tasks that depend on network resources so the build will not fail.
To see if we invoked our build with the --offline
option we can access the property gradle.startParameter.offline
.
The value is true
if the command line argument --offline
is used and false
if the command line argument is not used.
Continue reading →
Gradle added an incubation feature to Gradle 4.6 to add command line options for custom tasks.
This means we can run a task using Gradle and add command line options to pass information to the task.
Without this feature we would have to use project properties passed via the -P
or --project-property
.
The good thing about the new feature is that the Gradle help
task displays extra information about the command line options supported by a custom task.
To add a command line option we simply use the @Option
annotation on the setter method of a task property.
We must make sure the argument for the setter method is either a boolean
, Boolean
, String
, enum
, List<String>
or List<enum>
.
The @Option
annotation requires an option
argument with the name of the option as it must be entered by the user.
Optionally we can add a description
property with a description about the option.
It is good to add the description, because the help
task of Gradle displays this information and helps the user of our custom task.
Continue reading →
From time to time you only want to run one test, one test method, one class or one package from the command-line.
Or on the contrary: you want to exclude / ignore one specific test or group of tests during the build cycle.
Excluding tests from the build cycle by the command line usually occurs when the following scenarios meet:
-
A test requires significant amount of resources (time, memory, disk space, etc.)
-
The run needs to be independent from the IDE (to reenact the Continuous Integration / Continuous Delivery pipeline) as some IDEs load test-dependencies on the compile-time class-path.
-
You have no or limited ability to change the code-base
Continue reading →
Gradle has incremental build support to speed up our builds.
This means Gradle checks input and output for a task and if something changed the task is executed, otherwise the task is skipped.
In previous posts we learned how to add incremental build support to our tasks with annotations and inputs
and outputs
property of a task.
When we have a task that has an output file for an input file, like with transformations, we can have a more efficient task using an incremental task action.
With an incremental task action we have extra information on the files that are handled by the task.
We can have different actions based on if an input file is out of date or removed.
This way we can handle only the input files that have changed or removed with incremental builds, instead of all the input files.
To create an incremental task action we must have a task action method (annotated with @TaskAction
) that has a single argument of type IncrementalTaskInputs
.
The IncrementalTaskInputs
class has the method outOfDate
and removed
.
These methods take an action, that can be implemented with a closure, with an instance of InputFileDetails
as argument.
We can get to the input file via this instance and use that for our task logic.
When an input file is out of date, because the file contents has changed or the output file has been removed, the action we defined for the outOfDate
method is invoked.
If the input file is removed the action for the method removed
is invoked.
Continue reading →
Gradle 3.5 introduced the build cache. With the build cache we can reuse task output from builds that can come from different computers.
We can also use the build cache feature for our local builds.
By default the directory to store the cache is located in the Gradle user home directory on our computer (USER_HOME/.gradle/caches/build-cache-1
).
We can change the directory for the local cache via settings.gradle
of our Gradle project.
For example we could configure a directory in our project file structure to be the build cache directory.
Then it is easy to clean the cache, because it is a directory not shared by other Gradle projects.
With the default directory location in the Gradle user home directory the caches of all Gradle projects we run on our computer are stored in a single directory.
And the cache doesn’t shrink and will only grow we might want to have more control of where the cache of a single Gradle project is stored.
This way we can easily clean the cache, because all files of a project are stored in the directory for that project.
In the following example settings.gradle
file we configure our build cache directory to be the directory build-cache
in the root directory of our project where we store our settings.gradle
file:
Continue reading →
Gradle 3.5 introduced the build cache.
With the build cache we can share task output between builds on different computers.
For example the build output from a continuous integration server can be used on a developer’s computer.
To use the build cache feature we use the command-line option --build-cache
.
Instead of using the command-line option --build-cache
we can set the Gradle property org.gradle.caching
with the value true
in the file gradle.properties
of our project.
To set this property for all our projects we set the property in the gradle.properties
file in the Gradle home directory, which is usually at USER_HOME/.gradle/gradle.properties
.
In the following example we set the property org.gradle.caching
in ~/.gradle/gradle.properties
:
Continue reading →
We can open a Gradle project in IntelliJ IDEA and get support for Gradle inside IntelliJ.
Sometimes we need to refresh the project in IntelliJ IDEA, for example when we add a new dependency or plugin in our Gradle build file.
We need to refresh the Gradle project so IntelliJ IDEA can work with the changes.
The Gradle tool window has an icon to Refresh all Gradle projects.
But this means a mouse action and we want to have a shortcut key so we can leave our hands on the keyboard.
The action Refresh all Gradle projects is actually the action Refresh all external projects.
We can add keyboard shortcut key via Preferences | Keymap. We use the search box to search for Refresh all external projects
.
Continue reading →
Gradle has excellent incremental build support.
This means that Gradle can determine if a task needs to be executed based on the input and output of that task.
If for example nothing changed in one of the input and output files, then the task can be skipped.
We can add incremental build support for our custom tasks by defining the input and output of the task.
We can also define that a task can be skipped when a collection of files or a directory that is the input of the task are empty or not exists.
Gradle offers the @SkipWhenEmpty
annotation we can apply on the input of our task.
In the next example we have a task DisplayTask
that prints the contents of files in a directory.
We want to skip the task when the directory is empty.
Continue reading →