log4p

Peter Maas’s Weblog

Archive for November, 2007

CPD with maven2 and PMD

Detecting copy/pasted code is a useful technique to find potential buggy code. Duplicated code often results in bugs being fixed in only one version of the copied code. PMD is a great sourcecode analysis tool which integrates nicely with maven and has CPD (copy/paste detection) capabilities.

To configure with default settings and CPD support add the following to the pom:

  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-pmd-plugin</artifactId>
  4.   <configuration>
  5.     <sourceEncoding>utf-8</sourceEncoding>
  6.     <minimumTokens>100</minimumTokens>
  7.     <targetJdk>1.5</targetJdk>
  8.   </configuration>
  9. </plugin>

You can configure the minimum code size which trips the CPD. The default of 100 tokens corresponds to approximately 5-10 lines of code.

Generated reports look like this and show the file in which the duplicates where found and the duplicated code:

CPD Report

Really useful, great hooks for DRY-ing out code!

No comments

multiproject maven2: getting the site to work

When generating a project site for a multiproject setup as described in my previous post many people run into the same problem. The links to the different modules don't work when you use the 'site:site' target. A couple of solutions exist which 'solve' this 'problem' (technically it is a feature).

The actually links are configure correctly when the site is actually being staged or deployed. The following command will stage (generated) the site in the specified location:

mvn site:stage -DstagingDirectory=/Users/peter/Desktop/Wosi/generated_site/

And all links are working fine.

It is also possible to deploy the site to a different server. Configure the target location in the pom:

  1. <distributionManagement>
  2.   <site>
  3.     <id>maven.apache.org</id>
  4.     <url>scp://maven.apache.org/deploy/path</url> 
  5.   </site>
  6. </distributionManagement>

When running the 'site:deploy' target maven will zip and copy (and afterwards unzip) the generated site to the configured server.

If the ssh keys are configured correctly no passwords are needed; otherwise maven prompts for the ssh password and uses the currently logged in user (which is overridable by using scp://username@hostname/path/). Links will work now as well!

No comments

Getting started with multiproject maven2

I'm helping a group of students to split up their codebase into a couple of manageable project using maven2 as the build tool. Their existing project is a layered Spring MVC web application. The following layers are present:

  • data model
  • dao / integration
  • services
  • web application 1

Since dependencies where not managed yet the codebase managed to drift into containing spring 2 and spring 1.2.6 libs without anybody overseeing the classpath... something maven should really help to fix.

Once you've got maven2 installed it is quite easy to setup a project structure which packages and versions of the different layers in different libraries (jars) so they can be reused for different purposes.

First we'll define the parent project, which will contain dependencies and configuration shared between all layers:

mvn archetype:create -DgroupId=wosi -DartifactId=wosi

This will create a directory with the artifactId as name containing pom file (project object model) and a source directory. We won't need the source directory so it can be wiped.

Since the parent project will aggregate a couple of 'modules' the packaging should be changed from the default 'jar' to 'pom':

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>wosi</groupId>
  5.   <artifactId>wosi</artifactId>
  6.   <packaging>pom</packaging>
  7.   <version>1.0-SNAPSHOT</version>
  8.   <name>wosi</name>
  9.   <url>http://maven.apache.org</url>
  10.   <dependencies>
  11.     <dependency>
  12.       <groupId>junit</groupId>
  13.       <artifactId>junit</artifactId>
  14.       <version>3.8.1</version>
  15.       <scope>test</scope>
  16.     </dependency>
  17.   </dependencies>
  18. </project>

We can leave the dependency on junit, since we'll use it in all projects.

Now, if we create projects from the previously generated directory they'll be registered as 'modules' automatically. Let's create the different projects. From the command line run:

  1. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-domain -DpackageName=org.wosi.domain -Dversion=1.0
  2.  
  3. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-integration -DpackageName=org.wosi.integration -Dversion=1.0
  4.  
  5. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-services -DpackageName=org.wosi.services -Dversion=1.0
  6.  
  7. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-utils -DpackageName=org.wosi.utils -Dversion=1.0
  8.  
  9. # mind the fact that we use the maven-archetype-webapp to create the webapp!!!
  10. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=wosi -DartifactId=wosi-web -DpackageName=org.wosi.web -Dversion=1.0

Now if we look at the parent pom we'll see the different module entries:

  1. ...
  2.   <modules>
  3.     <module>wosi-domain</module>
  4.     <module>wosi-integration</module>
  5.     <module>wosi-services</module>
  6.     <module>wosi-utils</module>
  7.     <module>wosi-web</module>
  8.   </modules>
  9. ...

And all module poms will contain a reference to the parent:

  1. ...
  2.   <parent>
  3.     <artifactId>wosi</artifactId>
  4.     <groupId>wosi</groupId>
  5.     <version>1.0-SNAPSHOT</version>
  6.   </parent>
  7. ...

Now it's possible to compile or install all projects from the root directory of the project (mvn install) or you can compile any of the subprojects separately.

Useful commands to know are:

  • validate : validate the project is correct and all necessary information is available
  • compile : compile the source code of the project
  • test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package : take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test : process and deploy the package if necessary into an environment where integration tests can be run
  • verify : run any checks to verify the package is valid and meets quality criteria
  • install : install the package into the local repository, for use as a dependency in other projects locally
  • deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  • clean : cleans up artifacts created by prior builds
  • site : generates site documentation for this project

By default subprojects are not dependent on any of the other projects, it is however quite easy to create dependencies. For instance, say I want to have a dependency from my service layer on my integration layer. Just at it to the pom of the service project:

  1. <dependencies>
  2. ...
  3.   <dependency>
  4.     <artifactId>wosi-integration</artifactId> 
  5.     <groupId>wosi</groupId> 
  6.     <version>1.0</version>
  7.   </dependency>
  8. ...
  9. </dependencies>

Using the projects in Eclipse
It is possible to use the projects in Eclipse without any modifications (make sure to configure eclipse correctly though). Just run 'mvn eclipse:eclipse' from the root and import the generated projects using eclipse. If you would however like to use Eclipse's web tools to deploy webapplications a small modification to the parent pom is needed, ad a wtp configuration to the build/plugins section:

  1. <build>
  2.   <plugins>
  3.     <plugin>
  4.       <groupId>org.apache.maven.plugins</groupId>
  5.       <artifactId>maven-eclipse-plugin</artifactId>
  6.       <version>2.2</version>
  7.       <configuration>
  8.         <wtpversion>1.0</wtpversion>
  9.       </configuration>
  10.     </plugin>
  11.   </plugins>
  12. </build>

Now, after generating the eclipse projects again all web projects in the project should get configured for WST.

1 comment

Grails Video Plugin (flv transcoding & asset management)

During the years I've been involved in digital media related projects quite a bit. With the current dominance of flash video stuff has become a bit more straightforward; you don't have to support multiple proprietary formats in parallel anymore.

I met and read about quite a lot of people building their own mini-youtube on top of ffmpeg and red5.

Now, the folks at Cantina consulting wrapped all tools in a nice grails plugin licensed under the Apache License, version 2.0.

Current Features include:

  • Grails Service artifact that provides asynchronous (background) conversion of movies to the flash format
  • Storage of a digital master copy of the movie for later resampling of the video asset
  • Two options for Flash video players (JW FLV and Flowplayer) to display the uploaded flash video via a new GSP tag (see example below)
  • Streaming Flash video via HTTP streaming for large flash video
  • Small MVC application included to manage videos to serve as an administrative utility as well as a reference application that makes use of the Grails Video Service (VideoService) and options for the video players

According to the website support for Red5 (oss Flash media server) is planned.

My fingers are itching to give the plugin a go; have to be gentle to my eye though... the infection is almost, but not yet over! But I'll try to get it running soon!

15 comments

Ballooning away

During the weekend I attended a super cool company event: we went flying in a hot air balloon. I really enjoyed the experience!

We went up in Utrecht (near Ikea) and landed about 35km (measured in a straight line) in south east direction. During the flight I took about 160 pictures, I selected a couple of them for you:

The white line shows our flight pathA colleague of mine helping to inflate the balloonAlmost airbornUtrecht, see the Dom tower on the right?Abandoned factory?Knooppunt NieuwegeinHorses and a greenhousePeople playing soccerRiver LekA boatWhen we went higher we enjoyed a spectacular view of the sunWe landed in a nice muddy meadow… my shoes are still wet…

1 comment

Installing Leopard

No, don't worry... it's not me installing Leopard on my MacBook.

My wife managed to install Leopard without much help. Everything went smooth and Leopard runs really well. First impressions are (although it lacks Java 6) quite good; I really like spaces.

One of my colleagues was however less lucky. He ended up having to do the following:

OsX finger gymastics

Even worse then my old VCR! Cool ;-)

No comments