Friday, 9 September 2011

Maven and Oracle's JDBC driver

Due to licensing restrictions, the Oracle JDBC driver jar will not be in the repository. The 
pom is there, and it's useful to add that to the conguration:

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc14</artifactId>
	<version>10.2.0.4.0</version>
</dependency>

After that is done, you will then see an error message from mvn about "Could not find artifact com.oracle:ojdbc14:jar:10.2.0.4.0 ...". To resolve that, you need to manually add the JDBC driver jar file to your local .m2 repostiory:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 \
-Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=/path/to/ojdbc14.jar


PS: The ojdbc14.jar file will be on your database server in the ${ORACLE_HOME}/jdbc/lib/ directory.

Maven and Hibernate and Unit Tests (example using the JDK logger)

While attempting to run some unit tests that rely on the H2 in-memory database, I saw this failure:
...
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
	at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
	at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
	at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
	at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
	at org.hibernate.cfg.Configuration.(Configuration.java:165)
	[snip]
	... 74 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
	... 82 more

The maven dependency:tree target shows:
[INFO] +- org.hibernate:hibernate-core:jar:3.5.0-Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.5.8:compile

The org.slf4j:slf4j-api.jar provides the interface, but not a logging implementation. To add the JDK logger implementation, and additional dependency is required:
<dependency>
	<groupid>org.slf4j</groupid>
	<artifactid>slf4j-jdk14</artifactid>
	<version>1.5.8</version>
	<scope>test</scope>
</dependency>

(version 1.5.8 is required to match the api dependency in the hibernate 3.5.0-Final libraries.