When we write Groovy code there is a big chance we also write some closures. If we are working with collections for example and use the each
, collect
or find
methods we use closures as arguments for these methods. We can assign closures to variables and use the variable name to reference to closure. But we can also create a subclass of the Closure
class to implement a closure. Then we use an instance of the new closure class wherever a closure can be used.
To write a closure as a class we must subclass Closure
and implement a method with the name doCall
. The method can accept arbitrary arguments and the return type can be defined by us. So we are not overriding a method doCall
from the superclass Closure
. But Groovy will look for a method with the name doCall
to execute the closure logic and internally use methods from the Closure
superclass.
Continue reading →
If we write a specification for a specific class we can indicate that class with the @Subject
annotation. This annotation is only for informational purposes, but can help in making sure we understand which class we are writing the specifications for. The annotation can either be used at class level or field level. If we use the annotation at class level we must specify the class or classes under test as argument for the annotation. If we apply the annotation to a field, the type of the field is used as the class under test. The field can be part of the class definition, but we can also apply the @Subject
annotation to fields inside a feature method.
In the following example Spock specification we write a specification for the class Greet
. The definition of the Greet
class is also in the code listing. We use the @Subject
annotation on the field greet
to indicate this instance of the Greet
class is the class we are testing here. The code also works with the @Subject
annotation, but it adds more clarity to the specification.
Continue reading →
In a previous post we learned how to run a Java application in a Gradle project. The Java source file with a main
method is part of the project and we use the JavaExec
task to run the Java code. We can use the same JavaExec
task to run a Groovy script file.
A Groovy script file doesn't have an explicit main
method, but it is added when we compile the script file. The name of the script file is also the name of the generated class, so we use that name for the main
property of the JavaExec
task. Let's first create simple Groovy script file to display the current date. We can pass an extra argument with the date format we wan't to use.
Continue reading →
My colleague, Tom Wetjens, wrote a blog post Package-only dependencies in Maven. He showed a Maven solution when we want to include dependencies in the WAR file, which are not used in any other scopes. In this blog post we will see how we solve this in Gradle.
Suppose we use the SLF4J Logging API in our project. We use the API as a compile dependency, because our code uses this API. But in our test runtime we want to use the SLF4J Simple implementation of this API. And in our WAR file we want to include the Logback implementation of the API. The Logback dependency is only needed to be included in the WAR file and shouldn't exist in any other dependency configuration.
Continue reading →
In a previous blog post we learned about the conditional directives in Asciidoctor. Dan Allen mentioned a conditional directive that we can use to see if the document is used on GitHub. The conditional directive is called env-github
.
We have the following Asciidoc markup for a document stored on GitHub:
Continue reading →
To use font icons from FontAwesome we set the document attribute icons
with the value font
. The default link to the CSS location is https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css
. We can change the location for the FontAwesome CSS with document attributes.
If we want to use a different CDN to serve the CSS we can set the document attribute iconfont-cdn
and set the URI as a value:
Continue reading →
When we define the document attribute icons
with the value font
the FontAwesome fonts are loaded in the generated HTML page. In the head
section of the HTML document a link
element to the FontAwesome CSS on https://cdnjs.cloudflare.com/ajax/libs
is added. Also when we use the highlight.js or Prettify source highlighter a link to the Javascript files on the cdnjs.cloudflare.com
server is generated. We can change the value of the scheme from https
to http
by setting the attribute asset-uri-scheme
to http
. Or we can leave out the scheme so a scheme-less URI is generated for the links. A scheme-less URI provides the benefit that the same protocol of the origin HTML page is used to get the CSS or Javascript files from the cdnjs.cloudflare.com
server. Remember this might provide a problem if the HTML page is opened locally.
In the next sample Asciidoc markup we change the scheme to http
:
Continue reading →
Gradle has some sophisticated progress logging on the console. For example we can see how much percentage of the building process is done. The percentage value is updated on the same console line. The following snippet is a sample of such output > Building 0% > :dependencies > Resolving dependencies ':compile'
. The information is updated on the same line, which is really nice. But sometimes we might need to run Gradle builds on a system that doesn't support this mechanism on the console or terminal, possibly an continuous integration server. To disable the progress logging we can set the environment variable TERM
to the value dumb
.
Written with Gradle 2.0.
Continue reading →
When we write technical documentation with Asciidoctor we can easily include source code listings. When we use the coderay
or pygments
source code highlighter we can also include line numbers. We must add the attribute linenums
to the listing block in our markup. This attribute is used by the source highlighters to create and format the line numbers. We can specify that the line numbers must be generated in table mode or inline mode. When the line numbers are in table mode we can select the source code without the line numbers and copy it to the clipboard. If we use inline
mode the line numbers are selectable and are copied together with the selected source code to the clipboard. To specify which mode we want to use for the line numbers we use the document attribute coderay-linenums-mode
or pygments-linenums-mode
depending on the source highlighter we use. We can use the values table
(default) or inline
.
= Source code listing
Code listings look cool with Asciidoctor and {source-highlighter}.
[source,groovy,linenums]
----
// File: User.groovy
class User {
String username
}
----
[source,asciidoc,linenums]
----
# Hello world
Asciidoc(tor) is aweseome!
----
Continue reading →
Asciidoctor is a great tool for writing technical documentation. If we have source code in the Asciidoc markup we can set the document attribute source-highlighter
to pigments
, coderay
, prettify
and highlightjs
. When we use highlight.js we can also add an extra document attribute highlightjs-theme
with the value of a highlight.js theme. If we do not specify the highlightjs-theme
the default theme github is used.
We use the following Asciidoc markup to see how the HTML output is when we transform the markup using the HTML backend:
Continue reading →
We can add comments to our Asciidoc markup. The comments will not be added to generated output. We can add both single and multiline comments in the markup. Single line comments start with a double slash (//
). Multiline comments are enclosed in a block of four forward slashes (////
).
The following sample markup defines Asciidoc markup with comments:
Continue reading →
Since Asciidoctor 1.5.0 we can use the document attribute hide-uri-scheme
to turn URLs into links, where the link text is displayed without the URI scheme. This can save typing when we simply want to add a URL without any special description.
In the next Asciidoc syntax we first define URLs without the hide-uri-scheme
attribute, followed by URLs after the attribute is set:
Continue reading →