Spring Boot 排除属性文件并在执行时将其提供给 jar

Posted

技术标签:

【中文标题】Spring Boot 排除属性文件并在执行时将其提供给 jar【英文标题】:Spring boot exclude properties file and provide it to jar on execution 【发布时间】:2017-12-27 23:28:40 【问题描述】:

我在src/main/resources目录下有多组属性文件smtp、db、常用设置,我的项目是maven项目jar打包。我想从 jar 构建中排除所有这些属性文件,并将所有这些属性文件放到 target/conf 文件夹中。现在我想在执行应用程序 jar 时将所有这些配置文件提供给我的 jar。 如何做到这一点?我在 pom.xml 文件中做了几件事来排除所有这些属性,现在无法将它们提供给生成的 jar。 这是为了排除 src/main/resources 内容

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>*.properties</exclude>
        </excludes>
        <filtering>false</filtering>
    </resource>
</resources>

Spring boot 插件配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

Maven资源插件将所有资源文件放在target/conf文件夹下

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>target/conf</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

【问题讨论】:

请向我们展示您所做的工作。 “几件事”是行不通的。 【参考方案1】:

据我所知spring-boot-maven-plugin 不支持复制/移动文件(check out the documentation),您可以使用maven-resources-plugin-3.0.2 来做到这一点。

通过以下示例,您可以从src/main/resources 中排除所有属性文件并将它们移动到target/conf。这里是例子:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>$basedir/target/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

更新

Spring boot 提供了一组环境属性(spring boot documentation),你可以在插件中添加spring-boot-maven-plugin神器。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    ...
</plugins>

每个属性文件以逗号, 分隔。以下是从命令行运行的语法:

java -jar myJar.jar --spring.config.location=file:/path/location/prop1.properties,file:/path/location/prop2.properties

java -jar myJar.jar --spring.config.location=classpath:prop1.properties,classpath:prop2.properties

例如,带有完整路径的最终指令是:

java -jar MY_GENERATED.jar --spring.config.location=file:C:\Users\Admin\workspace\myProject\target\conf\application.properties,file:C:\Users\Admin\workspace\myProject\target\conf\jdbc.properties

如果您从target 文件夹运行,则无需指定完整路径:

java -jar MY_GENERATED.jar --spring.config.location=file:conf\application.properties,file:conf\jdbc.properties

更多信息check out this

【讨论】:

在此之后我如何通过使用以下命令向 jar 提供属性文件:java -jar MY_GENERATED.jar 感谢您的回答。我试过了。这与我在应用程序中使用的相同。

以上是关于Spring Boot 排除属性文件并在执行时将其提供给 jar的主要内容,如果未能解决你的问题,请参考以下文章

从 C# 启动 Excel 并在“保存”时将其关闭

Spring Boot 排除自动配置的4个方法

我可以在 Gradle 中设置一个变量并在编译时将其传递给 Java 吗?

使用 Spring Boot 2.0 时将指标导出到文件

运行bash命令并在超过特定时间时将其杀死[重复]

转储 Spring Boot 配置