Sprnig Boot 之路[3]--打包成可運行的jar

專題簡介

SpringBoot之路專題是一個記錄本人在使用Spring和SpringBoot相關技術中所遇到的問題和要解決的問題。每用到一處知識點,就會把這處知識補充到Github一個對應的分支上。會以專題的方式,力爭每一篇博客,由淺入深,把每個知識點講解到實戰級別,并且分析Spring源碼。整個項目會以一個開發一個博客系統為最終目標,每一個分支都記錄著一步一步搭建的過程。與大家分享,代碼會同步發布到這里

簡介

spring boot最大的特點之一,就是整個項目不需要像以前一樣需要容器環境,需要一堆各種配置。SpringBoot的項目可以直接把所有需要的依賴以一個jar包的形式打包,而運行則直接一個java -jar命令即可。這大大簡化了發布和部署的流程。也更加擁抱微服務、容器化、彈性擴容等等云計算時代的技術和概念。

使用maven打包

如果想要使用maven進行打包,并且可以直接使用java -jar XXX.jar來運行,如果你只有一個添加了@SpringBootApplication 的類,那么是不需要任何配置的。直接使用mvn package就可以打包出一個可運行的jar包。結果如下圖:

mvn package結果

如果你有多個入口類

如果,你有多個添加了@SpringBootApplication注解的入口類,然后在運行mvn package時,會爆出如下的錯誤:


[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.604s
[INFO] Finished at: Mon Aug 15 18:14:25 CST 2016
[INFO] Final Memory: 22M/226M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:repackage (default) on project beenoisy-spring-boot-way: 
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:
repackage failed: Unable to find a single main class from the following candidates 

[
com.beenoisy.springboot.way.BeenoisySpringBootWayApplication, 
com.beenoisy.springboot.way.TestConflict
] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.


大體的意思是告訴你,沒辦法找到唯一的一個入口類。

所以,需要在pom.xml文件中加入<start-class>配置:


    <properties>
        <start-class>com.beenoisy.springboot.way.BeenoisySpringBootWayApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

關于start-class,spring boot官方手冊是這么說明的:

The plugin rewrites your manifest, and in particular it manages the Main-Class and Start-Class entries, so if the defaults don't work you have to configure those there (not in the jar plugin). The Main-Class in the manifest is actually controlled by the layout property of the boot plugin, e.g.

<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.RELEASE</version>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>


所以,當你的默認方式不好用的時候,在properties中,加入一個start-class的屬性,用于告訴spring boot maven plugin哪個類是入口類即可。

# 運行
直接使用java -jar XXX.jar即可運行這個程序。

beenoisy-spring-boot-way BeeNoisy$ java -jar target/beenoisy-spring-boot-way-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
' |____| .
|| ||| |_, | / / / /
=========|
|==============|/=////
:: Spring Boot :: (v1.4.0.RELEASE)

2016-08-15 18:24:23.388 INFO 62643 --- [ main] c.b.s.w.BeenoisySpringBootWayApplication : Starting BeenoisySpringBootWayApplication v0.0.1-SNAPSHOT on bogon with PID 62643 (/Users/didi/IdeaProjects/beenoisy-spring-boot-way/target/beenoisy-spring-boot-way-0.0.1-SNAPSHOT.jar started by BeeNoisy in /Users/didi/IdeaProjects/beenoisy-spring-boot-way)
2016-08-15 18:24:23.406 INFO 62643 --- [ main] c.b.s.w.BeenoisySpringBootWayApplication : No active profile set, falling back to default profiles: default
2016-08-15 18:24:23.560 INFO 62643 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Mon Aug 15 18:24:23 CST 2016]; root of context hierarchy
2016-08-15 18:24:26.424 INFO 62643 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-08-15 18:24:26.453 INFO 62643 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-08-15 18:24:26.458 INFO 62643 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-08-15 18:24:26.683 INFO 62643 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-08-15 18:24:26.685 INFO 62643 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3130 ms
2016-08-15 18:24:26.963 INFO 62643 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-08-15 18:24:26.978 INFO 62643 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/]
2016-08-15 18:24:26.979 INFO 62643 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/
]
2016-08-15 18:24:26.979 INFO 62643 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/]
2016-08-15 18:24:26.979 INFO 62643 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/
]
2016-08-15 18:24:27.517 INFO 62643 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Mon Aug 15 18:24:23 CST 2016]; root of context hierarchy
2016-08-15 18:24:27.658 INFO 62643 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.beenoisy.springboot.way.controller.IndexController.index()
2016-08-15 18:24:27.664 INFO 62643 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-08-15 18:24:27.665 INFO 62643 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-08-15 18:24:27.719 INFO 62643 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-15 18:24:27.720 INFO 62643 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/
] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-15 18:24:27.805 INFO 62643 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-15 18:24:28.019 INFO 62643 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-08-15 18:24:28.158 INFO 62643 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-08-15 18:24:28.167 INFO 62643 --- [ main] c.b.s.w.BeenoisySpringBootWayApplication : Started BeenoisySpringBootWayApplication in 6.152 seconds (JVM running for 6.955)


參考資料:
>http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html
http://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar
http://docs.spring.io/spring-boot/docs/1.2.5.RELEASE/reference/html/getting-started-first-application.html#getting-started-first-application-executable-jar
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容