Do you want to get MS SQL JDBC Driver into a Maven War Build?

It’s a bit of a nuisance that Microsoft and Oracle do not publish their jar files for database connections in Maven central. Guys sort it out – it should not be that much of an issue.

I wanted to create a web application which connected to SQL server. The truth is that although there’s an open source alternative to the Microsoft SQL driver – jtds – it proved not to be as reliable as we wanted. So we needed the genuine article. We’re licensed to use it, so there’s no problem. Except it’s not in Maven and our project used a Maven build.

Here’s the right answer:

Install the Microsoft JDBC driver into your artifactory/Nexus with a suitable set of attributes and then your Maven build will pick it up from there

That wasn’t what I wanted. I wanted to have all the files for this build either within my source code repository or within the cloud. A middle-man repository for this project, risked the possibility that if the repository changed, and the project, which is not built more than once in a blue moon, wasn’t kept up to date, it would never build again. A more self-contained project was my desire and is more in keeping with best practice.

Advice number 2:

Have the jar in a source code repo somewhere and have a script that can install it in the nearest Maven repository

Not bad advice. Not what I wanted to do.

In the end, I used two tricks.

  1. Put the jar file in a local lib folder and provide the dependency from there (in fact the dependency wasn’t needed at all at compile time as JDBC generally works on class names at run time).
  2. Ensure that the contents of the lib folder were packaged into the war file by the war plug in.

Here’s how it looks:

Maven dependency – in case you need compile time access to the jar.


<!-- This is not really a maven dependency - it's been hacked into the WAR file via the WAR plug-in lower down
if you want it as part of a local deployment to a tomcat within your development environment, then add the jar
to your tomcat server directly as the Eclipse locally-produced deployment will not have this in it -->
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
</dependency>

War configuration.


<build>
<finalName>My Project</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<!-- Adds the jars from the lib folder into the war package
this does not work for local dev-deployments to a local tomcat
from Eclipse - you'll have to deploy these jars to your tomcat
directly. It _should_ work for deploying a war file to any tomcat -->
<resource>
<directory>lib</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

This genuinely works. It’s a decent workaround, but it’s a shame that Maven and Microsoft don’t work a bit more flexibly.

Advertisements

Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds.

Tagged with: ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: