autodeploy-springboot realizes automatic loading of external jars

Relying on Spring boot's PropertiesLauncher, autodeploy realizes automatic scanning of all jars in the jars subdirectory.

The specific implementation is as follows.

pom.xml specifies load-path and Spring boot packaging method

<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>

key point

  • The layout is in ZIP mode, don’t be confused by zip, the production is still jar files, not zip files
  • Specify Loader-Path as file:./jars

Start method

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

The above -Dloader.debug=true production mode does not need to be used, you can see the following output:

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

You can also specify load-path yourself

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

Finally, for the uberjar packaged in non-zip mode, you can use the following method to run

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

Guess you like

Origin blog.csdn.net/weixin_40455124/article/details/113777664