Run one or Exclude one test with Gradle

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

Run one or Exclude one test with Maven

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 (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

Spicy Spring: Creating a Fully Executable Jar

With a Spring (Boot/Cloud) application you can create a fully executable JAR, where the jar can be run from the command-line by just calling ./my-own-jar-0.0.1.jar.
My colleague Mr. Haki wrote a nice blog about creating a fully executable JAR for Grails.

Together with the famous one-liner of Josh Long in mind: “Make JAR not WAR!”, create a JAR whenever you develop a Spring Boot/Cloud application. As described in Mr. Haki’s blog, it is very easy to make our Spring application executable:

Maven configuration

Gradle configuration

You can find the description in the Spring Reference guide.

Happy Developing! And remember, “Make JAR not WAR!”

Prevent ‘No plugin found’ in multi-module maven

Defining a maven plugin on a submodule in a multi-module maven project can give us a ‘No plugin found’ error. Especially if we have a multi-module project and we want to apply a maven plugin in only one specific module this error occur pretty often.

Let’s say we have a multi-module root pom which looks like this.

Instinctively we add the plugin (tomcat7 for example) to this specific module module2, like this.

When we run the command mvn tomcat7:help on the multi-module root pom we get the following error:

The solution is very simple: We specify the plugin in the pluginManagement section of our multi-module root pom.

And in our specific module module2 we clear the version of the plugin, since it is already defined in the multi-module root pom (the parent).

Now when can run the command mvn tomcat7:help and get no error. We are ready to configure the plugin on our submodule.

Stateless Spring Security Part 3: JWT + Social Authentication

This third and final part in my Stateless Spring Security series is about mixing previous post about JWT token based authentication with spring-social-security. This post directly builds upon it and focusses mostly on the changed parts. The idea is to substitude the username/password based login with “Login with Facebook” functionality based on OAuth 2, but still use the same token based authentication after that.

Login flow

Client-side

The user clicks on the “Login with Facebook” button which is a simple link to “/auth/facebook”, the SocialAuthenticationFilter notices the lack of additional query parameters and triggers a redirect leading the user of your site to Facebook. They login with their username/password and are redirected back, again to “/auth/facebook” but this time with “?code=…&state=…” parameters specified. (If the user previously logged in at facebook and had a cookie set, facebook will even instantly redirect back and no facebook screen is shown at all to the user.) The fun part is that you can follow this in a browsers network log as it’s all done using plain HTTP 302 redirects. (The “Location” header in the HTTP response is used to tell the browser where to go next)

Continue reading

Package-only dependencies in Maven

Sometimes you have a Maven project that needs dependencies for running tests that you do not want ending up in the final packaged WAR. We all know the test directive in the POM that accomplishes this. You might also have dependencies that are only required at runtime and need to be in the WAR but not on the compile classpath. Normally you would use the runtime directive in the POM.

Consider a situation where we have a dependency that we want to be available at runtime (in the WAR), but not on the classpath during the execution of our tests. A nice example of this is logging implementations: we want to use the slf4j-simple implementation for running unit tests, but we want logback-classic to be packaged in the WAR.

To accomplish this, you can use the maven-dependency-plugin as illustrated in the following POM snippet:

This way you have a single logging implementation on the classpath when running tests (slf4j-simple) and a single logging implementation for runtime in the final packaged WAR (logback).

Example source code

Detect Maven Dependency Mediation

As of Maven 2.0.9 a new feature was added to Maven called dependency mediation.
Dependency mediation is the technique used by Maven to resolve your project dependencies in the specific case when a dependency occurs multiple times in your dependency tree.
Usually this occurs on transitive dependencies linked through the dependencies of your project.
In these cases mediation will be performed using the nearest win-strategy.
In short this strategy means that Maven will use the version declared in the pom.xml that is closest to your project pom.xml.
Hence, no in-depth intelligence is used to resolve the dependency conflict.
Actually, I can’t really think of a conflict resolve strategy that would really solve this problem.

Any strategy I can think of has the hazard of linking incompatible dependencies in to your project.
Using Maven version ranges can ofcourse resolve compatibility between artifacts, but this also requires you to establish a compatibility matrix of your dependencies. A pretty tedious task if you ask me.

Now this whole mediation feature might sound like a very undesirable feature, but it’s not!
With this feature you can now at least be made aware of any dependency conflicts in your project dependencies.
When you build your project using the -X switch, Maven will output all mediations (and a lot more) that have been performed.

Now, wouldn’t it be cool if there was a maven-plugin to detect mediation?
JDriven took the liberty of extending Apache’s dependency plugin with such a feature and share it with you.
Continue reading

Compare JAR files content; decompiling class files

When I was recently working on a large restructuring and refactoring where I also replaced Ant by Maven, it was really necessary to compare the complete content of two different JAR files. It was required to know that the result of the restructuring and refactoring hadn’t changed the artifacts, thus the JAR files.
In the JAR files were different Class files present. When I compared the content of the two JAR files (with a binary compare) all the content was radically changed. This was partly because the compiler compiled the Class files at a different timestamp.

Since I wanted the best possible comparison between the two JAR files I needed to compare all Class files in the JAR by decompiling and comparing. This should give me a clearer and more honest picture of the differences. For this action I used Beyond Compare. By using an additional File Format (Java Class to Source) I was able to completely compare the decompiled Class files of the two JARS.

If you ever need to compare JAR, WAR or EAR files and need to compare the Class files inside; don’t try to unpack to a folder, decompile to a different folder and finally folder compare. You should use Beyond Compare and save time on comparing. The Beyond Compare license will pay itself off.

Consume REST JSON webservices easily using Spring Web!

Spring made it very easy to consume JSON webservices. In this article I describe how to receive and parse JSON and how to send your Java objects as JSON.

First we need to include the required dependencies. If you use maven, include the following dependencies:

Spring web has a RestTemplate class which can be used to call the REST webservices. The Jackson dependency supplies a message converter class which can be used to send and receive Java objects which are automatically converted to JSON en reversed from JSON.

Continue reading