Maven學習(四) - 插件maven-resources-plugin

Resources插件負責處理項目資源文件並拷貝到輸出目錄。Maven將main resources和test resources分開,通常main resources關聯main source code,而test resources關聯test source code。apache

Resources插件目標有三個:maven

  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}屬性。ui

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

也能夠經過<configuration>指定編碼編碼

<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目錄下查找資源。若是你的資源不在此目錄下,能夠用<resources>標籤指定,同時也支持多個目錄。插件

<build>
	<resources>
		<resource>
			<directory>src/main/resources1</directory>
		</resource>
		<resource>
			<directory>src/main/resources2</directory>
		</resource>
	</resources>
</build>

有的時候,資源文件中存在變量引用,能夠使用<filtering>標籤指定是否替換資源中的變量。變量的來源爲pom文件中的<properties>標籤中定義的變量。也能夠在<build>中定義過濾器資源。code

<build>
	<filters>
		<filter>filter-values.properties</filter>
	</filters>
	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
</build>

可能目錄下的資源文件都須要被使用,能夠使用<includes>和<excludes>來精細控制。生命週期

<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>

若是資源中原本存在${}字符,不須要被替換,能夠在$前加\,並在<configuration>中使用<escapeString>。資源

<plugins>
	<plugin>
		<artifactId>maven-resources-plugin</artifactId>
		<version>3.0.2</version>
		<configuration>
			<escapeString>\</escapeString>
			<encoding>UTF-8</encoding>
		</configuration>
	</plugin>
<plugins>

另外目錄下存在二進制文件,須要排除,也能夠在<configuration>中使用<nonFilteredFileExtensions>根據後綴來過濾。文檔

<!-- 過濾後綴爲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。get

<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>

參考文檔:

  1. http://maven.apache.org/plugins/maven-resources-plugin/
相關文章
相關標籤/搜索