Springboot jar動態(tài)指定啟動類

需求: springboot jar 包java -jar 方式啟動竹捉,動態(tài)指定啟動類

知識點

  1.  spring-boot-maven-plugin maven插件配置是可以指定 layout 屬性的,不同的layout代表不同的spring launcher, 官方文檔描述如下:
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <version>2.1.6.RELEASE</version>
         <configuration>
             <mainClass>${start-class}</mainClass>
             <layout>ZIP</layout>
         </configuration>
         <executions>
             <execution>
                 <goals>
                     <goal>repackage</goal>
                 </goals>
             </execution>
         </executions>
     </plugin>
     The layout property defaults to a guess based on the archive type (jar or war). The following layouts are available:
     
     JAR: regular executable JAR layout.
     WAR: executable WAR layout. provided dependencies are placed in 
     WEB-INF/lib-provided to avoid any clash when the war is deployed in a servlet container.
     ZIP (alias to DIR): similar to the JAR layout using PropertiesLauncher.
     NONE: Bundle all dependencies and project resources. Does not bundle a bootstrap loader.
     
     解釋一下:
     JAR: 生成的MAINFEST.MF文件里的Main-class 為 JarLauncher
     
     WAR:會把依賴的項目放置在 WEB-INF/lib-provided 目錄下去避免用戶將war包單獨
     
     部署在其他servlet容器下沥阳,Main-class 沒看选酗,有興趣自己看一下生成的MAINFEST.MF文件
     
     ZIP: 重點,他會把默認(rèn)的jar打包方式的Launcher替換為PropertiesLauncher實現(xiàn)類,
     
     查閱官方文檔可知饺律,這個Launcher可以動態(tài)指定Start-class通過配置文件的方式。
     
     接下來會重點分析
     
     NONE: 不使用spring的launcher啟動jar包跺株,只會把項目的依賴打進jar包里复濒。
    
  2. lauout 為 zip 的打包方式運行機制:

Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible 
and more amenable to creating well-behaved OS-level services than a model based on executable jars.

Looks in various places for a properties file to extract loader settings, 
defaulting to loader.properties either on the current classpath or in the current working directory.
The name of the properties file can be changed by setting 
a System property loader.config.name
(e.g. -Dloader.config.name=foo will look for foo.properties.
If that file doesn't exist then tries loader.config.location 
(with allowed prefixes classpath: and file: or any valid URL). 
Once that file is located turns it into Properties and extracts 
optional values (which can also be provided overridden as System properties in case the file doesn't exist):

loader.path: a comma-separated list of directories 
(containing file resources and/or nested
archives in *.jar or *.zip or archives) or archives to append to the classpath. 
BOOT-INF/classes,BOOT-INF/lib in the application archive are always used

loader.main: the main method to delegate execution to once the class loader is set up.
No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

解釋一下: 就是說如果通過java -jar運行這個jar包,首先這個PropertiesLauncher會在classpath下或者當(dāng)前

工作目錄(執(zhí)行 java -jar 命令的目錄)下找尋

一個叫 load.properties 的配置文件乒省,

這個配置文件的名字呢芝薇,也可以通過修改系統(tǒng)屬性 loader.config.name 來指定,

比如說 -Dloader.config.name=start ,表示會讀取 start.properties 配置文件作儿,

也可以通過file://xxx.properties或classpath://xxx.properties 去指定其他路徑下的配置文件洛二。

然后呢,找到了配置文件它就會讀取里面的這兩個屬性:

loader.path

loader.name 

好了,loader.path 就可以動態(tài)的指定依賴的 jar路徑了晾嘶,建議如果沒有這個需求就不要動妓雾。

loader.name 就是需要我們添加的了,配置好我們需要使用的啟動類就行了

應(yīng)用:


目錄結(jié)構(gòu)圖

命令:

java -Dloader.config.name=start -jar ./demo.jar   #使用啟動的class
java -Dloader.config.name=stop -jar ./demo.jar   #使用關(guān)閉的class
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末垒迂,一起剝皮案震驚了整個濱河市械姻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌机断,老刑警劉巖楷拳,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異吏奸,居然都是意外死亡欢揖,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拧粪,你說我怎么就攤上這事腊瑟。” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我臭脓,道長,這世上最難降的妖魔是什么腹忽? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任来累,我火速辦了婚禮,結(jié)果婚禮上留凭,老公的妹妹穿的比我還像新娘佃扼。我一直安慰自己,他們只是感情好蔼夜,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布兼耀。 她就那樣靜靜地躺著,像睡著了一般求冷。 火紅的嫁衣襯著肌膚如雪瘤运。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天匠题,我揣著相機與錄音拯坟,去河邊找鬼。 笑死韭山,一個胖子當(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
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年掀抹,在試婚紗的時候發(fā)現(xiàn)自己被綠了虐拓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡渴丸,死狀恐怖侯嘀,靈堂內(nèi)的尸體忽然破棺而出另凌,到底是詐尸還是另有隱情谱轨,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布吠谢,位于F島的核電站土童,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏工坊。R本人自食惡果不足惜献汗,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望王污。 院中可真熱鬧罢吃,春花似錦、人聲如沸昭齐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阱驾。三九已至就谜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間里覆,已是汗流浹背丧荐。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留喧枷,地道東北人虹统。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓弓坞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親车荔。 傳聞我的和親對象是個殘疾皇子昼丑,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355