how to add sqljdbc_auth.dll to my maven project

2019-06-16 05:14发布

问题:

I am trying to establish connection to database. It's a simple project with using maven. I have problems with sqljdbc_auth.dll

I have added mssql jdbc driver and added a dependency in pom.xml

    <dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>4.0.0</version>
    </dependency>

This is my try block

    try {
        // Establish the connection. 
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setIntegratedSecurity(true);
        ds.setServerName("BUILDSRV");
        ds.setDatabaseName("master");
                    ds.setIntegratedSecurity(true);
        con = ds.getConnection();       
        }

and I have an error

    21.11.2012 18:07:04 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
    WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in       java.library.path
    com.microsoft.sqlserver.jdbc.SQLServerException:

I have my sqljdbc_auth.dll but I don't need to put it to my C:\windows\... I need to add it in my project from maven. How can I do this?

I tried to add it to the pom.xml but it doesn't work

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
              <execution>
                  <id>attach-artifacts</id>
                  <goals>
                      <goal>attach-artifact</goal>
                  </goals>
                  <configuration>
                      <artifacts>
                          <file>target</file>
                          <type>dll</type>
                      </artifacts>
                  </configuration>
              </execution>
    </executions>
    </plugin>
    </plugins>

I got another error while building

Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.1:attach-artifact (attach-artifacts) on project mavenproject1dbconnect: Unable to parse configuration of mojo org.codehaus.mojo:build-helper-maven-plugin:1.1:attach-artifact for parameter file: Cannot configure instance of org.codehaus.mojo.buildhelper.Artifact from target -> [Help 1]

回答1:

i dont think you need to use the maven-helper-plugin here. The doc here says you either need to install the dll or specify its path in the system variable java.library.path.

Look at http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated

UPDATE: make sure the dll is distributed along with your jar. If you are using a maven, Put the dll file in your src/main/resources folder. Then make sure the dll ends up outside the jar by excluding it from the package. Follow steps similar to ones outlined here. After this you should have your dll along with your built jar file in target directory

Then when you run the app pass system properties as commandline parameter java -Djava.library.path=..

If you prefer not to bother passing parameters via commandline -- you can extract current working directory using System.getProperty("user.dir")) and follow steps as stated here to set your java.library.path to current dir programatically in your main method