maven-resources-plugin主要是将项目中的资源文件编译、打包;注意,资源文件不是源代码,一般是环境的配置文件,默认存在src/main/resources和src/test/resources中。
maven-resources-plugin的三个目标(goal):
resources插件的配置
如果默认的配置文件不能满足我们的需求,比如资源文件中使用了占位符${},需要由属性文件替换。那就需要我们进行相关的配置,有两个地方需要进行配置:
1.在<build />下配置一些全局信息
<filters>
<filter>src/main/filters/dev.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/${environment.dir}</directory>
<includes>
<include>${environment}.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</testResource>
</testResources>
配置的解释如下:
2. 在maven-resources-plugin下配置
在插件标签<configuration/>下,和全局使用方式一样,这里列举一个设置字符集的例子:
a、指定project.build.sourceEncoding属性,maven-resources-plugin会自动使用这个属性作为字符集
<project ...>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
..
</project>
b、在maven-resources-plugin插件里自己设置
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
...
<encoding>UTF-8</encoding>
...
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>