SpringBoot项目eclipse运行正常maven install打包启动后报错ClassNotFoundException

时间:2022-11-21 16:54:32

parent的pom.xml

    <groupId>cn.licoy</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
<packaging>pom</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent> <modules>
<module>../rest</module>
<module>../service</module>
</modules> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.licoy.rest.RestApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

直接在IDEA里面运行SpringBoot启动类是可以正常访问的,但是使用mvn install打包后,报出如下错误:

java.lang.ClassNotFoundException: cn.licoy.service.entity.User
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_131]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_131]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[rest-0.1.jar:0.1]

其中,Springboot启动包是rest包,当中引用了service包中的User类,在打包之后的rest.jar里面lib目录下有service.jar,但是一访问就找不到类

解决方法:要给被依赖的module的pom.xml中添加

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>