如何 运行 Spring 在不修改应用程序的情况下使用类路径上的额外文件启动应用程序(xxx.jar)
how to run Spring Boot application with an extra file on classpath without modifying the application (the xxx.jar)
我想 运行 一个 Spring 启动应用程序,在类路径上有一个额外的文件(恰好是 css)。我无法触摸罐子(应用程序本身)。我只能修改启动脚本
我收到了应用程序的启动脚本::
#! /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
我天真的第一次尝试是用 -cp 添加一个文件夹并将文件放入该文件夹。但是这不起作用,因为 -cp 和 -jar 不兼容(这里很好的解释:Differences between "java -cp" and "java -jar"?)
然后我找到了使用 PropertiesLauncher + loader.path 的建议,它可以看作是命令行上类路径的替代品 (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?)
我能做的就是修改shell脚本。
我应该如何(如果可能的话)在不修改 Spring 启动应用程序的情况下将额外的文件放到类路径中?
我找到了这篇文章,它展示了如何在不修改应用程序(不修改 pom.xml)的情况下使用 PropertiesLauncher:
https://mash213.wordpress.com/2017/01/05/hack-how-2-add-jars-2-springboot-classpath-with-jarlauncher/
因此,通过上面的内容,我可以向类路径添加一个额外的文件夹,它确实有效:
#! /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
这也适用于我:
#! /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
在我的例子中,这似乎比使用 PropertiesLauncher 更简单。
我想 运行 一个 Spring 启动应用程序,在类路径上有一个额外的文件(恰好是 css)。我无法触摸罐子(应用程序本身)。我只能修改启动脚本
我收到了应用程序的启动脚本::
#! /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
我天真的第一次尝试是用 -cp 添加一个文件夹并将文件放入该文件夹。但是这不起作用,因为 -cp 和 -jar 不兼容(这里很好的解释:Differences between "java -cp" and "java -jar"?)
然后我找到了使用 PropertiesLauncher + loader.path 的建议,它可以看作是命令行上类路径的替代品 (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?)
我能做的就是修改shell脚本。
我应该如何(如果可能的话)在不修改 Spring 启动应用程序的情况下将额外的文件放到类路径中?
我找到了这篇文章,它展示了如何在不修改应用程序(不修改 pom.xml)的情况下使用 PropertiesLauncher: https://mash213.wordpress.com/2017/01/05/hack-how-2-add-jars-2-springboot-classpath-with-jarlauncher/
因此,通过上面的内容,我可以向类路径添加一个额外的文件夹,它确实有效:
#! /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
这也适用于我:
#! /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
在我的例子中,这似乎比使用 PropertiesLauncher 更简单。