Maven Resources Plugin: Difference between revisions
(32 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
=Overview= | =Overview= | ||
By default, the plugin reads the configuration declared in the <project><build><resources> and <project><build><testResources> sections and automatically executes its "resources:resources" and "resource:testResources" goals at the "[[Maven_Concepts_-_Lifecycle#process-resources|process-resources]]" and "[[Maven_Concepts_-_Lifecycle#process-test-resources|process-test-resources]]" lifecycle phases, respectively. When copying resources, it uses <project><build><outputDirectory> as destination and when copying test resources, it uses <project><build><testOutputDirectory> as destination. | |||
The plugin can be used to copy resources at any phase, by using the "resources:copy-resources" goal, as shown in the [[#Copy_Resources_at_an_Arbitrary_Phase|Copy Resources at an Arbitrary Phase]] section below. | |||
Variables can be replaced during when the resource plugin executes. For more details on how to specify variables to be replaced, see [[Maven Filtering and Property Substitution]]. | Variables can be replaced during when the resource plugin executes. For more details on how to specify variables to be replaced, see [[Maven Filtering and Property Substitution]]. | ||
Line 65: | Line 69: | ||
This will copy all text files found under src/main/java into ./target/classes and make them available in the class path. | This will copy all text files found under src/main/java into ./target/classes and make them available in the class path. | ||
<blockquote style="background-color: Gold; border: solid thin Goldenrod;"> | |||
:<br>If you specify just the <directory>src/main/java</directory> resource under <resources>, this will remove all other implicit resources, including <directory>src/main/resources</directory> so if you were relying on that (for <tt>log4j.xml</tt> or whatever else) you will need to add it explicitly, as shown in the example below.<br><br> | |||
</blockquote> | |||
<pre> | <pre> | ||
Line 87: | Line 93: | ||
</build> | </build> | ||
</pre> | </pre> | ||
=Java Source Code Filtering= | |||
<font color=red> | |||
Last time I tried to filter java source code with the following <resources> declaration, it did not work. Clarify why. | |||
</font> | |||
<syntaxhighlight lang='xml'> | |||
<build> | |||
<resources> | |||
<resource> | |||
<directory>src/main/java/io/novaordis/gld/extensions/jboss/datagrid</directory> | |||
<includes> | |||
<include>JBossDatagrid7Service.java</include> | |||
</includes> | |||
<filtering>true</filtering> | |||
</resource> | |||
</resources> | |||
</build> | |||
</syntaxhighlight> | |||
=Copy Resources at an Arbitrary Phase= | |||
We may want to copy resources at an arbitrary phase, to make sure they are not overwritten by the result of previous phases: | |||
<syntaxhighlight lang='xml'> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-resources-plugin</artifactId> | |||
<version>3.0.2</version> | |||
<executions> | |||
<execution> | |||
<phase>prepare-package</phase> | |||
<goals> | |||
<goal>copy-resources</goal> | |||
</goals> | |||
<configuration> | |||
<outputDirectory>${build.outputDirectory}</outputDirectory> | |||
<resources> | |||
<resource> | |||
<directory>src/main/resources</directory> | |||
<includes> | |||
<include>log4j.xml</include> | |||
</includes> | |||
<filtering>true</filtering> | |||
</resource> | |||
</resources> | |||
</configuration> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
</syntaxhighlight> | |||
The <resource> elements path should be relative to the project directory. | |||
Note that the phase is "[[Maven_Concepts_-_Lifecycle#prepare-package|prepare-package]]", instead of the default "[[Maven_Concepts_-_Lifecycle#process-resources|process-resources]]". |
Latest revision as of 19:08, 2 March 2018
External
- http://maven.apache.org/plugins/maven-resources-plugin/
- http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
Internal
Overview
By default, the plugin reads the configuration declared in the <project><build><resources> and <project><build><testResources> sections and automatically executes its "resources:resources" and "resource:testResources" goals at the "process-resources" and "process-test-resources" lifecycle phases, respectively. When copying resources, it uses <project><build><outputDirectory> as destination and when copying test resources, it uses <project><build><testOutputDirectory> as destination.
The plugin can be used to copy resources at any phase, by using the "resources:copy-resources" goal, as shown in the Copy Resources at an Arbitrary Phase section below.
Variables can be replaced during when the resource plugin executes. For more details on how to specify variables to be replaced, see Maven Filtering and Property Substitution.
Resources
- http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
- http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Variable Substitution in Resource Files
Set "filtering" to true as follows, and all variables will be substituted on copy:
<project> ... <build> ... <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> ... </resources> ... </build> ... </project>
For more details on how to specify the variables to be replaced, see:
Copy Text Files in target and Final Artifact
<build> ... <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.txt</include> </includes> </resource> </resources> ... </build>
This will copy all text files found under src/main/java into ./target/classes and make them available in the class path.
If you specify just the <directory>src/main/java</directory> resource under <resources>, this will remove all other implicit resources, including <directory>src/main/resources</directory> so if you were relying on that (for log4j.xml or whatever else) you will need to add it explicitly, as shown in the example below.
<build> ... <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.txt</include> </includes> </resource> </resources> ... </build>
Java Source Code Filtering
Last time I tried to filter java source code with the following <resources> declaration, it did not work. Clarify why.
<build>
<resources>
<resource>
<directory>src/main/java/io/novaordis/gld/extensions/jboss/datagrid</directory>
<includes>
<include>JBossDatagrid7Service.java</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
Copy Resources at an Arbitrary Phase
We may want to copy resources at an arbitrary phase, to make sure they are not overwritten by the result of previous phases:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
The <resource> elements path should be relative to the project directory.
Note that the phase is "prepare-package", instead of the default "process-resources".