maven插件之maven-resource-plugin

maven插件-maven-resource-plugin

maven resource plugin将复制的工程的resources下的文件到输出目录。一般的maven工程都有两种资源文件:main resourcestest resources

使用

指定资源目录

默认情况下,这个插件查询的是src/main/resources下的文件:

Project
|-- pom.xml
`-- src
    `-- main
        `-- resources

但是并不是所有的资源文件都放在src/main/resources这个目录下的,而这个插件可以指定资源目录文件:

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
     //可以有指定多个资源目录
     <resource>
       <directory>resource2</directory>
     </resource>
     <resource>
       <directory>resource3</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

Filtering

一般的,资源文件像.properties文件可能会包涵占位符,而这个属性是的作用就是将${property}的属性替换。当且仅当filtering=true时才会替换。

如在src/main/resources目录下有一文件hello.txt,其中内容是:

Hello ${name}

在工程的pom文件中这样写:

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

经过这样处理之后,hello.txt内容就变成(如果name=tan):

Hello tan

include and exclude 包括与排除文件目录

当指定一个具体的资源目录的时候,在这个资源目录下的所有文件将被忽略。所有我们必须指定要加入或者是排除指定的文件

要包涵一个资源文件,可能通过<include>标签:


<project>
  <build>
    ...
    <resources>
      <resource>
        <directory>[your directory]</directory>
        <includes>
          <include>[resource file #1]</include>
          <include>[resource file #2]</include>
          ...
          <include>[resource file #n]</include>
        </includes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

如果要排除某个资源文件,可能通过<exclude>标签:


<project>
  <build>
    ...
    <resources>
      <resource>
        <directory>[your directory]</directory>
        <excludes>
          <exclude>[non-resource file #1]</exclude>
          <exclude>[non-resource file #2]</exclude>
          <exclude>[non-resource file #3]</exclude>
          ...
          <exclude>[non-resource file #n]</exclude>
        </excludes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

当然了,两个标签可以同时使用

Binary filtering(二进制文件过滤)

这个插件阻止对二进制文件的过滤,对于如jpg,jpeg,gif,bmp,png扩展名的文件,我们不需要添加exclude的配置。如果需要添加额外的的扩展名文件,可以通过以下方式实现:


<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          ...
          <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
            <nonFilteredFileExtension>swf</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>
发布了76 篇原创文章 · 获赞 66 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/u013887008/article/details/103846988