SpringBoot 打包jar运行时提示没有主清单属性问题
关于springboot打包成jar文件,然后运行java -jar命令时没有主清单属性问题,网上目前有好多种解决方案,而最尴尬的地方在于,不怎么好使,今天来搞两种能用的。
第一种方案
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>repackage</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后
# 切换到子项目然后执行打包命令
$ cd test-package & mvn package
第二种解决方案
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
巧用IDEA,先选中该项目,然后
最终结果
$ java -jar test-package-1.0-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
2020-09-28 16:58:08.980 INFO 23124 --- [ main] test.PackageApplication : Starting PackageApplication on DESKTOP-4G39J2B with PID 23124 (\demo-project\test-package\target\test-package-1.0-SNAPSH
OT.jar started by idea in \demo-project\test-package\target)
2020-09-28 16:58:08.983 INFO 23124 --- [ main] test.PackageApplication : No active profile set, falling back to default profiles: default
2020-09-28 16:58:09.937 INFO 23124 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-09-28 16:58:09.951 INFO 23124 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-28 16:58:09.951 INFO 23124 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-09-28 16:58:10.017 INFO 23124 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-28 16:58:10.017 INFO 23124 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 977 ms
2020-09-28 16:58:10.172 INFO 23124 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-28 16:58:10.323 INFO 23124 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-28 16:58:10.331 INFO 23124 --- [ main] test.PackageApplication : Started PackageApplication in 1.7 seconds (JVM running for 2.068)
2020-09-28 16:59:52.704 INFO 23124 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
其实基本原理相同,都是先调用package然后调用了spring-boot:repackage的plugin,经过实测,这两种解决方案确实可以正确完成springboot的打包。