spring boot项目没有mainClass如何实现打包运行
这篇文章主要介绍了spring boot项目没有mainClass如何实现打包运行,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
目录
springboot项目没有mainClass实现打包运行
框架starter运行类如下
业务代码模块如何配置?
使用maven打包指定mainClass
springboot项目没有mainClass实现打包运行
项目分为两个部分,一个是业务代码模块,一个是框架模块,运行class放在框架部分,那业务代码如何配置才能正常运行?
框架starter运行类如下
1 2 3 4 | @SpringBootApplication ( scanBasePackages = { "com" } ) public class Starter { |
@SpringBootApplication只会扫描@SpringBootApplication注解标记类包下及其子包的类(特定注解标记,比如说@Controller,@Service,@Component,@Configuration和@Bean注解等等)纳入到spring容器,如果不在@SpringBootApplication注解标记类相同包下及其子包的类,需要我们去配置一下扫包路径,即scanBasePackages 。
业务代码模块如何配置?
借助spring-boot-maven-plugin配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < mainClass >com.core.Starter</ mainClass > < layout >ZIP</ layout > < classifier >exec</ classifier > < includeSystemScope >true</ includeSystemScope > </ configuration > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > |
mainClass最终会在jar包里的MANIFEST.MF文件中指定
1 | Start-Class: com.core.Starter |
Spring Boot Maven plugin的5个Goals
spring-boot:repackage
,默认goal。在mvn package之后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.originspring-boot:run
,运行Spring Boot应用spring-boot:start
,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理spring-boot:stop
,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理spring-boot:build-info
,生成Actuator使用的构建信息文件build-info.properties
layout属性的值可以如下:
JAR
,即通常的可执行jarMain-Class
: org.springframework.boot.loader.JarLauncherWAR
,即通常的可执行war,需要的servlet容器依赖位于WEB-INF/lib-providedMain-Class
: org.springframework.boot.loader.warLauncherZIP
,即DIR,类似于JARMain-Class
: org.springframework.boot.loader.PropertiesLauncherMODULE
,将所有的依赖库打包(scope为provided的除外),但是不打包Spring Boot的任何LauncherNONE
,将所有的依赖库打包,但是不打包Spring Boot的任何Launcher
classifier
生成可执行jar包的后缀名
includeSystemScope
将system范围依赖包也打进来,如
1 2 3 4 5 6 7 8 9 | < dependency > < groupId >com.alipay</ groupId > < artifactId >alipay-sdk-java</ artifactId > < version >1.1</ version > < scope >system</ scope > < systemPath > ${project.basedir}/local_repo/com/alipay/alipay-sdk-java/1.1/alipay-sdk-java-1.1.jar </ systemPath > </ dependency > |
使用maven打包指定mainClass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < mainClass >com.xxx.XxxApplication</ mainClass > </ configuration > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > |
以上为个人经验,希望能给大家一个参考
原文链接:https://blog.csdn.net/wm5920/article/details/85063894