maven-resources-plugin

maven-resources-plugin

运行install ,默认含有 maven-resources-plugin ?

作用:他的作用是将项目的资源(resources目录下)目录的文件复制到输出目录(target),输出目录又分为了两个,一个是测试的输出目录,一个是主资源的。所以对应了process-resources和process-test-resources两个阶段上。

官网介绍:https://maven.apache.org/plugins/maven-resources-plugin/

1、字符集编码

既然插件是copy资源文件那必然涉及到文件的编码,可以选择的编码有ASCII, UTF-8 或者 UTF-16,设置编码的方式有两种。

第一种:使用project.properties标签声明project.build.sourceEncoding,声明好后,插件当中的标签会取这个编码。

<project ...>
 ...
 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   ...
 </properties>
 ..
</project>

第二种通过maven-resources-plugin插件配置:

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

2、指定资源文件目录

    <resources>
        <resource>
            <directory>src/config</directory>
            <includes>
                <include>
                    other.conf
                </include>
            </includes>
        </resource>
    </resources>

注意:使用了resources标签不会将src/main/resources中的文件copy到target中,src/test/resources不受影响。

3、filtering

src/main/resources/config.properties

name = ${my.name}

编译后:
class/config.properties

name = world

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

name = ${my.name}
other.name = ${other.name}
some.name = \${some.name}

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
            <escapeString>\</escapeString>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

4、auto-config

src/config/other.conf

other.name = xian

src/config/some.conf

some.name = skx

pom.xml

    <build>
        <filters>
            <filter>src/config/other.conf</filter>
            <filter>src/config/some.conf</filter>
        </filters>
		...
           <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
			...
    </build> 

编译后:
class/config.properties

name = world
other.name = xian
some.name = skx

5、copy-resources

 <build>
       <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>META-INF/**/*</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>process-META</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources/</directory>
                                    <includes>
                                        <include>**/*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>
posted @ 2023-07-10 14:52  北feng  阅读(497)  评论(0编辑  收藏  举报