Monday, 11 July 2011

Maven Settings for the Hibernate JSR 303 Bean Validator

The Guy’s at Spring, when talking about their MVC examples, always bang on about JSR 303 - Bean Validation, and why not, it’s an integral part of their validation strategy replacing the older inheritance based validation of deprecated classes such as SimpleFormController. The reference implementation for JSR 303 has been written by Hibernate and is available from the JBoss repository. To get hold of the validation JAR files you’ll need to either add the following to your settings.xml:

<repositories>
   <repository>
    <id>jboss-public-repository-group</id>
    <url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    </repository>
  </repositories>

or add a repository entry into your individual POM files:

<repositories>
        <repository>
            <id>jboss-public-repository</id>
            <url>https://repository.jboss.org/nexus/content/repositories/public</url>
        </repository>
    </repositories>

If you’re diving straight in to using the Hibernate Reference Validator, you’ll need to add the following dependencies into your project’s POM file:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.CR1</version>
    </dependency>    
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4jVersion}</version>
        <scope>runtime</scope>
    </dependency>         
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4jVersion}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>${slf4jVersion}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4jVersion}</version>
        <scope>runtime</scope>
    </dependency>

where ${slf4jVersion} is currently 1.6.1

If you’re needing some additional information, then consider looking at the Hibernate getting started page. This takes you through setting up a simple project that demonstrates the validation annotations. This is achieved by using the following Maven archetype:

mvn archetype:generate -DarchetypeGroupId=org.hibernate \
   -DarchetypeArtifactId=hibernate-validator-quickstart-archetype \
   -DarchetypeVersion=4.2.0.CR1 \
   -DarchetypeRepository=http://repository.jboss.org/nexus/content/groups/public-jboss/ \
   -DgroupId=com.mycompany \
   -DartifactId=hv-quickstart

This needs to be all on one line and will generate a complete and working sample project, but it does add some useless junk to the POM file, which, for clarity, you may wish to remove:

  • Remove the <repositories> entry if you’ve modified your settings.xml file.
  • Remove the maven-archetype-plugin entry.
  • Remove the maven-deploy-plugin as this is used for deploying to remote repositories, which you probably won’t do with the sample code.
  • If you’re using Java 6, then remove jaxb-api and jaxb-impl dependencies.
  • You could also re-parent the project to fit in with your own project structure, giving rise to the possiblity of removing other dependencies and simplifying your POM even further.

No comments: