【发布时间】:2017-11-23 13:30:00
【问题描述】:
我正在使用 Netbeans 8.2 开发 Spring 应用程序。我遇到问题的这个特定应用程序是 Spring Boot 1.5.3 应用程序。我有一个 spring xml 文件和一个 application.properties,我保存在根项目目录下的 /config 中。
我通过@ImportResource 注释和@ImportResource(value="${config.xmlfile}") 之类的值属性将spring xml 文件传递给我的项目。
当我单击 Netbeans 中的“运行项目”按钮时,我的 Spring 应用程序会启动,它会在我的 /config 文件夹中正确找到 application.properties 文件。但是,对该文件夹中其他文件的任何类路径引用都将丢失。例如,将 config.xml 文件设置为 classpath:config/file.xml 或 classpath:file.xml 均无法找到该文件,但 file:config/file.xml 有效。
同样,从命令行运行时,我的结构如下:
app/
|-- bin
| `-- app-run.sh
|-- config
| |-- application.properties
| |-- log4j2.xml
| |-- file.xml
`-- app-exec.jar
我正在使用spring-boot-maven-plugin 来制作罐子,如下所示:
<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 脚本执行以下操作:
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 都无法找到该文件,但 file:../config/file.xml 有效。
我希望能够在 IDE 和命令行中设置类路径,以便我可以使用类路径引用访问 Spring 中的文件,从而使事情变得更容易。我不想将它们全部放在src/main/resources 中并将它们打包在 jar 中,因为我需要在打包和部署后对其进行编辑。
有没有人有任何想法或有用的提示?提前致谢!
【问题讨论】:
标签: java maven spring-boot netbeans spring-boot-maven-plugin