External library folder for Spring Boot

2020-02-10 13:08发布

问题:

I wonder how to externalize all jdbc drivers for my Spring Boot applications, I would not like to insert jdbc drivers into my fat jar once the application is built.

Is there any way to set a java vm parameter, informing which external folder should be included with jar execution? Or may else exists some kind of spring property for it.

For example:

java  -DLib=file:\\\c:\Drivers -jar sample.jar

回答1:

See the documentation about PropertiesLauncher:

  • https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features

Looks like you can use the loader.path property to define a lib folder location, containing jars to load - in fact the lib folder (inline with the jar) is the default location:

loader.path (if empty) defaults to lib (meaning a local directory or a nested one if running from an archive)



回答2:

Isn't that what the classpath is for? As long as the jdbc driver jars are on the classpath, this should work. Something like this:

java -classpath /path/to/driver/jar -jar application.jar

You could also set the CLASSPATH environment variable for the same.