Maven rf option: If your project has some modules and the build fails at one of them, it’s possible to run the build only at this module, preventing from running the entire build. You can achieve this behavior by executing this command:

mvn -rf my-project clean install

Maven pl option: This option allows you to build specific modules instead of building all projects. You can execute the following command to have this behavior:

Some months ago I was noting the behavior of the Maven Eclipse plugin during the eclipse:eclipse goal.

I realized that its default behavior is to build the dependencies based on the Eclipse projects instead of the installed packages on the repository. During development time, it’s the behavior we need, but if you want to build the Eclipse files using the packages on the repository, you have to use the following command:

mvn eclipse:eclipse -Declipse.useProjectReferences=false

By default, the useProjectReferences flag is set to true, in other words, the plugin will create and reference the projects in Eclipse. Without it you’ll have to use mvn install to make changes available for dependent projects. Very interesting to note.

Update *: After creating the project dependencies, you have to execute mvn install to make it available for mvn eclipse:eclipse on dependent projects for the first time.

Configuration classes which read values from properties files should be Singletons that are loaded once on the application startup time.

The Configuration values can be used this way:

1

System.out.println(Configuration.HOST.getValue());

This example shows that the key and value from properties are stored with Enum constants. They are type-safe, they can be easily accessed through code completion in your favourite IDE and they can take advantage of refactoring tools.

Java 1.5 introduced the concept of Enum types. They are type-safe constants, which implements equals(), hashCode() and cannot be extended. Each constant can have attributes and override an abstract method created on each Enum class.

Although Singletons are not encouraged, the best way to create it is using Enum types. Here is an example:

1

2

3

4

5

6

7

publicenumSingleton{

INSTANCE;

publicvoidsayHello(){

System.out.println("Hello World!");

}

}

And then you call it this way:

1

Singleton.INSTANCE.sayHello();

Using Enums to create Singletons brings the serialization mechanism already bundled in the Enum type. This technique is described on the Effective Java Second Edition book by Joshua Block.

Danilo sent us an interesting Java-Quiz from the Java Specialists’ Newsletter created by Olivier Croisier. You have to insert your corrective code in place of the //FIXME comment, following these instructions:

Lately I started using the core Hamcrest matchers bundled with the JUnit framework to create more readable unit tests.

Hamcrest matchers were created to improve the readability of unit testing code. It’s a framework which facilitates the creation of matcher objects to match rules specified in unit tests. Some examples will let it to be clearer:

The first example checks if one Person object is equal to another using the Object equals method, which was overridden in the Person class. The is syntax defines a matcher which is a shorthand to is(equalTo(value)). The second one uses the is(equalTo(value)) matcher to check the size of an integer list of fixed size numbers. The assertThat method is used in conjunction with the is(equalTo(value)) matcher, which makes the test sentence very human readable.

An interesting thing is the possibility to create a custom matcher, like this one which tests if a given list only has even numbers:

These two tests use the static factory method evenNumbers to instantiate the matcher on the test code. Not the use of the not matcher on the shouldNotHaveOddNumbers test to assert that no odd numbers are present on the given list. All tests use the static import feature, which turns the test not clean and not cluttered with the class qualification.

I haven’t experienced the other common matchers on unit testing code, like the Beans, Collections and Number ones. I think they turn the tests more readable, clean and easy to change. And you? Have you ever used Hamcrest matcher? If you have other examples of using it, post them here!