Friday, December 6, 2013

Add Microsoft SQL JDBC driver to Maven

Found out today that Maven does not directly support some libraries, like Microsoft’s SQL Server JDBC. The reason for that is that this library is not open source and can be stored in global repository. Anyway, i recently was wondering how to add a library to local maven repository to use it in my automation projects. Fast search in Google got me to this blog post. It's really easy :

  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:

mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0
Then you'll get this result :
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom
---
[INFO] Installing C:\sqljdbc4.jar to C:\IDEConfigs\.m2\repository\com\microsoft\
sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar
[INFO] Installing C:\Users\AF4C0~1.ZAK\AppData\Local\Temp\mvninstall797028335566
232276.pom to C:\IDEConfigs\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\
sqljdbc4-4.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.925s
[INFO] Finished at: Fri Dec 06 15:59:40 CET 2013
[INFO] Final Memory: 5M/121M
[INFO] ------------------------------------------------------------------------
Now you have this library in your local repository. If you want to use it in your project, then just add dependancy to your pom.xml file.

    com.microsoft.sqlserver
    sqljdbc4
    4.0

That's it. Now you can create connection to SQL Server in your tests

3 comments:

  1. After doing the same it is showing .........

    Error reading file C:\Users\nitin.singh\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar
    C:\Users\nitin.singh\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar (The system cannot find the file specified)


    can u please suggest me something to resolve that one...

    ReplyDelete
  2. The problem might be in your user name - it contains dot and maybe that corrupts the path. Try to put maven home into another folder that doesn't contains dot's, special symbols in the path and try again.

    ReplyDelete
  3. Hi, I guess this is required.


        com.microsoft.sqlserver
        sqljdbc4
        4.0

    ReplyDelete