抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

插件的groupdId和artifactId

1
2
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>

介绍

Resources插件负责处理项目资源文件并拷贝到输出目录。Maven将main resources和test resources分开,一般main resources关联main source code,而test resources关联test source code。

Resources插件目标有三个:

  1. resources:resources,拷贝main resources到main output directory。它绑定了process-resources生命周期阶段,当执行Compiler:compile插件目标前就会执行此阶段。
  2. resources:testResources,拷贝test resources到test output directory。它绑定了process-test-resources生命周期阶段,当执行surefire:test插件目标前就会执行此阶段。
  3. resources:copy-resources,手动拷贝资源到输出目录

可以指定resources插件读取和写入文件的字符编码,比如ASCII,UTF-8或UTF-16。也可以指定${project.build.sourceEncoding}属性。

1
2
3
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

也可以通过指定编码

1
2
3
4
5
6
7
8
9
10
11
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

默认情况下,Maven会从项目的src/main/resources目录下查找资源。如果你的资源不在此目录下,可以用标签指定,同时也支持多个目录。

1
2
3
4
5
6
7
8
9
10
<build>
<resources>
<resource>
<directory>src/main/resources1</directory>
</resource>
<resource>
<directory>src/main/resources2</directory>
</resource>
</resources>
</build>

有的时候,资源文件中存在变量引用,可以使用标签指定是否替换资源中的变量。变量的来源为pom文件中的标签中定义的变量。也可以在中定义过滤器资源。

1
2
3
4
5
6
7
8
9
10
11
<build>
<filters>
<filter>filter-values.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

可能目录下的资源文件都需要被使用,可以使用来精细控制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.txt</include>
<include>**/*.rtf</include>
</includes>
<excludes>
<exclude>**/*.bmp</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
<resources>
</build>

如果资源中本来存在${}字符,不需要被替换,可以在$前加\,并在中使用

1
2
3
4
5
6
7
8
9
10
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<escapeString>\</escapeString>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugins>

另外目录下存在二进制文件,需要排除,也可以在中使用根据后缀来过滤。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 过滤后缀为pdf和swf的文件 -->
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<plugins>

如果你需要在其他阶段拷贝资源文件,可以使用插件目标copy-resources。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

参考文档:

http://maven.apache.org/plugins/maven-resources-plugin/

关于变量替换

变量替换有两种写法 ${xx} 和 @xx@,${xx}这种写法会和springboot起冲突,会不生效。而使用@xx@在springboot中却会生效。

来源

https://my.oschina.net/u/2377110/blog/1584204

评论