Start Spring Boot with classpath and class

There is an easy way to start a Spring Boot application by just calling

java -jar MyApplication-0.0.1.jar

This requires all dependencies to be bundled into the jar file and makes the jar file “fat”. That’s also the key word to search for when trying to create a complete jar.

But there is another way.

For example if I want to allow users to exchange the logging framework or allow the usage of different database drivers the dependencies can be exported into a separate directory using the following lines in the “build.gradle” build definition:

task copyRuntimeLibs(type: Copy, group: 'build', description: 'Export dependencies.') {
	into "build/deps"
	from configurations.runtimeClasspath
}

Executing this task will copy all the runtime dependencies, like Spring Boot and transient libraries, into a folder called “build/deps”.

I can then start my application from the project directory with the following command:

java -cp "build/libs/*;build/deps/*" my.application.MainClass

Hope this helps somebody.