Delete all Eclipse project related meta information via shell

Today I was fighting some really annoying behaviour of Eclipse in combination with refactored multi module maven projects.

Whenever I tried to import the projects, I recieved the error

An internal error occurred during: "Updating Maven Project". java.lang.NullPointerException

I tried to solve the problem by removing my eclipse projects and cleaning all eclipse related files, but that didn’t work. For some reason mvn eclipse:clean refused to delete my files. Thus I had to find a way to recursively delete all eclipse related information from my project. So here it is:


find . -name .classpath -exec rm -rf {} \;

find . -name .project -exec rm -rf {} \;

find . -type d -name .settings -exec rm -rf {} \;

 

Get rid of “unmappable character for encoding Cp1252″ once and for all

Nowadays, many open source as well as commercially developed projects contain code that is UTF-8 encoded. More general it is very unlikely that you checkout some code from GIT, SVN or [insert arbitrary abbreviation here] that is actually encoded with Windows’ crappy default encoding. (In case of my western European system, that encoding would be Cp1252).

So, how many times have you seen the above error when compiling some Java code on a windows machine? And how many times have you wondered if there is a solution to set the file encoding in a global, system wide manner? Well, fortunately, there is a way to do that!

A quite simple procedure to globally set the file encoding for all JVMs is given in the answer by erickson in this stackoverflow question. All you need to do, is to specify a envirnoment variable called JAVA_TOOL_OPTIONS. If you set this variable to -Dfile.encoding=UTF8, everytime a JVM is started, it will pick up this information.

Obviously, you are able set other JVM parameters via JAVA_TOOL_OPTIONS as well. See the JVM documentation for more details. If you are interested in further details about character encodings in Java, check out this excellent article.

Database Reverse Engineering with Spring Roo 1.2

Reverse engineering an existing database into a new application seems to be one of the most important features in rapid application development. Having the right tools at hand can give you a major boost when rewriting an application on top of an existing database.

Spring Roo hasn’t had support for Database Reverse Engineering (DBRE) when its first major version was released. Not surprisingly, adding DBRE support had been the most requested feature for subsequent versions. In Roo 1.1 full support for incremental DBRE was added and since then it has been constantly improved. It is fully documented and pretty much straight forward to use.

According to my WordPress Stats, my old post about DBRE in Roo is still the second most visited on my blog. As it became more than obsolete, I thought it might be time to clear things up and point you guys to the new DBRS features in Spring Roo 1.1.x and 1.2.x

So if you are interested in Roo and/or DBRE, make sure to check out the latest developments in this area.

Missing Snapshot Dependency in Spring Roo Project

Today I ran into an error while trying to create a plain new Spring Roo project based on the latest nightly snapshot build of Roo.

[ERROR] Failed to execute goal on project cassandra: Could not resolve
dependencies for project cassandra:cassandra:jar:0.1.0.BUILD-SNAPSHOT:
Failure to find org.springframework.roo:org.springframework.roo.annotations:
jar:1.2.0.BUILD-SNAPSHOT in http://maven.springframework.org/release was
cached in the local repository, resolution will not be reattempted until the
update interval of spring-maven-release has elapsed or updates are forced

 
The solution to the problem is quite easy. Just intsall the org.springframework.roo.annotations-1.2.0.BUILD-SNAPSHOT.jar into your local maven repositroy (or alternatively your company’s repository server). The file itself can be found in the unpacked roo installation folder under spring-roo-1.2.0.BUILD-SNAPSHOT/annotations. To install it into your local repository, execute:



mvn install:install-file -DgroupId=org.springframework.roo
-DartifactId=org.springframework.roo.annotations -Dversion=1.2.0.BUILD-SNAPSHOT
-Dpackaging=jar -Dfile=org.springframework.roo.annotations-1.2.0.BUILD-SNAPSHOT.jar

Maven 3 Site Plugin – How To

Creating various helpful reports in a Maven based project is quite easy and straight forward. Such reports vary from simple javadoc to dependency reports and code coverage analysis and having them can be a major advantage during development. In the following post, I present an understandable how-to of generating these reports.

Configuration

Site and report generation is not a base feature of Maven (anymore [see footnote below]) , instead there is a plugin that is responsible for this tasks. The site plugin can be configured in the plugins section of a pom.xml. The configuration itself can contain several report plugins. The following snippet configures the site plugin and adds a number of useful report plugins to it. Simply copy this configuration to your personal pom.xml and adopt any parameters to your needs. Make sure to remove or replace the configuration file location within the maven-checkstyle-plugin.

 
<build>
<plugins>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-site-plugin</artifactId>
  <version>3.0-beta-3</version>
  <configuration>
    <reportPlugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.2</version>
        <reports>
          <report>index</report>
          <report>project-team</report>
          <report>license</report>
          <report>mailing-list</report>
          <report>dependencies</report>
          <report>dependency-convergence</report>
          <report>plugin-management</report>
          <report>cim</report>
          <report>issue-tracking</report>
          <report>scm</report>
          <report>summary</report>
        </reports>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.6</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.7</version>
      </plugin>     
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <!-- Adopt config location to your needs, or remove configuration entry completly to use default version.
          <configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation&gt;
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <argLine>-Xmx256m</argLine>
          <argLine>-XX:MaxPermSize=256m</argLine>
        </configuration>
        <!-- Usually findbugs needs a lot of memory, change these values if needed. -->
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jdepend-maven-plugin</artifactId>
        <version>2.0-beta-2</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>taglist-maven-plugin</artifactId>
        <version>2.4</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
        <version>2.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <linkXref>true</linkXref>
          <minimumTokens>100</minimumTokens>
          <minimumPriority>3</minimumPriority>
          <!-- Change minimum priority to see more or less messages -->
          <targetJdk>1.6</targetJdk>
        </configuration>
      </plugin>
    </reportPlugins>
  </configuration>
</plugin>
</plugins>
</build>

 

Report generation

Once, your configuration is set up, you can trigger a report generation with the command mvn site. Depending on the reports used, this may take a while. The final reports will be placed within the target folder. Just open target/site/index.html and you should see something similar to the following screenshot. Click on one of the two red-circled links to expand the report sections. Enjoy reading through the reports and improving your code.

Example Page - Maven Site Plugin

Note: With the introduction of Maven 3, there have been changes in the area of site and report generation, thus the shown pom.xml snippet does only work for Maven 3.0 or later.

10 Typical Problems in Enterprise Java Applications by Eberhard Wolff

Just had a look through some nice slides done by Eberhard Wolff (there is also a video in German, but I haven’t had the time to watch it yet). As Eberhard mentioned on his blog post, depending on the audience many people seem to be already aware of those typical problems. Then on the other hand I have seen sooo many projects and source code violating common sense and introducing exactly those common mistakes…

My personal opinion; especially the use of AOP and comprehensive testing should get more emphasize in businesses and education. Not sure if people will ever learn to write (Java) code that is capable of handling multi threading and transactions appropriately. But some of the other issues can defiantly be tackled by better education of software developers.

Anyways, a quite nice presentation, and even if you aware of most of these problems, getting them back to your brain might help during your next project. Thanks Eberhard!

Faster logging with SLF4J

Logging in Java has a long and obscure history. There are numerous APIs for logging, and there is for sure no answer which one is the best. Today, I started a project from scratch, which gave me the opportunity to try out SLF4J (Simple Logging Facede 4 Java). In most of my projects I have been using  Log4J so far. The switch was rather simple, anyways I would like to write down the most important steps here.

Why SLF4J

  • Log4J was initially created by Ceki Gülcü. After Log4J Ceki created SLF4J, so I guess he put everything he learned from Log4J into SLF4J.
  • SLF4J provides a nice way of creating short and easy to read logging statements in the code, that have only minor resource consumption if logging is turned off.
  • Some commonly used open-source projects use SLF4J compared with some of its provider bridges, in seldom cases this might struggle using the bridged logging APIs.
  • Last but not least: Why not? As previously mentioned, there is no best solution for logging, so why not trying alternatives.

How does it work

As the name suggests SLF4J is a logging facade that needs an actual logging implementation to be able to log anything at all. All the widespread logging solutions can be used by SLF4J. So you might use Log4J or JCL or even the Simple Implementation to do logging with SLF4J. In any case you will use the same API calls in your code (thus facade in the name).

Get the JARs

If you configure your JARs by copying them into some lib folder (I hope nobody is actually doing that any longer!), just go to their web page and download the latest versions of slf4j-api and the slf4j-{your favorite provide}. Don’t forget to put the JAR of your provider into the lib folder as well.

If you use maven or ivy, here is the sample configuration for SLF4J backed by Log4J. Please note, that the provider JAR will be automatically retrieved, so do not put it into your maven dependencies.

<properties>
    <slf4j.version>1.6.0</slf4j.version>
</properties>
...
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j.version}</version>
</dependency>

Let’s log

Logging with SLF4J is pretty straight forward. Just have a look at the example below. The second logging statement shows how the API should be used to avoid to String conversion for large objects if logging is turned off (or the level is lower). Check the SLF4J FAQ for further details on performance of (non) logging. If you are wondering if you should declare your logger as a static or instance variable, make sure to look at that consideration.

final static Logger logger = LoggerFactory.getLogger(Slf4jTest.class);

public void someMethod(HeavyObject object) {
    logger.debug("This is a simple debug message");
    logger.debug("HeavyObject is {}", object);
}

Roo 1.1.0 released and millionth question on stackoverflow

Ben Alex and his team down here in Sydney have successfully released Spring Roo 1.1.0. The feature list is long. Promising ones like GAE & GWT Integration as well as Incremental Database Reverse Engineering capabilities are just some of the highlights. Make sure to take a look at Ben’s blog post or visit the official Roo site.

Meanwhile another impressive project has reached a further milestone. Stackoverflow is growing faster and faster and has now listed more than one millionen (1.000.000 !) questions asked and answered by professional and enthusiastic programmers. If you are not yet a member of this fantastic community, make sure to join today!

Spring Roo at JUG Stuttgart – Presentation Slides available

The presentation slides of my Spring Roo introduction talk at the Java User Group Stuttgart are now available. Thanks to the audience for the feedback and interesting discussions after the presentation.

Die Vortragsfolien zur Spring Roo Einführung bei der Java User Group Stuttgart sind jetzt online. Vielen Dank an die Zuhörer für das Feedback und die interessanten Gespräch nach dem Vortrag.

http://www.jugs.org/protokolle2010.html#14062010

Building Spring Roo from Source

Spring Roo 1.1.0 M1 has just been released and the development on 1.1.0 M2 is just about to start. If you want to check it out from the development stream and see the latest features, here is the tutorial you need. If you are running a Windows operation system this task is quite a bit more challenging. All you ubuntu (and most of the other *nix systems) users only need to do this:

sudo apt-get install git-core gitk
git clone git://github.com/SpringSource/spring-roo.git
cd roo
mvn clean install
sudo ln -s bootstrap/roo-dev /usr/bin/roo-dev

Thats it, seriously! For all the Windows users, read the following paragraphs to accomplish the same.

Installing Git on Windows

First of all you need to install a version of Git for Windows. I decided to not go with the Cygwin + Git option, but instead install Msysgit. A third alternative for Windows is to use Egit for Eclipse, I may discuss that in a couple of days here as well.

There is a nice blog post by Kyle Cordes on the installation of Msysgit. In my case I downloaded version 1.7.0.2-preview double-clicked it and kept all the default installation options (this changed the font style of my cmd shell, but I could change it back later).

Edit: If you get an error like this one Result of cmd.exe /X /C “git log “–pretty=format:Git-Commit-Hash: %H” -n 1″ execution is: ’1′. you should add {Msysgit_installation}/cmd to your PATH environment variable.

Getting Roo Sources via Git

In the next step you need to create a local clone of the current Roo sources by using Git. Therefore you should create a new folder in which you want to store the files. Right-click that folder and select Git Bash from the context menu. The git command bash opens.

Enter git clone git://github.com/SpringSource/spring-roo.git and wait till it is finished. This will create a new folder named roo. You should see something similar to the picture below.

Roo setup 3

Building Roo Sources via Maven
Open a windows command shell and switch to the created roo folder. Build the project by typing mvn clean install. (I assume you already have Maven 2 installed, if not do so now). Wait again, this may take a couple of minutes.

Configure Roo-Dev
The last thing you need to do is setting up an environment variable called ROO_DEV_HOME in your system settings, which should point to the bootstrap folder within the roo folder. You also need to add this variable to your path environment variable. See the screenshots below (sorry, that those are in German. I guess you mange to set up environment variables anyhow).

Roo setup 1

Roo setup 2

Using Roo-Dev
You can now use your build development version of Roo. Just open the windows command shell, type mkdir myproject, cd myproject and roo-dev. By now you should see something similar to the below picture. If so, congratulations! You are now running the current development version of Spring Roo.

Roo Setup 4

Updating Roo-Dev
You may want to update your Roo development version from time to time. Therefore open the GIT shell as you done have before. This time type git pull, which will pull all the remote changes into your local clone. Do not forget to run mvn clean install afterwards to build the changed sources.