命令行中带参数指定${}变量值
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 资源文件拷贝插件,处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
Hello ${name}
jest.urls=${name}
(1) 执行mvn resources:resources -Dname="world"
è
Hello world
jest.urls=world
(2) 执行mvn install -Dname="world"
è
Hello world
jest.urls=world
Properties标签中指定${}变量值
<properties>
<name>my testname</name>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 资源文件拷贝插件,处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
(1) 执行mvn resources:resources
è
Hello my test name
jest.urls=my testname
(2) 执行mvn install
è
Hello my test name
jest.urls=my testname
properties文件中用<filter>标签过滤
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/resources/my-filter-values.properties</filter>
</filters>
<plugins>
<!-- 资源文件拷贝插件,处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
my-filter-values.properties
name=mytestingname
(1) 执行mvn resources:resources
è
Hello my testingname
jest.urls=my testingname
(2) 执行mvn install
è
Hello my testingname
jest.urls=my testingname
使用copy-resources copy
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.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/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
(1) 执行mvn resources:resources
è
Hello my testingname
jest.urls=my testingname
排除文件
<project>
...
<name>MyResources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
排除二进制文件
<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>
禁止过滤 使用<escapeString>
<properties>
<name>my test name</name>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 资源文件拷贝插件,处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<escapeString>\</escapeString>
</configuration>
</plugin>
</plugins>
</build>
指定\, 说明\${}的è${},其它照样替换
Hello\${name}
jest.urls=${name}
(1) 执行mvn resources:resources
è
Hello ${name}
jest.urls=my testname
(2) 执行mvn install
è
Hello ${name}
jest.urls=my testname
根据id在不同环境下打对应参数
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 资源文件拷贝插件,处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<escapeString>\</escapeString>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<jest.urls>http://n2:9200,http://n4:9200</jest.urls>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>production</id>
<properties>
<jest.urls>http://192.168.3.241:9200,http://192.168.3.242:9200</jest.urls>
</properties>
</profile>
</profiles>
mvn clean package-DskipTests -Pdev
è
Hello\${jest.urls}
jest.urls=http://n2:9200,http://n4:9200
mvn clean package-DskipTests -Pproduction
è
Hello\${jest.urls}
jest.urls=http://192.168.3.241:9200,http://192.168.3.242:9200
自定义过滤器 Custom resources filters
With version 2.5 you are now able to build your own customresources filter(s).
Your custom resources filter classes must implements org.apache.maven.shared.filtering.MavenResourcesFiltering.
CustomResources Filter Implementation
Your custom resources filter classes must be marked as aPlexus Component. Below a sample with a roleHint itFilter.
1. /**
2. * @plexus.component role="org.apache.maven.shared.filtering.MavenResourcesFiltering"
3. * role-hint="itFilter"
4. */
5. public class ItFilter
6. implements MavenResourcesFiltering
Then you must activate in your build the mojo which willscan javadoc annotations to transform thoses to plexus component metadata.
1. <plugin>
2. <groupId>org.codehaus.plexus</groupId>
3. <artifactId>plexus-maven-plugin</artifactId>
4. <version>1.3.4</version>
5. <executions>
6. <execution>
7. <goals>
8. <goal>descriptor</goal>
9. </goals>
10. </execution>
11. </executions>
12. </plugin>
Dependencydeclaration
Your classes must be available in the maven-resources-pluginclasspath, this can be done with adding your artifact to the plugindependencies.
1. <project>
2. ...
3. <build>
4. <plugins>
5. <plugin>
6. <groupId>org.apache.maven.plugins</groupId>
7. <artifactId>maven-resources-plugin</artifactId>
8. <version>3.1.0</version>
9. <configuration>
10. ...
11. </configuration>
12. <dependencies>
13. <dependency>
14. <groupId>custom resources filters artifact groupId</groupId>
15. <artifactId>custom resources filters artifact artifactId</artifactId>
16. <version>custom resources filters artifact version</version>
17. </dependency>
18. </dependencies>
19. </plugin>
20. </plugins>
21. ...
22. </build>
23. ...
24.</project>
Useof your Custom Resource Filter with the maven-resources-plugin
You must now declare you custom filter in the plugin.mavenFilteringHint must respect same syntax as your Plexus Component roleHint.
1. <plugin>
2. <groupId>org.apache.maven.plugins</groupId>
3. <artifactId>maven-resources-plugin</artifactId>
4. <version>3.1.0</version>
5. <configuration>
6. ...
7. <mavenFilteringHints>
8. <mavenFilteringHint>itFilter</mavenFilteringHint>
9. </mavenFilteringHints>
10. </configuration>
11. ...
12. </configuration>
13. </plugin>