可执行 jar 的外部属性文件

External properties file for an executable jar

我正在尝试将外部属性文件添加到可执行 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>

如有任何帮助或意见,我们将不胜感激。

编辑

我暂时无法成功,但是我决定拆分配置文件并将属性文件相应地包含到可执行 jar 中。

然而,最好能够读取外部属性文件,我们仍然欢迎任何帮助。

您可以在 .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 个 jar :

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