Java – PropertiesLauncher with Spring Boot 2 (and running WAR archive)

PropertiesLauncher with Spring Boot 2 (and running WAR archive)… here is a solution to the problem.

PropertiesLauncher with Spring Boot 2 (and running WAR archive)

I’m creating a tomcat/jsp-based WAR spring boot 2.0.4 executable that works fine when using only the bootWar gradle target. But now I want to load a local external JAR and WarLauncher doesn’t support this (is there any reason?). )。 So I switched to PropertiesLauncher:

bootWar {
    enabled = true
    manifest {
        attributes 'Main-Class': 'org.springframework.boot.loader.PropertiesLauncher'
    }
}

NOW LOOKING AT THE GENERATED WAR FILE, EVERYTHING IN THE MANIFEST LOOKS FINE AS FOLLOWS:

Start-Class: com.mypackage.Application
Main-Class: org.springframework.boot.loader.PropertiesLauncher

Now I’m trying to figure out the right command line arguments to start this thing, which is a trial and error but gets me close to the finish line :

java -Dloader.path=WEB-INF/lib-provided,WEB-INF/lib,WEB-INF/classes -jar myapplication-4.0.0.war

So I basically looked at WarLauncher and tried to recreate the classpath via loader.path value without adding a 3rd party jar, because first I just wanted to launch my app.

My app loads fine.

Now I want to add a “local” path where my 3rd party jar is. I thought I could do it :

java -Dloader.path=WEB-INF/lib-provided,WEB-INF/lib,WEB-INF/classes,jar:file:lib -jar myapplication-4.0.0.war

So adding a jar:file:lib should refer to the “lib” folder next to my runnable WAR. But that doesn’t work. Also, just adding “,lib” to loader.path doesn’t work.

The only thing that works is to add a full path like “jar:file:/foo/bar/lib”, but I really want it relative to the currently runnable WAR folder.

Can anyone tell me how to define a relative local folder for scan jars?

I

hope I can help others who are struggling with working WAR files with PropertiesLauncher because it’s not very simple, except
https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html

Solution

Answer my own question, but it might help because the official documentation isn’t that good.

If you want to include a JAR file with definitions, you can use :

-Dloader.path=file:/myfolder/my.jar

If you want to include a complete director with multiple jars, you can do this:

-Dloader.path=file:/myfolder/

(You can use file:myfolder to simply make a folder related to WAR.)

Other variants, such as reading from inside the JAR, can be obtained by reading the following code
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java

What really works is the debug flag:

-Dloader.path=file:/myfolder/ -Dloader.debug=true

You will get system.outs about the PropertiesLauncher class loading problem.

I’d really like to see a -Dloader.schema=war flag that takes the WAR layout instead of the JAR layout. This way, you only need to add your own path without having to add the desired folder in the WAR.

Related Problems and Solutions