需求: springboot jar 包java -jar 方式啟動竹捉,動態(tài)指定啟動類
知識點
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包里复濒。
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