欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

maven-resources-plugin

最编程 2024-03-30 13:00:08
...

简介

本文参照官网:http://maven.apache.org/plugins/maven-resources-plugin/

处理资源⽂件。默认的主资源⽂件⽬录是src/main/resources,很多⽤⼾会需要添加额外的资源⽂件⽬录,这个时候就可以通过配置maven-resources-plugin来实现。

指令

  • resources:resources

    拷贝main resources到main output directory。它绑定了process-resources生命周期阶段,当执行Compiler:compile插件目标前就会执行此阶段。

  • resources:testResources

    拷贝test resources到test output directory。它绑定了process-test-resources生命周期阶段,当执行surefire:test插件目标前就会执行此阶段。

  • resources:copy-resources

    手动拷贝资源到输出目录

常用配置

指定编码

可以选择诸如ASCII,UTF-8或UTF-16之类的字符编码方案来读取和写入文件。

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          ...
          <encoding>UTF-8</encoding>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>

指定resources目录

默认情况下,Maven会从项目的src/main/resources目录下查找资源

Project
|-- pom.xml
`-- src
    `-- main
        `-- resources
<build>
    <resources>
         <resource>
             <directory>[your folder here]</directory>
         </resource>
        <resource>
            <directory>src/main/user_resource_1</directory>
        </resource>
        <resource>
            <directory>src/main/user_resource_2</directory>
        </resource>
    </resources>
</build>

过滤/替换

在你的resources中包含变量,这些变量以$ {...}分隔符表示。可以来自系统属性,项目属性,过滤器资源和命令行。

例如:src/main/resources/hello.txt 包含以下内容

Hello ${name}
<!-- POM -->
<project>
  <name>My Resources Plugin Practice Project</name>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
  </build>
</project>
# 运行命令:
mvn resources:resources

将在target/classes/hello.txt中创建一个包含完全相同的文本的资源输出

Hello ${name}

但是,如果将<filtering>标记添加到POM并将其设置为true,如下所示:

...
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...

再次运行命令,target/classes/hello.txt文本如下,文件变量被<properties>中的属性替换

mvn resources:resources
Hello Hello My Resources Plugin Practice Project

可以使用-Dname属性进行替换,比如-Dname="world"输出 Hello wold

mvn resources:resources -Dname="world"

对与多个变量的替换,使用外置文件更为放便

  <properties>
    <!-- 指定外置配置文件 -->
    <filter_file>filters_file.properties</filter_file>
  </properties>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <filters>
      <!-- 引入外置配置文件 -->
      <filter>${filter_file}</filter>
    </filters>
</build>
# 外置文件 filters_file.properties
name= world
version= 1.0.1

注意:

  • 不建议对二进制文件使用filter,将会带来输出错误/损坏,建议将resource分为两个部分: src/main/resources (default),src/main/resources-filtered
  • 如果使用一个文件夹,建议配置将pdf、image等二进制不进行过滤

包含/排除

指定资源目录包含的文件/排除的文件

<build>
    <resources>
        <resource>
              <directory>src/my-resources</directory>
              <includes>
                    <include>**/*.txt</include>
                    <include>**/*.rtf</include>
                    <include>[resource file #1]</include>
                    <include>[resource file #2]</include>
              </includes>
              <excludes>
                    <exclude>**/*test*.*</exclude>
                    <exclude>**/*.pdf</exclude>
                    <exclude>[non-resource file #n]</exclude>
              </excludes>
        </resource>
    </resources>
  </build>

配置 escape

指定转译符号,比如如下配置转译符号为\

  • \${name} 将直接输出字面量${name}
  • #${name} 将会被过滤 比如 name=word 将输出#word
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          ...
          <escapeString>\</escapeString>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

资源拷贝

<project>
  ...
  <build>
      <plugins>
          <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.2.0</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>
  ...
</project>
# 执行下面操做会看到拷贝效果
mvn process-resources

过滤二进制

插件将阻止二进制文件过滤,而无需为以下文件扩展名jpg,jpeg,gif,bmp和png添加某些排除配置

如果您想添加补充文件扩展名,则可以通过使用类似以下的配置来简单地实现:

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
              <nonFilteredFileExtensions>
                  <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                  <nonFilteredFileExtension>swf</nonFilteredFileExtension>
               </nonFilteredFileExtensions>
            </configuration>
        </plugin>