简体   繁体   English

如何在类路径上运行带有额外文件的Spring Boot应用程序而不修改应用程序(xxx.jar)

[英]how to run Spring Boot application with an extra file on classpath without modifying the application (the xxx.jar)

I would like to run a Spring Boot application with an extra file (happens to be a css) being on the classpath. 我想在类路径上运行一个带有额外文件(可能是css)的Spring Boot应用程序。 I can't touch the jar (the application itself). 我不能碰罐子(应用程序本身)。 I can only modify the start script. 我只能修改启动脚本。

I've received a start script with the application:: 我已经收到该应用程序的启动脚本::

#! /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

My naive first try was adding a folder with -cp and put the file into that folder. 我天真的第一次尝试是使用-cp添加一个文件夹,然后将文件放入该文件夹。 However that is not working because -cp and -jar is not compatible ( nice explanation here: Differences between "java -cp" and "java -jar"? ) 但是,这不起作用,因为-cp和-jar不兼容(此处很好的解释: “ java -cp”和“ java -jar”之间的区别?

Then I found suggestions for using PropertiesLauncher + loader.path which can be seen as a replacement for classpath on the command line ( https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features ) . 然后我发现了使用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? ) 但是,要使用PropertiesLauncher,示例建议修改pom,而我做不到( Spring Boot:是否可以在带有胖子的任意目录中使用外部application.properties文件?

What I can do is modifying the shell script. 我可以做的是修改shell脚本。

How should I (if it is possible at all) put the extra file onto the classpath without modifying the Spring Boot application? 我应该如何(如果有可能)将多余的文件放到类路径中而不修改Spring Boot应用程序?

This also works for me: 这也适用于我:

#! /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

In my case this seems to be simpler than using the PropertiesLauncher. 就我而言,这似乎比使用PropertiesLauncher更简单。

I've found this article, which shows how to use PropertiesLauncher without modifying the application (without modifying the pom.xml): https://mash213.wordpress.com/2017/01/05/hack-how-2-add-jars-2-springboot-classpath-with-jarlauncher/ 我找到了这篇文章,该文章显示了如何使用PropertiesLauncher而不修改应用程序(无需修改pom.xml): https : //mash213.wordpress.com/2017/01/05/hack-how-2-add- jars-2-springboot-classpath-with-jarlauncher /

So with the above I can add an extra folder to the classpath and it's indeed working: 因此,通过上述操作,我可以在类路径中添加一个额外的文件夹,它确实可以正常工作:

#! /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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM