我在src / main / resources目录下有多组属性文件smtp,db,常用设置,我的项目是maven project jar packaging。我想从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>
答案 0 :(得分:5)
据我所知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