【发布时间】:2016-04-21 11:38:56
【问题描述】:
我正在尝试将外部属性文件添加到可执行 jar,但目前没有成功。
问题:是否不可能将外部属性文件包含到可执行 jar 中?
当我在本地运行我的程序时,将batch.properties 放在src/main/resources 下,它工作正常。但部署jar和batch.properties后失败,将其链接到/etc/init.d/下,放入jar和batch.properties
/usr/local/myapp/myapp.jar
/usr/local/myapp/batch.properties
/etc/init.d/my-app --(symlink)--> /usr/local/myapp/myapp.jar
my-app 可以像普通服务一样执行:
/etc/init.d/my-app start
但它说由于缺少占位符,某些 bean 无法实例化。
Invalid bean definition with name 'dataSource' defined in class path resource [batch.xml]: Could not resolve placeholder 'spring.datasource.batch.class_name' in string value "${spring.datasource.batch.class_name}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.batch.class_name' in string value "${spring.datasource.batch.class_name}"
我的 pom.xml 是这样的:
<project ...>
// Some other definitions...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>my.project.Bootstrap</mainClass>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
而Spring的设置是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...">
<context:property-placeholder location="classpath*:batch.properties" />
<!-- Some bean definitions -->
</beans>
任何帮助或 cmets 将不胜感激。
编辑
我暂时无法成功,但我决定拆分配置文件并将相应的属性文件包含到可执行 jar 中。
但最好能够读取外部属性文件,但仍欢迎任何帮助。
【问题讨论】:
-
如果一切都失败了,你可以明确地询问JVM包含你的类的jar文件在哪里,然后去那个目录,找到属性文件并自己加载。
标签: java maven spring-boot executable-jar