Spring Boot add files to classpath from command line execution

瘦欲@ 提交于 2019-12-04 11:33:09

Updated answer:

You can follow the practice in my original answer, but we recently dropped this for a simpler and cleaner option that is more standard (the "Java" way). We made the change because we needed to dynamically load dependent libraries at runtime that were not available at compile time (in their exact version). In our case we wanted to load dependent jars only from separate folder(s) and not from an executable jar. We ended up having duplicate dependencies in the executable jars and in separate folder(s), so we decided to drop the executable jar Properties Launcher and instead only load dependencies from separate folders. This is often NOT the best option and should be evaluated for your use case. I prefer reading the standard Java classpath.

To run a Spring Boot app without an executable jar, we used Maven Assembly to put the dependent jars in a /libs directory and dropped the spring-boot-maven-plugin. The steps and some code for this are below:

  1. Remove the spring-boot-maven-plugin that creates the executable jar in ZIP format
  2. Add the following to your assembly XML

    <dependencySets> <dependencySet> <outputDirectory>where you want the libs to go</outputDirectory> <useProjectArtifact>whether you want to include the project artifact here</useProjectArtifact> </dependencySet> </dependencySets>

  3. Run your code from the main class and include the dependent jar folder(s) on the classpath. Use the standard classpath notation on your OS and not the custom, awkward PropertiesLauncher loader path syntax

    java -cp <standard-classpath> <main-class>

An example of an actual call: java -cp $CLASSPATH:./lib/*:./cfg/*:my-app.jar Application.class

In this way you execute the Spring Boot app via standard java execution call, no custom Spring loading syntax. You just need to ensure that all of your dependencies are available on the classpath at runtime. We found this much easier to maintain and made this the standard for all of our apps.

Original answer:

After some researching, and thanks to @TuyenNguyen's helpful answer I was able to get the following working:

I added the following to my spring-boot-maven-plugin so that when I run from the command line it uses the PropertiesLauncher instead of the JarLauncher:

<configuration>
     <mainClass>${mainClass}</mainClass>
     <layout>ZIP</layout> //THIS IS THE IMPORTANT PART
</configuration>

See here and here for more about the PropertiesLauncher options. It allows you to set the classpath, among other things. See here, here, and here for where I found the answer to this problem. Using format ZIP makes the PropertiesLauncher be used.

From there, I was able to use this command to launch the application as I intended:

java -Dloader.path=../config,../ -Dloader.config.location=classpath:application.properties -jar ../app-exec.jar

Another important note: when specifying the -Dloader.path make sure to use comma-separated values and only directories and files, as described here. Also, be sure to put the -D args before you specify -jar jar or they will not be set.

If anyone has any suggestions or edits to further improve this answer or the original question in order to help additional users, please let me know or make the edits yourself!

If you don't put your files in src/main/resources then you can put it inside any folder that you want, BUT you must set your folder as a resources folder. Because classpath is always point to resources folder. Once you make your folder as a resource folder, it will be packaged into the jar. If you want to edit your resource file, just using 7 zip tool to open your jar -> edit files -> save -> it will update your change in the jar.

Another solution is create a folder, put all files you want to edit and not packaged in that, then set classpath manually to that folder every time you run, but the way you set above is not correct, try this solution for set classpath correct way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!