Setting up a project with Hibernate Annotations using Maven2 is quite simple… or at least it should be. You should just have to set up your pom.xml file with the following dependencies:
<project>
...
<dependencies>
<dependency>
<groupid>hibernate</groupid>
<artifactid>hibernate-annotations</artifactid>
<version>3.2.1ga</version>
</dependency>
</dependencies>
...
</project>
And this is what I expected to start using Hibernate Annotations on my project. But if you try to compile your project you’ll get the following:
...
Missing:
----------
1) hibernate:hibernate-annotations:jar:3.2.1ga
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate-annotations \
-Dversion=3.2.1ga -Dpackaging=jar -Dfile=/path/to/file
Path to dependency:
1) ...
2) hibernate:hibernate-annotations:jar:3.2.1ga
…
So, Maven couldn’t resolve our dependency but at least gave us a hint on how to solve the problem. After downloading the Hibernate Annotations 3.2.1GA file and unzip it, we used the suggested command to solve the problem:
mvn install:install-file -DgroupId=hibernate -DartifactId=hibernate-annotations -Dversion=3.2.1ga -Dpackaging=jar -Dfile=hibernate-annotations.jar
After successfully installing the package, the project successfully compiled!
The problem is that usually we don’t want to use Hibernate Annotations by itself, we want to use the Java Persistence API (JPA) which is included on the javax.persistence package. So by including another dependency on our pom.xml like this:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>ejb</artifactId>
<version>3.0-public_review</version>
</dependency>
we get a similar error as we did when we tried to use Hibernate Annotations. Maven can’t resolve the dependency, but this time it even give us a hint where to dowloand the missing library:
Missing:
----------
1) javax.persistence:ejb:jar:3.0-public_review
Try downloading the file manually from:
http://prdownloads.sourceforge.net/hibernate/hibernate-annotations-3.1beta5.tar.gz?download
Then, install it using the command:
mvn install:install-file -DgroupId=javax.persistence -DartifactId=ejb \
-Dversion=3.0-public_review -Dpackaging=jar -Dfile=/path/to/file
If you notice, the URL where we can download the missing library is pointing to Hibernate Annotations! Since we already have downloaded Hibernate Annotations and it’s an earlier version than the suggested we just have to find out where the jar containing the javax.persistence is. And it’s on the ejb3-persistence.jar under the lib directory.
Issuing the installation command on the file, and you’re ready to go! Just type the following to install the jar on the local Maven repository:
mvn install:install-file -DgroupId=javax.persistence -DartifactId=ejb -Dversion=3.0-public_review -Dpackaging=jar -Dfile=ejb3-persistence.jar
That’s it! Not so fun using Maven when the dependencies aren’t resolved “automagically” like the most, but it still is a fantastic tool for managing your Java projects!