我在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>
Run Code Online (Sandbox Code Playgroud)
Spring Boot插件配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
据我所知spring-boot-maven-plugin,不支持复制/移动文件(请参阅文档),您可以使用来实现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>
Run Code Online (Sandbox Code Playgroud)
更新
Spring Boot提供了一组环境属性(spring boot文档),您可以spring-boot-maven-plugin向插件添加构件。
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
...
</plugins>
Run Code Online (Sandbox Code Playgroud)
每个属性文件都用逗号分隔,。这是从命令行运行的语法:
java -jar myJar.jar --spring.config.location=file:/path/location/prop1.properties,file:/path/location/prop2.properties
Run Code Online (Sandbox Code Playgroud)
要么
java -jar myJar.jar --spring.config.location=classpath:prop1.properties,classpath:prop2.properties
Run Code Online (Sandbox Code Playgroud)
例如,具有完整路径的最终指令将是:
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
Run Code Online (Sandbox Code Playgroud)
如果您target不需要从文件夹运行,请指定完整路径:
java -jar MY_GENERATED.jar --spring.config.location=file:conf\application.properties,file:conf\jdbc.properties
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看此
| 归档时间: |
|
| 查看次数: |
8676 次 |
| 最近记录: |