Coding

Gradle Goodness: Use Command Line Options With Custom Tasks

Posted on by  
Hubert Klein Ikkink

Suppose we have a custom task with some properties that can be configured. Normally we would add the configuration in the build script. But we can also use command line options to configure a task. So when we run the task from the command line we can provide a configuration value for the task on the command line. To see which command line options are available for a task we can use the Gradle built-in task help followed by the option --task and the task name. To indicate a property as command line option we use a @Option annotation. We can specify the name of the command line option, a short description and also the order that is used to display the options with the help task.

Let's create a sample custom task and use the @Option annotation. In the following build file we create a custom task GenerateVersionFile. This task generates a file with a default name of version.txt in the build/ directory. The file contains the project version value. We make the property that defines the output filename as a command line option. This way the name can be defined when we run Gradle (and still of course using the default configuration in a build file).

Continue reading →

Spocklight: Check No Exceptions Are Thrown At All

Posted on by  
Hubert Klein Ikkink

In a previous post we learned that we can check a specific exception is not thrown in our specification with the notThrown method. If we are not interested in a specific exception, but just want to check no exception at all is thrown, we must use the noExceptionThrown method. This method return true if the called code doesn't throw an exception.

In the following example we invoke a method (cook) that can throw an exception. We want to test the case when no exception is thrown:

Continue reading →

Gradle Goodness: Add But Do Not Apply Plugin Using Plugins Block

Posted on by  
Hubert Klein Ikkink

Sometimes we want to include the classes from a plugin, like tasks, in our build class path without actually applying the plugin. Or we want to add the classes to the root project and actually apply the plugin in subprojects. We can achieve this with a buildScript block and add the plugin dependency to the classpath configuration. But we can also do this with the newer plugins configuration block. Inside the plugins block we define the id and the version of the plugin, and since Gradle 3.0 we can also use the apply method. We have to set the value false to include the plugin to the class path, but not apply it to the project.

In the following example we add the Asciidoctor plugin to our build file, but we only want to use the AsciidoctorTask task from this plugin.

Continue reading →

Gradle Goodness: Check The Gradle Daemon Status

Posted on by  
Hubert Klein Ikkink

Since Gradle 3 the Gradle daemon is automatically used to run Gradle. This means Gradle will startup up faster after a first run. Gradle tries to re-use a daemon if it is available. We can check the status of the Gradle daemon processes with the new command-line option --status. We get the status results for the Gradle daemons with the same Gradle version that is used to view the status. So when we use Gradle 3.0 with the --status option we only see the 3.0 Gradle daemons.

The following example shows the sample output of running gradle with the --status option:

Continue reading →

Check Which Candidates Can Be Updated With SDKMAN!

Posted on by  
Hubert Klein Ikkink

The Software Development Kit Manager (SDKMAN!) is an awesome and very useful tool. We can use it to install and manage candidates like Groovy, Grails, Griffon and Gradle. If we want to know if a new version of an installed candidate is available we use the outdated command. SKDMAN! returns a list of candidates with newer versions and also displays the version we have and is available. If we specify the candidate we can see if for that specific candidate a newer version is available.

For example we can get the following results:

Continue reading →

"this" in javascript

Posted on by  
Tammo Sminia

In object-oriented languages, like Java, this refers to the instance of the class where you run the method. In javascript this is often also the case, but not always. In this post we'll explore some situations. And I give some tips on how to deal with them. The normal case. The function eat is defined on carrot. And we simply run it. this will refer to the enclosing object, which is carrot, with name "carrot".

var carrot = {
  name: "carrot",
  eat: function () {
    console.log(this);
    console.log("eating " + this.name);
  }
};

carrot.eat(); //result: eating carrot

Continue reading →

Securing your application landscape with Spring Cloud Security - Part 1

Posted on by  
Riccardo Lippolis

Securing an application is difficult. Securing an entire application landscape is even more difficult! In this modern era of blazing fast microservices we do not want the additional complexity of having to secure it all manually. This is where Spring Cloud Security comes in. By combining proven technologies, it helps us achieve performant, configurable end-to-end security across multiple applications. So what technologies are being combined? Well, a lot... We will not mention them all here, but the foundation relies on Spring Boot and Spring Security OAuth. OAuth, or, in our case, OAuth2 is basically an authorization delegation protocol. To quote Wikipedia, OAuth:

[...] specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials.

Continue reading →

Spring Batch and Feign OAuth2 RequestInterceptor

Posted on by  
Willem Cheizoo

We have a microservice architecture with one of the microservices having a Spring Batch job which processes a CSV file and calling another microservice. Both microservices are OAuth2 protected ResourceServers. When we call the first microservice, a batch job is started and we want the Authorization header to be passed to the second microservice. The solution can be defined as: In a Feign RequestInterceptor, grab the current OAuth access_token and pass it on the the RequestTemplate with Hystrix running in SEMAPHORE execution isolation strategy

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableResourceServer
@EnableOAuth2Client
public class MyApplication {

  public static void main(String[] args) {
    SpringApplication.run(MyApplication .class, args);
  }
}

Continue reading →

Ratpacked: Create a Partial Response

Posted on by  
Hubert Klein Ikkink

Suppose we want to support partial JSON responses in our Ratpack application. The user must send a request parameter with a list of fields that need to be part of the response. In our code we must use the value of the request parameter and output only the given properties of an object. We implement this logic using a custom renderer in Ratpack. Inside the renderer we can get access to the request parameters of the original request.

In our example Ratpack application we have a Course class, which is a simple class withs some properties:

Continue reading →

Grails Goodness: Pass JSON Configuration Via Command Line

Posted on by  
Hubert Klein Ikkink

We can use the environment variable SPRING_APPLICATION_JSON with a JSON value as configuration source for our Grails 3 application. The JSON value is parsed and merged with the configuration. Instead of the environment variable we can also use the Java system property spring.application.json.

Let's create a simple controller that reads the configuration property app.message:

Continue reading →

Ratpacked: Using Groovy Configuration Scripts As Configuration Source

Posted on by  
Hubert Klein Ikkink

Ratpack has a lot of options to add configuration data to our application. We can use for example YAML and JSON files, properties, environment variables and Java system properties. Groovy has the ConfigSlurper class to parse Groovy script with configuration data. It even supports an environments block to set configuration value for a specific environment. If we want to support Groovy scripts as configuration definition we write a class that implements the ratpack.config.ConfigSource interface.

We create a new class ConfigSlurperConfigSource and implement the ConfigSource interface. We must implement the loadConfigData method in which we read the Groovy configuration and transform it to a ObjectNode so Ratpack can use it:

Continue reading →

shadow-left