简体   繁体   中英

how to run Spring Boot application with an extra file on classpath without modifying the application (the xxx.jar)

I would like to run a Spring Boot application with an extra file (happens to be a css) being on the classpath. I can't touch the jar (the application itself). I can only modify the start script.

I've received a start script with the application::

#! /bin/sh
commandline="java -jar xxx-1.0.0.jar"
commandline="$commandline --spring.config.location=../config/xxx.properties"
commandline="$commandline --logging.config=../config/log4j2.xml"
$commandline

My naive first try was adding a folder with -cp and put the file into that folder. However that is not working because -cp and -jar is not compatible ( nice explanation here: Differences between "java -cp" and "java -jar"? )

Then I found suggestions for using PropertiesLauncher + loader.path which can be seen as a replacement for classpath on the command line ( https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features ) . However to use PropertiesLauncher the examples suggested modifying the pom, which I can't do ( Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar? )

What I can do is modifying the shell script.

How should I (if it is possible at all) put the extra file onto the classpath without modifying the Spring Boot application?

This also works for me:

#! /bin/sh
commandline="java -cp ../css:xxx-1.0.0.jar org.springframework.boot.loader.JarLauncher"
commandline="$commandline --Spring.config.location=../config/xxx.properties"
commandline="$commandline --logging.config=../config/log4j2.xml"
$commandline

In my case this seems to be simpler than using the PropertiesLauncher.

I've found this article, which shows how to use PropertiesLauncher without modifying the application (without modifying the pom.xml): https://mash213.wordpress.com/2017/01/05/hack-how-2-add-jars-2-springboot-classpath-with-jarlauncher/

So with the above I can add an extra folder to the classpath and it's indeed working:

#! /bin/sh
commandline="java -Dloader.path=../css -cp xxx-1.0.0.jar org.springframework.boot.loader.PropertiesLauncher"
commandline="$commandline --spring.config.location=../config/xxx.properties"
commandline="$commandline --logging.config=../config/log4j2.xml"
$commandline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM