Maven does not directly support some libraries, like Microsoft’s SQL Server JDBC. This tutorial will show you how to add an external dependency to your local Maven repository. It assumes you have already installed Maven.

Download the JDBC driver for Microsoft SQL Server

  1. Visit the MSDN site for SQL Server and download the latest version of the JDBC driver for your operating system.
  2. Unzip the package
  3. Open a command prompt and switch into the expanded directory where the jar file is located.
  4. Execute the following command. Be sure to modify the jar file name and version as necessary:
  5. mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0
  6. You should see something similar to this:
  7. [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ standalone-pom ---
    [INFO] Installing /Users/claude/installers/JDBC/sqljdbc_4.0/enu/sqljdbc4.jar to /Users/claude/.m2/repository/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.jar
    [INFO] Installing /var/folders/c6/q1bdtq557kv54783p1g6cbsw0000gp/T/mvninstall1874482299687761721.pom to /Users/claude/.m2/repository/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.208s
    [INFO] Finished at: Tue Mar 13 14:07:31 EDT 2012
    [INFO] Final Memory: 3M/81M
    [INFO] ------------------------------------------------------------------------
    

    Modify your POM

    Include the new dependency by modifying your project’s pom.xml. Add the following dependency:

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>
    

    Save the pom.xml file and build the project to make sure no errors exist.

This article has 10 comments

  1. Róbert

    Thanks a lot for the howto, but you did not check that stuff for the pom.xml before pasting it, right? groupId and artifactId are spelled with a capital i.

  2. Claude

    You are welcome. And you are absolutely right, those properties were not in the correct case. They are correct now.

  3. Gurvinder

    My code was working fine with MySQL. I replace dependency for MSQL and made required change in rest of code for Connection URL etc.
    When I am trying to run my program, I am getting following error:

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
            at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
            at sun.security.util.SignatureFileVerifier.process(Unknown Source)
            at java.util.jar.JarVerifier.processEntry(Unknown Source)
            at java.util.jar.JarVerifier.update(Unknown Source)
            at java.util.jar.JarFile.initializeVerifier(Unknown Source)
            at java.util.jar.JarFile.getInputStream(Unknown Source)
            at sun.misc.URLClassPath$JarLoader$2.getInputStream(Unknown Source)
            at sun.misc.Resource.cachedInputStream(Unknown Source)
            at sun.misc.Resource.getByteBuffer(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: org.feed.JAXBUnmarshaller. Program will exit.
    
  4. Claude

    It doesn’t look like your exception relates to the JDBC change. org.feed.JAXBUnmarshaller could not be found.

  5. Mejnar

    I’m not sure this is the way to do it. This will add those artifacts to your local workstation’s repository. I would say only to use the method described here as your absolutely LAST resort.

    Maven is all about consistency. If there’s more than one developer working on the project he/she should get the same downloaded artifacts as you do.

    The correct, but somewhat cumbersome, solution is to upload such artifacts to your company-wide Maven repository where there’s already a place for that called “3rd part library repo”. Yes, this requires that you run such a company-wide Maven Repository Manager. There’s Archiva, Artifactory and Nexus to choose from.

  6. Claude

    The ideal development environment, especially a corporate one with many developers, requires an internal repository for artifacts not available on the central repository.

    This example is a solution for those lone developers who need to add a dependency that is not open source and don’t have a corporate repository.

  7. Krishna Kumar Chourasiya

    I have successfully executed command to save sqljdbc4.jar file into .m2 (local repository). But when I was trying to add dependency at the pom it is still showing me “Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0:compile”. 

    Any help greatly appreciated.

    Thanks,
    Krishna

  8. Claude

    There isn’t a whole lot of room for error when loading the driver in Maven or using it in your project’s POM. You need to make sure the file name, groupId, artifactId and version are correct for your file.

    Feel free to reply with the installation command you used, its output and your POM dependency for review.

Comments are closed.