spring Boot项目:多个启动类,如何选择启动?
在spring boot项目开发中,有时会包含多个启动类,例如主应用和用于特定功能的子应用。打包成单个Jar包后,如何指定启动哪个启动类呢?
本文解决Spring Boot项目包含两个启动类,打包成Jar包后,如何选择启动类的问题。
关键在于使用Spring Boot maven插件(spring-boot-maven-plugin)的mainClass参数。该插件负责将Spring Boot项目打包成可执行Jar包。通过配置mainClass,指定程序启动时使用的主类。
在pom.xml文件中,找到spring-boot-maven-plugin插件配置,在
例如,假设两个启动类分别为com.example.app1.App1和com.example.app2.App2,要启动App1,则pom.xml配置如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.app1.App1</mainClass> </configuration> </plugin> </plugins> </build>
重新打包项目后,运行生成的Jar包,将启动指定的应用。要启动App2,只需将mainClass的值修改为com.example.app2.App2即可。 这样就能灵活控制启动哪个应用。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END