如何将本地jar添加到Maven项目

时间:2019-02-20 08:10:32

标签: java maven jdbc jar db2

我正在尝试一个小型的maven项目,并且尝试连接到db2数据库,因此需要com.ibm.db2.jcc.DB2Driver jar进行连接。

我尝试了许多来自Internet的方法,但仍然失败,这是我尝试过的一些方法:

  1. 在pom.xml中编辑

        <dependency>
            <groupId>com.ibm.db2</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>10.5.0.7</version>
            <scope>system</scope>
            <systemPath>
                /home/Workspace/book/springData/lib/db2jcc4-10.5.0.7.jar
            </systemPath>
        </dependency>
    
  2. 使用maven命令进行安装:

    mvn install:install-file -Dfile=/home/Workspace/book/springData/lib/db2jcc4-10.5.0.7.jar -DpomFile=/home/Workspace/book/springData/pom.xml
    

    mvn install:install-file -Dfile=/home/meow/Workspace/mbsb-cib/springData/lib/db2jcc4-10.5.0.7.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc4 -Dversion=10.5.0.7 -Dpackaging=jar -DgeneratePom=true
    

但是,我仍然无法加载JDBC驱动程序。

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.ibm.db2.jcc.DB2Driver]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 70 more
Caused by: java.lang.IllegalStateException: Could not load JDBC driver class [com.ibm.db2.jcc.DB2Driver]
    at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:150)
    at com.journaldev.spring.config.DataConfig.dataSource(DataConfig.java:52)
    at com.journaldev.spring.config.DataConfig$$EnhancerBySpringCGLIB$$46013fb.CGLIB$dataSource$0(<generated>)
    at com.journaldev.spring.config.DataConfig$$EnhancerBySpringCGLIB$$46013fb$$FastClassBySpringCGLIB$$8fec3ead.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at com.journaldev.spring.config.DataConfig$$EnhancerBySpringCGLIB$$46013fb.dataSource(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 71 more
Caused by: java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver from [Module "deployment.springData.war" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:412)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:400)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:147)
    ... 82 more

1 个答案:

答案 0 :(得分:0)

尝试从pom.xml的中央存储库添加引用吗?

<!-- https://mvnrepository.com/artifact/com.ibm.db2.jcc/db2jcc4 -->
<dependency>
     <groupId>com.ibm.db2.jcc</groupId>
     <artifactId>db2jcc4</artifactId>
     <version>10.1</version>
</dependency>

添加后,请使用Maven重建项目。

或者如果要从本地磁盘继续:click here to view the answer in stackoverflow itself