天天看點

autodeploy-springboot 實作自動加載外部jar

依托Spring boot 的PropertiesLauncher,autodeploy實作了自動掃描jars子目錄下的所有jar。

具體實作方式如下。

pom.xml指定load-path及Spring boot 打包方式

<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<excludes>
						<exclude>**/application-dev.properties</exclude>
						<exclude>**/env.properties</exclude>
					</excludes>
					<archive>

						<manifestEntries>
							<Loader-Path>file:./jars</Loader-Path>

							<mainClass>${start-class}</mainClass>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>

					<mainClass>${start-class}</mainClass>
					<layout>ZIP</layout>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
           

關鍵點

  • layout 為ZIP模式,不要被zip迷惑,生産的還是jar檔案,不是zip檔案
  • 指定Loader-Path 為file:./jars

啟動方法

java   -Dloader.debug=true    -jar  elasticjob-autodeploy-0.2.jar --spring.profiles.active=dev   --spring.config.location=file:./jars/application-dev.properties
           

以上-Dloader.debug=true 生産模式無需使用,可以看到如下輸出:

Trying file: D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target/loader.properties
Not found: file:D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target/loader.properties
Trying classpath: /loader.properties
Not found: classpath:loader.properties
Trying classpath: /BOOT-INF/classes/loader.properties
Not found: classpath:BOOT-INF/classes/loader.properties
Property 'Loader-Path' from archive manifest: file:./jars
Nested archive paths: [file:./jars/]
Adding classpath entries from D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target\jars
Adding classpath entries from nested jars/
Property 'Start-Class' from archive manifest: elasticjob.operation.simplejob.JobChangeListenerMain
           

也可以自己指定load-path

java   -Dloader.debug=true -Dloader.path=../testjar   -jar  elasticjob-autodeploy-0.2.jar --spring.profiles.active=dev   --spring.config.location=file:./jars/application-dev.properties
Trying file: D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target/loader.properties
Not found: file:D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target/loader.properties
Trying classpath: /loader.properties
Not found: classpath:loader.properties
Trying classpath: /BOOT-INF/classes/loader.properties
Not found: classpath:BOOT-INF/classes/loader.properties
Property 'loader.path' from environment: ../testjar
Nested archive paths: [../testjar/]
Adding classpath entries from D:\mydocuments\as4_code\elasticjob\elasticjob-operator\target\..\testjar
Adding classpath entries from nested ../testjar/
Property 'Start-Class' from archive manifest: elasticjob.operation.simplejob.JobChangeListenerMain
           

最後附加一下,對于非zip模式打包的uberjar,可以使用以下方式運作

java -cp elasticjob-autodeploy-0.2.jar;elasticjob-autodeploy-example-0.0.1-SNAPSHOT.jar -Dloader.debug=true -Dloader.path=..\testjar -Dloader.main=elasticjob.operation.simplejob.JobChangeListenerMain   -Dloader.config.name=application-dev org.springframework.boot.loader.PropertiesLauncher --spring.profiles.active=dev   --spring.config.location=file:./application-dev.properties