我有多模块Java项目。一个模块(通用)中的数据库逻辑,另一模块(restapi)中的restapi Web应用程序。我在restapi模块的通用模块中调用方法有问题。无法从通用模块pom解决mysql依赖关系。
这是我的pom.xml
根
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>qwerty.project</groupId>
<artifactId>project</artifactId>
<version>0.1</version>
<modules>
<module>common</module>
<module>restapi</module>
</modules>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
用于通用模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>qwerty.project</groupId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>common</name>
<packaging>jar</packaging>
<artifactId>qwerty.common</artifactId>
<dependencies>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</project>
这是用于restapi模块的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>qwerty.project</groupId>
<version>0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rest-api</artifactId>
<dependencies>
<dependency>
<groupId>qwerty.project</groupId>
<artifactId>qwerty.common</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在我的restapi控制器中,我将从通用模块中注入Dao。
@RestController
public class VideoController {
@Autowired
VideoDAO videoDAO;
@RequestMapping("/video")
public String getVideo() throws SQLException {
List<String> links = videoDAO.doSome();
return links.get(0);
}
}
我将进行mvn全新安装 如果我从模块restapi启动spring boot app,我在日志中看到通用模块的c3p0已成功初始化。但是mysql不是,-NoClassFoundException。为什么会这样呢?
2018-10-12 11:34:34.060 WARN 10160 --- [HelperThread-#2] c.m.v2.c3p0.DriverManagerDataSource : Could not load driverClass com.mysql.cj.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_144]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_144]
在资源的设置文件的通用模块中声明了jdbc类
c3p0.driverClass=com.mysql.cj.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/Some
c3p0.user=***
c3p0.password=***
c3p0.initialPoolSize=5
c3p0.minPoolSize=5
...
我注意到,如果将mysql依赖项转移到root pom文件中,则项目将开始正常工作。 我认为,但这不是一个好主意,因为从逻辑上讲,mysql应该位于通用模块中。 我将为您的建议而感激
更新 restapi模块的依赖树
qwerty.project:rest-api:jar:0.1
[INFO] +- qwerty.project:qwerty.common:jar:0.1:compile
[INFO] | +- com.mchange:c3p0:jar:0.9.5.2:compile
[INFO] | | \- com.mchange:mchange-commons-java:jar:0.2.11:compile
[INFO] | \- mysql:mysql-connector-java:jar:5.1.43:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.6.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.6.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:1.5.6.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.6.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.6.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.6.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.16:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.16:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.16:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.3.5.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.9:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.9:compile
[INFO] | +- org.springframework:spring-web:jar:4.3.10.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.3.10.RELEASE:compile
[INFO] | | +- org.springframework:spring-beans:jar:4.3.10.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:4.3.10.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.3.10.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:4.3.10.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.6.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:1.5.6.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.6.RELEASE:test
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.assertj:assertj-core:jar:2.6.0:test
[INFO] | +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] | | \- org.objenesis:objenesis:jar:2.1:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:4.3.10.RELEASE:compile
[INFO] | \- org.springframework:spring-test:jar:4.3.10.RELEASE:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.2.0:test
[INFO] | +- net.minidev:json-smart:jar:2.2.1:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.1:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.3:test
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.projectlombok:lombok:jar:1.16.20:compile
[INFO] \- log4j:log4j:jar:1.2.17:compile
以及常见的
[INFO] qwerty.project:qwerty.common:jar:0.1
[INFO] +- com.mchange:c3p0:jar:0.9.5.2:compile
[INFO] | \- com.mchange:mchange-commons-java:jar:0.2.11:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.12:compile
[INFO] | \- com.google.protobuf:protobuf-java:jar:2.6.0:compile
[INFO] +- org.projectlombok:lombok:jar:1.16.20:compile
[INFO] \- log4j:log4j:jar:1.2.17:compile
答案 0 :(得分:0)
问题与Maven无关。您包含的依赖项根本不包含类com.mysql.cj.jdbc.Driver
。您是说com.mysql.jdbc.Driver
吗?还是需要更新版本的连接器?
-编辑-
好。刚刚意识到,您有两个不同的版本。因此,这是一个Maven问题。 我假设其中一个父项目管理着对该旧项目的依赖关系版本。
解决方案:在根pom中将依赖项管理置于所需的版本中。