サクサク読めて、アプリ限定の機能も多数!
トップへ戻る
世界禁煙デー
blog.mrhaki.com
The nice thing about Gradle is that we can use Java libraries in our build script. This way we can add extra functionality to our build script in an easy way. We must use the classpath dependency configuration for our build script to include the library. For example we can include the library Grgit, which provides an easy way to interact with Git from Java or Groovy code. This library is also the
Spock supports JUnit rules out of the box. We simply add a rule with the @Rule annotation to our Spock specification and the rule can be used just like in a JUnit test. The Spring Boot project contains a JUnit rule OutputCapture to capture the output of System.out and System.err. In the following example specification we apply the OutputCapture rule and use it in two feature methods: package com.m
In Grails we can execute code when the application starts and stops. We just have to write our code in grails-app/conf/BootStrap.groovy. Code that needs to be executed at startup must be written in the closure init. In the destroy closure we can write code that needs be executed when the application stops. But we are not limited to one BootStrap class. We can create multiple BootStrap classes as l
Groovy is a great language to create scripts. Most of the time if we invoke the scripts we pass arguments to the script. These arguments are available as a String[] array in our script. For example to get the first argument we can use the following code s = args[0]. To have real argument handling in our script we use Groovy's CliBuilder class. This class uses Jakarta Commons CLI under the hood. Wi
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 env
Groovy 2.1 introduced the @DelegatesTo annotation. With this annotation we can document a method and tell which class is responsible for executing the code we pass into the method. If we use @TypeChecked or @CompileStatic then the static type checker of the compiler will use this information to check at compile-time if the code is correct. And finally this annotation allows an IDE to give extra su
We can run test code with Gradle using the test task that is added by the Java plugin. By default all tests found in the project are executed. If we want to run a single test we can use the Java system property test.single with the name of the test. Actually the pattern for the system property is taskName.single. The taskName is the name of the task of type Test in our project. We will see how we
In a previous blog post we learned how we can use Hamcrest matchers. We can also create a custom matcher and use it in our Spock specification. Let's create a custom matcher that will check if elements in a list are part of a range. In the following specification we create the method inRange() which will return an instance of a Matcher object. This object must implement a matches() method and extr
Spock has support for Hamcrest matchers and adds some extra syntactic sugar. In an expect: block in a Spock specification method we can use the following syntax value Matcher. Let's create a sample Spock specification and use this syntax with the Hamcrest matcher hasKey: // File: SampleSpecification.groovy package com.mrhaki.spock @Grab('org.hamcrest:hamcrest-all:1.3') import static org.hamcrest.M
The Spring security plugin makes it easy to add authentication and autorization to our Grails application. The underlying Spring security framework is still accessible using Spring configuration and as a matter of fact the plugin uses a lot of the Spring security components. When we choose the logout action so-called logout handlers are configured and we can customize them in the Spring configurat
Groovy 2.1 introduces a new annotation to group multiple annotations and define an alias for the group. The new @AnnotationCollector annotation is an AST transformation. If we repeatedly have to add multiple annotations we can now create an alias for the multiple annotations and use it in our code. We can define the group of annotations as an argument for the @AnnotationCollector annotations. For
Groovy 2.0 brought us extension modules. An extension module is a JAR file with classes that provide extra methods to existing other classes like in the JDK or third-party libraries. Groovy uses this mechanism to add for example extra methods to the File class. We can implement our own extension module to add new extension methods to existing classes. Once we have written the module we can add it
Gradle has a special task to run a Java class from the build script: org.gradle.api.tasks.JavaExec. We can for example create a new task of type JavaExec and use a closure to configure the task. We can set the main class, classpath, arguments, JVM arguments and more to run the application. Gradle also has the javaexec() method available as part of a Gradle project. This means we can invoke javaexe
Starting from Grails 2.0 we can change the URL format in our application. Default a camel case convention is used for the URLs. For example a controller SampleAppController with an action showSamplePage results in the following URL /sampleApp/showSamplePage. We can change this convention by creating a new class that implements the grails.web.UrlConverter interface. Grails already provides the cust
We can use the Gradle announce plugin to send announcements from the build process. We can send data to Twitter (I don't know if our followers are waiting for this, but if you want to you can), but also to notification applications on our local computers. For Mac OSX Growl is supported, for Linux notify-send and for Windows Snarl. The plugin adds an announce object with the announce() method. The
A Gradle build file describes what is needed to build our Java project. We apply one or more plugins, configure the plugins, declare dependencies and create and configure tasks. We have a lot of freedom to organize the build file as Gradle doesn’t really care. So to create maintainable Gradle build files we need to organize our build files and follow some conventions. In this post we focus on orga
If we want to set an explicit encoding for the Java compiler in Gradle we can use the options.encoding property. For example we could add the following line to our Gradle build file to change the encoding for the compileJava task: apply plugin: 'java' compileJava.options.encoding = 'UTF-8' To set the encoding property on all compile tasks in our project we can use the withType() method on the Task
Groovy is a great language to write DSL implementations. The Groovy syntax allows for example to leave out parenthesis or semi colons, which results in better readable DSL (which is actually Groovy code). To run a DSL script we can use the GroovyShell class and evaluate the script. By default the script is evaluated with an instance of groovy.lang.Script class. But we can extends this Script class
Apache Extras Companion for Log4j contains a RollingFileAppender, which can be configured to automatically compress old log files. We can even save the old, archived log files to another directory than the active log file. In this post we learn how we can add and configure the RollingFileAppender in our Grails application. First we must define our dependency on the Log4j Extras Companion libary. W
In Groovy we can invoke a method just like in Java: we simply type the name of the method in our code. But what if we want to invoke a method name, but we don't know the name yet when we write the code, but we do know it at runtime? We can use the Reflection API for example to invoke the method, but in Groovy it can be shorter. In Groovy we can use a variable value for method invocation with a GSt
Since Groovy 1.7.6 we can use a Map as a case in a switch statement. Groovy adds the isCase() method where the switch operand is used to find map elements. Groovy tries to find a key with the given switch operand in the Map and if it is found true is returned. def testSwitch(val) { def result switch (val) { // New in Groovy 1.7.6: Map support. case [groovy: 'Rocks!', version: '1.7.6']: result = "M
If we want to unit test a Spring managed bean with a private field that is annotated with for example the @Autowired annotation, we must do something special. Normally we cannot access the private field to assign for example a stub implementation for testing, because we have to use the public setter method. But Spring allows the use of the @Autowired annotation on a private field. And that means w
In the groovy.util package we find the Proxy class. We can use this class to create a proxy for another object. All methods calls to the proxy object are redirected to the original object, instead for the methods we override in the proxy class: class User { String name String email String password String displayName() { name } } class UserProxy extends groovy.util.Proxy { List fields // Override g
In Groovy we can create a map and use the withDefault() method with a closure to define default values for keys that are not yet in the map. The value for the key is then added to the map, so next time we can get the value from the map. def m = [start: 'one'].withDefault { key -> key.isNumber() ? 42 : 'Groovy rocks!' } assert 'one' == m.start assert 42 == m['1'] assert 'Groovy rocks!' == m['I say'
The is macro in the clojure.test namespace can be used to write assertions about the code we want to test. Usually we provide a predicate function as argument to the is macro. The prediction function will call our code under test and return a boolean value. If the value is true the assertion passes, if it is false the assertion fails. But we can also provide a custom assertion function to the is m
In Grails we can add new Log4J appenders to our configuration. We must add the new appender definition in the log4j closure in conf/Config.groovy. We define new appenders with the appenders() method. We pass in a closure where we define our appenders. The name of the appender is an identifier we can use when we want to define the log level and packages that need to be logged by the appender. In th
In Groovy we can apply the @NullCheck annotation to a class, constructor or method. The annotation is an AST (Abstract Syntax Tree) transformation and will insert code that checks for null values in methods or constructors. If a null value is passed to an annotated method or constructor, it will throw an IllegalArgumentException. Without the annotation we could have a NullPointerException if we tr
このページを最初にブックマークしてみませんか?
『Messages from mrhaki』の新着エントリーを見る
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く