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

猜你喜欢

转载自blog.csdn.net/weixin_40455124/article/details/113777664
今日推荐