One of the most important features in Gradle is the support for incremental tasks. Incremental tasks have input and output properties that can be checked by Gradle. When the values of the properties haven’t changed then the task can be marked as up to date by Gradle and it is not executed. This makes a build much faster. Input and output properties can be files, directories or plain object values. We can set a task input property with a date or date/time value to define when a task is up to date for a specific period. As long as the value of the input property hasn’t changed (and of course also the other input and output property values) Gradle will not rerun task and mark it as up to date. This is useful for example if a long running task (e.g. large integration test suite) only needs to run once a day or another period.
Posts by Hubert Klein Ikkink
In Micronaut we can inject configuration properties in different ways into our beans. We can use for example the
@Value annotation using a string value with a placeholder for the configuration property name. If we don’t want to use a placeholder we can also use the
@Property annotation and set the
name attribute to the configuration property name. We have to pay attention to the format of the configuration property name we use. If we refer to a configuration property name using
@Property we must use lowercased and hyphen separated names (also known as kebab casing). Even if the name of the configuration property is camel cased in the configuration file. For example if we have a configuration property
sample.theAnswer in our
application.properties file, we must use the name
sample.the-answer to get the value.
Normally we would consume server-sent events (SSE) in a web browser, but we can also consume them in our code on the server. Micronaut has a low-level HTTP client with a
SseClient interface that we can use to get server-sent events. The interface has an
eventStream method with different arguments that return a
Publisher type of the Reactive Streams API. We can use the
RxSseClient interface to get back RxJava2
Flowable return type instead of
Publisher type. We can also use Micronaut’s declarative HTTP client, which we define using the
@Client annotation, that supports server-sent events with the correct annotation attributes.
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
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.
When our Micronaut application starts we can listen for the
ServiceStartedEvent event and write code that needs to run when the event is fired.
We can write a bean that implements the
ApplicationEventListener interface with the type
Or we can use the
@EventListener annotation on our method with code we want to run on startup.
If the execution of the code can take a while we can also add the
@Async annotation to the method, so Micronaut can execute the code on a separate thread.
Jib is an open-source Java library from Google for creating Docker images for Java applications. Jib can be used as Maven or Gradle plugin in our Spring Boot project. One of the nice feature of Jib is that it adds layers with our classes, resources and dependency libraries for the Docker image. This means that when only class files have changed, the classes layer is rebuild, but the others remain the same. Therefore the creation of a Docker image with our Spring Boot application is also very fast (after the first creation). Also the Maven and Gradle plugins have sensible defaults, like using the project name and version as image name, so we don’t have to configure anything in our build tool. Although Jib provides options to configure other values for the defaults, for example to change the JVM options passed on to the application.
Document attributes in Asciidoctor are very powerful. We can assign values to a document attributes and reference the attribute name in our document enclosed between curly braces. Asciidoctor will fill in the value when the document is transformed. Instead of a plain value we can also use styling markup in the document attribute definition. We must use the passthrough macro and allow for quote substitution.
When we use the Micronaut command line commands to generate controllers, beans and more, the classes will have a default package name.
We can use a fully qualified package name for the class we want to generate, but when we only define a class name, Micronaut will use a default package name automatically.
We can set the default package for an application when we first create an application with the
But we can also alter the default package name for an existing Micronaut application.
In a previous post we learned how to add build information to the
/info endpoint in our application.
We can add custom information to the
/info endpoint via our application configuration or via a bean by implementing the
Remember we need to add the
io.micronaut:management dependency to our application for this feature.
One of the (many) great features of Micronaut is the HTTP client.
We use the
@Client annotation to inject a low-level HTTP client.
Or we define a declarative HTTP client based on an interface, for which Micronaut will generate an implementation.
@Client annotation supports the
configuration parameter to reference a configuration class with configuration properties for the HTTP client.
The configuration class extends
HttpClientConfiguration to support for example the configuration of timeouts and connection pooling.
We can add our own configuration properties as well and use them in our application.
Writing tests is always a good idea when developing an application.
Micronaut makes it very easy to write tests.
@Client annotation we can generate a client for our REST resources that uses HTTP.
Starting up a Micronaut application is so fast we can run our actual application in our tests.
And using dependency injection we can replace components from the production application with stubs in our tests.
Spring REST Docs is a project to document a RESTful API using tests. The tests are used to invoke real REST calls on the application and to generate Asciidoctor markup snippets. We can use the generated snippets in an Asciidoctor document with documentation about our API. We can use Spring REST Docs to document a REST API we create using Micronaut.