【问题标题】:External properties file for an executable jar可执行 jar 的外部属性文件
【发布时间】: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


【解决方案1】:

您可以在 .jar 旁边添加一个外部属性文件,前提是您将其命名为 application.properties

如果你不能重命名属性文件,你也可以给外部属性文件的路径作为选项:

java -jar myproject.jar --spring.config.location=/path/to/batch.properties

#Or for executable jars
./myproject.jar --spring.config.location=/path/to/batch.properties

您还可以创建符号链接application.properties -> batch.properties

Externalized configuration

编辑

这是我的 Maven 配置:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.3.3.RELEASE</version>
      <configuration>
        <executable>true</executable>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

在 mvn clean install 之后,我得到了 2 个罐子:

backend/target [ ll
total 53M
-rwxr--r-- 1 adenoyelle adenoyelle  53M avril 22 10:56 backend-1.0-SNAPSHOT.jar
-rw-r--r-- 1 adenoyelle adenoyelle 353K avril 22 10:56 backend-1.0-SNAPSHOT.jar.original

然后,我将第一个 jar 复制到目标文件夹并在其旁边放置一个configuration.properties

backend@[...]:~/backend$ ll
total 53900
drwxrwxr-x 2 backend backend     4096 Apr 21 13:56 ./
drwxr-xr-x 6 backend backend     4096 Apr 21 13:56 ../
-rw-rw-r-- 1 backend backend      511 Apr 20 16:13 application.properties
-rwxr--r-- 1 backend backend 55175294 Apr 20 19:06 backend-1.0-SNAPSHOT.jar*
lrwxrwxrwx 1 backend backend       24 Apr 20 19:20 backend -> backend-1.0-SNAPSHOT.jar*
-rw-r--r-- 1 backend backend      179 Apr 20 19:26 backend.service

您可以看到我还创建了一个名为 backend.service 的文件,以便通过 systemd 将 jar 安装为服务。

这是文件的内容:

[Unit]
Description=backend
After=syslog.target

[Service]
User=backend
ExecStart=/home/backend/backend
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

/etc/systemd/system 中有一个指向此文件的符号链接:

backend@[...]:/etc/systemd/system$ ll | grep backend
lrwxrwxrwx  1 root root   37 Apr 20 19:23 backend.service -> /home/backend/backend.service

【讨论】:

  • 嗯,可执行jar不一定要通过java命令执行,所以不知道显式添加路径的方法。
  • @tsuda7 我在我的项目中使用了一个可执行的 jar,并在 jar 旁边放置了一个 application.properties 来完成这项工作。可以试试吗?
  • 好的,我错过了你的第二个 ./jar 建议。这似乎是解决方案,我会尝试它!如果它有效,我会告诉你,请给我一些时间
  • 嗨@Arnaud,我正在尝试您的建议,但没有任何改变...我原则上了解如何为我的 jar 提供配置,但它无法以某种方式工作...你是使用 springboot 的可执行选项,就像我在 pom.xml 中所做的那样?我尝试的是:将batch.properties重命名为应用程序属性,添加spring.config.location选项,从java命令及其组合执行。
  • @tsuda7 我更新了答案以显示配置。
猜你喜欢
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2011-05-06
  • 1970-01-01
  • 2013-05-07
  • 2016-11-29
  • 2014-12-30
相关资源
最近更新 更多