Spring Boot从命令行执行将文件添加到classpath [英] Spring Boot add files to classpath from command line execution

查看:1841
本文介绍了Spring Boot从命令行执行将文件添加到classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Netbeans 8.2开发Spring应用程序。我遇到问题的这个特定应用程序是一个Spring Boot 1.5.3应用程序。我有一个spring xml文件和一个application.properties,我保存在根项目目录下的/ config中。

I am using Netbeans 8.2 to develop Spring applications. This specific app with which I am having trouble is a Spring Boot 1.5.3 app. I have a spring xml file and an application.properties that I keep in /config under the root project directory.

我通过 @ImportResource 注释和一个像<$的值属性将spring xml文件传递给我的项目c $ c> @ImportResource(value =$ {config.xmlfile})。

当我点击运行项目按钮时在Netbeans中我的Spring应用程序启动并正确找到我的/ config文件夹中的application.properties文件。但是,对该文件夹中其他文件的任何类路径引用都将丢失。例如,将config.xml文件设置为 classpath:config / file.xml classpath:file.xml 两者找不到文件,但 file:config / file.xml 有效。

When I click the 'Run Project' button in Netbeans my Spring app starts up and it correctly finds the application.properties file in my /config folder. However, any classpath references to other files in that folder are lost. For example, setting the config.xml file to classpath:config/file.xml or classpath:file.xml both fail to find the file but file:config/file.xml works.

同样,从命令运行时line我的结构如下:

Similarly, when running from the command line I have the following as my structure:

app/
|-- bin
|   `-- app-run.sh
|-- config
|   |-- application.properties
|   |-- log4j2.xml
|   |-- file.xml
`-- app-exec.jar

我正在使用使用 spring-boot-maven-plugin 制作jar如下:

I am using the spring-boot-maven-plugin to make the jar as follows:

<plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>${spring.boot.version}</version>
     <executions>
          <execution>
               <goals>
                    <goal>repackage</goal>
               </goals>
               <configuration>
                    <classifier>exec</classifier>
               </configuration>
          </execution>
     </executions>
</plugin>

我的app-run.sh脚本执行以下操作:

and my app-run.sh script executes the following:

exec /bin/java -cp :.:../config/*:../app-exec.jar 
-Dlogging.config=../config/log4j2.xml 
-Dspring.config.location=../config/application.properties 
-jar ../app-exec.jar

其中/ bin / java表示我安装java的位置。在-cp中设置的类路径似乎不起作用。与通过IDE运行时类似,将config.xml文件设置为classpath:config / file.xml或classpath:file.xml都无法找到文件但文件:../ config / file.xml无效。

where /bin/java represents the location where I have java installed. The classpath set in -cp does not seem to be working here. Similarly to when running through the IDE, setting the config.xml file to classpath:config/file.xml or classpath:file.xml both fail to find the file but file:../config/file.xml works.

我希望能够在IDE和命令行中设置类路径,以便我可以使用类路径引用访问Spring中的文件,以简化操作。我不想将它们全部放在 src / main / resources 中,并将它们打包在jar中,因为我需要在打包和部署后编辑它们。

I would like to be able to set the classpath in both the IDE and from command line so that I can access files in Spring using classpath reference to make things easier. I do NOT want to put them all in src/main/resources and have them be packaged in the jar, as I need to edit these after packaging and deployment.

有人有任何想法或有用的提示吗?提前致谢!

Does anybody have any ideas or helpful hints? Thanks in advance!

推荐答案

更新回答:



您可以关注在我原来的答案中的做法,但我们最近放弃了这个更简单和更清洁的选项,更标准(Java方式)。我们进行了更改,因为我们需要在运行时动态加载依赖库,这些库在编译时不可用(在它们的确切版本中)。在我们的例子中,我们只想从单独的文件夹加载依赖的jar,而不是从可执行的jar加载。我们最终在可执行jar和单独的文件夹中有重复的依赖项,因此我们决定删除可执行jar属性Launcher,而只是从单独的文件夹中加载依赖项。这通常是 NOT 最佳选项,应根据您的使用情况进行评估。我更喜欢阅读标准的Java类路径。

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.

要运行没有可执行jar的Spring Boot应用程序,我们使用Maven Assembly将依赖的jar放在/ libs目录中并删除spring-boot-maven-plugin。步骤和一些代码如下:

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. 删除以ZIP格式创建可执行jar的spring-boot-maven-plugin

  2. 将以下内容添加到程序集XML

  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>你希望libs去哪里< / outputDirectory>
< useProjectArtifact>是否要在此处包含项目
artifact< / useProjectArtifact>
< / dependencySet>
< / dependencySets>

从主类运行代码并包含相关的jar文件夹在类路径上。在您的操作系统上使用标准的类路径表示法而不是自定义的,笨拙的PropertiesLauncher加载程序路径语法

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>

实际通话的示例:
java -cp $ CLASSPATH:./ lib /*:./ cfg / *:my-app.jar Application.class

这样你就可以通过标准的java执行调用执行Spring Boot应用程序,没有自定义的Spring加载语法。您只需确保在运行时类路径上的所有依赖项都可用。我们发现这更容易维护,并使其成为我们所有应用程序的标准。

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.

经过一番研究,感谢@ TuyenNguyen的有用回答,我得到了以下工作:

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

我将以下内容添加到我的spring-boot-maven-plugin中,以便当我从命令行运行时,它使用 PropertiesLauncher 而不是 JarLauncher

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>

参见这里这里了解有关 PropertiesLauncher 选项。它允许您设置类路径等。
请参阅此处此处这里我在哪里找到答案这个问题。使用格式ZIP可以使用 PropertiesLauncher

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

另一个重要注意事项:指定时 -Dloader.path 确保使用逗号分隔值,只使用目录和文件,如上所述此处。此外,请务必在指定-jar jar之前放置-D args,否则将无法设置它们。

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!

这篇关于Spring Boot从命令行执行将文件添加到classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆