Sprnig Boot 之路[3]--打包成可運(yùn)行的jar

專題簡介

SpringBoot之路專題是一個記錄本人在使用Spring和SpringBoot相關(guān)技術(shù)中所遇到的問題和要解決的問題乒省。每用到一處知識點(diǎn),就會把這處知識補(bǔ)充到Github一個對應(yīng)的分支上隆判。會以專題的方式,力爭每一篇博客,由淺入深佛嬉,把每個知識點(diǎn)講解到實(shí)戰(zhàn)級別夜只,并且分析Spring源碼垒在。整個項(xiàng)目會以一個開發(fā)一個博客系統(tǒng)為最終目標(biāo),每一個分支都記錄著一步一步搭建的過程扔亥。與大家分享场躯,代碼會同步發(fā)布到這里

簡介

spring boot最大的特點(diǎn)之一旅挤,就是整個項(xiàng)目不需要像以前一樣需要容器環(huán)境踢关,需要一堆各種配置。SpringBoot的項(xiàng)目可以直接把所有需要的依賴以一個jar包的形式打包粘茄,而運(yùn)行則直接一個java -jar命令即可签舞。這大大簡化了發(fā)布和部署的流程秕脓。也更加擁抱微服務(wù)、容器化儒搭、彈性擴(kuò)容等等云計(jì)算時代的技術(shù)和概念吠架。

使用maven打包

如果想要使用maven進(jìn)行打包,并且可以直接使用java -jar XXX.jar來運(yùn)行搂鲫,如果你只有一個添加了@SpringBootApplication 的類傍药,那么是不需要任何配置的。直接使用mvn package就可以打包出一個可運(yùn)行的jar包魂仍。結(jié)果如下圖:

mvn package結(jié)果

如果你有多個入口類

如果怔檩,你有多個添加了@SpringBootApplication注解的入口類,然后在運(yùn)行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>

關(guān)于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>


所以,當(dāng)你的默認(rèn)方式不好用的時候锯岖,在properties中介袜,加入一個start-class的屬性,用于告訴spring boot maven plugin哪個類是入口類即可出吹。

# 運(yùn)行
直接使用java -jar XXX.jar即可運(yùn)行這個程序遇伞。

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市捶牢,隨后出現(xiàn)的幾起案子鸠珠,更是在濱河造成了極大的恐慌,老刑警劉巖秋麸,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渐排,死亡現(xiàn)場離奇詭異,居然都是意外死亡灸蟆,警方通過查閱死者的電腦和手機(jī)驯耻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炒考,“玉大人可缚,你說我怎么就攤上這事≌啵” “怎么了帘靡?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長杏慰。 經(jīng)常有香客問我测柠,道長炼鞠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任轰胁,我火速辦了婚禮谒主,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赃阀。我一直安慰自己霎肯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布榛斯。 她就那樣靜靜地躺著观游,像睡著了一般。 火紅的嫁衣襯著肌膚如雪驮俗。 梳的紋絲不亂的頭發(fā)上懂缕,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機(jī)與錄音王凑,去河邊找鬼搪柑。 笑死,一個胖子當(dāng)著我的面吹牛索烹,可吹牛的內(nèi)容都是我干的工碾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼百姓,長吁一口氣:“原來是場噩夢啊……” “哼渊额!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起垒拢,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤旬迹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后子库,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舱权,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年仑嗅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片张症。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡仓技,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出俗他,到底是詐尸還是另有隱情脖捻,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布兆衅,位于F島的核電站地沮,受9級特大地震影響嗜浮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜摩疑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一危融、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雷袋,春花似錦吉殃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鸠删,卻和暖如春抱完,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背刃泡。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工巧娱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人捅僵。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓家卖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親庙楚。 傳聞我的和親對象是個殘疾皇子上荡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容