【问题标题】:Externalized configuration for rpmrpm 的外部化配置
【发布时间】:2016-09-19 09:18:02
【问题描述】:

简介:

这个问题与How to put a directory first on the classpath with Spring Boot? 非常相似,我搜索了一下,但不幸的是到目前为止没有任何效果。既没有在spring-boot-maven-plugin 中更改为packaging=ZIP,也没有按照boot features external confighow to - properties and configuration 中的说明使用loader.path,所以我一定遗漏了一些东西。还有一件事,如果可能的话,我想避免将每个配置文件作为参数传递给file://...


我有一个应用程序,它最初是用于多种服务类型的模拟器:sftp、soap 等。最初它支持通过属性文件配置的每种服务器类型的 1 个实例。

我现在已经更新它以支持不同端口上的多个服务器实例,并且配置是在 YAML 文件中完成的,而且我还从经典 spring 迁移到 boot。最后,应用程序以 RPM 的形式交付,其结构如下

installation-dir
    |--bin
    |    '-- simulator.sh [lifecycle management script]
    |--config
    |    |--application.properties
    |    |--log4j2.xml
    |    |--samples       [sample files for various operations]
    |    |    |-- sample1.csv
    |    |    |-- sample2.csv
    |    |    '-- sample3.csv
    |    |--simulators.yaml [simulators config]
    |    '--simulator.jks
    |--lib
    |    '-- simulator-1.0.jar
    '--log
         '-- simulator.log

之前,simulators.sh 启动了主类,将config 目录添加到类路径中,spring 会毫无问题地加载属性文件:

java <other args> -cp "..." com.whatever.SimulatorLauncher

自迁移以来,它现在启动生成的 jar,因此不再使用 -cp,我无法让它拾取配置文件:

java <other args> lib/simulator-1.0.jar

忽略没有找到aplication.properties的事实,模拟器的配置加载如下:

@Configuration
@ConfigurationProperties(locations = {"classpath:/simulators.yml"}, ignoreUnknownFields = false, prefix = "simulators")
public class SimulatorSettings {
    private static final Logger log = LoggerFactory.getLogger(SimulatorSettings.class);

    @NotNull
    private List<SftpSettings> sftp;

    @NotNull
    private List<SoapSettings> soap;

由于配置应该是可更新的,无需重新打包应用程序,所有文件都从生成的 jar 中排除,而是打包在 config 下的 rpm 中:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <excludes>
                    <!-- these files will be included in the rpm under config -->
                    <exclude>application.properties</exclude>
                    <exclude>log4j2.xml</exclude>
                    <exclude>samples/**</exclude>
                    <exclude>simulators.yml</exclude>
                    <exclude>simulator.jks</exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.whatever.SimulatorLauncher</mainClass>
                <layout>ZIP</layout>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>rpm-maven-plugin</artifactId>
            <version>2.1.5</version>
            ...
            <configuration>
            ...
                    <mapping>
                        <directory>${rpm.app.home}/config</directory>
                        <sources>
                            <source>
                                <location>src/main/resources</location>
                                <includes>
                                    <include>application.properties</include>
                                    <include>simulators.yml</include>
                                    <include>log4j2.xml</include>
                                    <include>nls-sim.jks</include>
                                </includes>
                            </source>
                        </sources>
                        <filemode>755</filemode>
                    </mapping>

非常感谢任何帮助或提示。

【问题讨论】:

    标签: spring-boot rpm spring-boot-maven-plugin


    【解决方案1】:

    我最终删除了 spring-boot-maven-plugin 以防止重新打包,并使用我的人工制品及其依赖项创建 RPM,并像以前一样启动应用程序:

    java <other args> -cp "<installation dir>/config:...:<libs>" com.whatever.SimulatorLauncher
    

    不过,我很想知道是否有人知道使用 spring boot 重新打包的 jar 的方法...

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 2018-10-03
      • 2019-07-15
      • 2019-08-17
      • 2017-08-14
      • 2021-01-05
      • 2020-08-21
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多