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 {} \;

 

Scala & Maven – POM Example

Today, there is no waffle about what Scala or Maven is. This is just the plain configuration to build Scala projects with Maven. If you want more detailed information check Maven for Beginners at scala-lang.org or browse through the documentation of the Scala-Maven-Plugin.

By the way,the configuration works as well if you want to mix Java and Scala within a single project. Just place your scala files in src/main/scala and your Java files in src/main/java (or the src/test equivalents). Maven will do the rest for you.

Enjoy!

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.company</groupId>
  <artifactId>my-artifact</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-tools Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.9.1</artifactId>
      <version>1.6.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
          </jvmArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Getting started with Scala

Update: Scala Documentation (an excellent site for getting started with Scala) has just been launched. Make sure to check it out as well.

If you want to get started with Scala, here is a short list with (in my opinion) some of the most important resources to get you on the fast track.

  1. Get a book! I highly recommend this one, but there are plenty out there already.
  2. Install SBT and learn how to use it. (You might as well use Maven with Scala if you really want to.)
  3. Install Scala IDE for Eclipse and use sbteclipse to import your SBT projects.
  4. Get some friends out there, and learn.
  5. And not to forget my personal favourite for quick hacks: Simply Scala
  6. Enjoy!

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.

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.

Wie man Code-Qualität mit Eclipse, Maven, Findbugs, PMD und Checkstyle erreichen kann

Heute möchte ich ein paar Worte über Code-Qualität loswerden, oder vielmehr über Tools, mit denen man Code-Qualität fördern kann. Und da das Thema für mich irgendwie so einen typisch deutschen Charakter hat, gibt es den Eintrag heute mal in deutsch. Nicht das man in anderen Ländern nicht auch auf Qualität achten würde, aber in Deutschland hat man meiner Meinung nach schon einen sehr starken hang zu Qualitätsmaßnahmen und formalen Methoden um diese zu spezifizieren und zu verifizieren.

Nun aber zum Inhalt, die Idee ist es ein paar Tools, welche die Code-Qualität enorm verbessern können, mittels Maven so in ein Projekt zu integrieren, dass eine Reihe von Tools automatisch bei den Entwicklern in der Eclipse IDE eingebunden werden. Voraussetzung hierzu ist natürlich, dass zum einen das Projekt auf Maven basiert und zum anderen, dass Eclipse als IDE verwendet wird.

Die Tools die hierbei eingebunden werden sollen sind: Checkstyle, Findbugs und PMD. Ersteres prüft Source-Code auf Styling-Conventions hin und die beiden letzteren führen eine statische Code-Analyes durch um eine Reihe bekannter Quellen für Bugs im Source-Code aufzudecken. Zu allen drei Tools existieren Eclipse-Plugins, die sich jeder Entwickler zunächst in seiner IDE installieren muss. Der Installationsprozess soll hier kein Thema sein, sondern vielmehr die Konfiguration von Maven um die Tools automatisch mit in die Eclipse Projektkonfiguration aufzunehmen. Alle Tools können auch direkt in den Maven-Build integriert werden, und dies ist auch in jedem Fall zu empfehlen. Es hat sich jedoch gezeigt, dass die wenigsten Entwickler regelmäßig auf den Build-Server gehen und sich die jeweiligen Reports ansehen. Daher die so wichtige Integration direkt in die Entwicklungsumgebung.

Nachdem die Plugins erfolgreich in Eclipse installiert wurden, muss das Projekt POM angepasst werden. Wer ein allgemeines Parent POM hat, dass von allen Projekten verwendet wird, kann die Anpassungen gleich hier vornehmen, so gelten sie in Zukunft für alle Projekte. Für das Erstellen der Eclipse-Projekt-Konfiguration ist das Maven-Eclipse-Plugin zuständig, und eben jenem Plugin müssen lediglich die drei zusätzlichen Projectnatures sowie Buildcommands hinzugefügt werden.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <additionalProjectnatures>   
                    <nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
                    <nature>edu.umd.cs.findbugs.plugin.eclipse.findbugsNature</nature>
                    <nature>net.sourceforge.pmd.eclipse.plugin.pmdNature</nature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
                    <name>edu.umd.cs.findbugs.plugin.eclipse.findbugsBuilder</name>
                    <name>net.sourceforge.pmd.eclipse.plugin.pmdBuilder</name>
                </additionalBuildcommands>
            </configuration>
        </plugin>
    </plugins>
</build>

Wird nun das Maven-Eclipse-Plugin von einem Entwickler aufgerufen, und das Projekt anschließend in Eclipse importiert, verwendet dieses automatisch die drei Plugins für Checkstyle, Findbugs und PMD. Wer zusätzlich die Konfiguration zu den jeweiligen Tools anpassen und für alle Entwickler gesamtheitlich hinterlegen möchte, fügt ein paar weitere Zeilen in der Maven-Eclipse-Plugin Section hinzu. Exemplarisch sei hier gezeigt, wie eine Checkstyle-Konfiguration, die auf einem für alle Entwickler erreichbaren Server hinterlegt ist, eingebunden werden kann. Das Einbinden weiterer Konfigurations-Dateien erfolgt analog, Details zur Erstellung dieser Dateien finden sich auf den jeweiligen Tool Homepages.


    </additionalBuildcommands>
    <additionalConfig>
        <file>
            <name>.checkstyle</name>
            <content>
            <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
            <fileset-config file-format-version="1.2.0" simple-config="true">
                <local-check-config name="meine-checkstyle-checks" location="http://someserver/somepath/checkstyle-config.xml" type="remote" description="">
                    <additional-data name="cache-file" value="false"/>
                </local-check-config>
                <fileset name="all" enabled="true" check-config-name="meine-checkstyle-checks" local="true">
                    <file-match-pattern match-pattern="." include-pattern="true"/>
                </fileset>
            </fileset-config>]]>
            </content>
        </file>
    </additionalConfig>
</configuration>

Das war auch schon alles, von nun an bekommt jeder Entwickler direkt in seiner IDE angezeigt, wenn er gegen Style-Conventions verstößt, oder potenzielle Fehlerquellen in den Source-Code einbaut. Die so erzeugten Informationen gehen also nicht irgendwo auf dem Build-Server verloren.

Nochmal in der Übersicht:

1. Plugins in Eclipse installieren
2. Project-Nature und Buildcommands in POM eintragen
3. Individuelle Konfigurationen zur Verfügung stellen und ebenfalls im POM verlinken