简体   繁体   English

可执行jar的外部属性文件

[英]External properties file for an executable jar

I'm trying to add an external properties file to an executable jar, but not in succeed for now. 我正在尝试将外部属性文件添加到可执行jar中,但暂时没有成功。

Question : Is it impossible to include an external properties file into an executable jar? 问题 :不可能将外部属性文件包含到可执行jar中吗?

When I run my program locally, with putting batch.properties under src/main/resources , it works fine. 当我在本地运行程序时,将batch.properties放在src/main/resources ,它可以正常工作。 But it fails after deploying the jar and batch.properties , linking it under /etc/init.d/ , putting the jar and batch.properties 但是,在部署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 can be executable just like an usual service: my-app可以像通常的服务一样可执行:

/etc/init.d/my-app start

But it says that some beans cannot be instantiated due to missing placeholders. 但是它说,由于缺少占位符,无法实例化某些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}"

My pom.xml is like this: 我的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>

And the Spring setting is just like this: Spring设置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...">
  <context:property-placeholder location="classpath*:batch.properties" />
  <!-- Some bean definitions -->
</beans>

Any help or comments would be appreciated so much. 任何帮助或评论将不胜感激。

EDIT 编辑

I couldn't succeed it for now, but alternately I decided to split profiles and include a properties file accordingly into the executable jar. 我暂时无法成功,但我还是决定拆分配置文件,并将相应的属性文件包含在可执行jar中。

However it's better to be able to read an external properties file, any help would still be welcomed. 但是,最好能够读取外部属性文件,仍然欢迎任何帮助。

You can add an external properties file next to the .jar provided that you call it application.properties . 您可以在.jar旁边添加一个外部属性文件,前提是您将其称为application.properties

If you cannot rename the properties file, you can also give the path of the external properties file as an option : 如果您不能重命名属性文件,则还可以选择提供外部属性文件的路径:

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

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

You can also create a symlink application.properties -> batch.properties . 您还可以创建一个符号链接application.properties > batch.properties

See Externalized configuration . 请参阅外部化配置

EDIT 编辑

This is my maven config : 这是我的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>

After mvn clean install, I get 2 jars : MVN全新安装后,我得到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

Then, I copy the first jar to the destination folder and place a configuration.properties next to it : 然后,将第一个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

You can see that I also created a file called backend.service in order to install the jar as a service via systemd . 您可以看到,我还创建了一个名为backend.service的文件,以便通过systemd将jar作为服务安装。

Here is the content of the file : 这是文件的内容:

[Unit]
Description=backend
After=syslog.target

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

[Install]
WantedBy=multi-user.target

And there is a symlink to this file in /etc/systemd/system : /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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM