Using the Maven Resources plugin

The Resources plugin comes into picture to copy project resources to the output directory. The resources can be for the project to run or for the purpose of testing.

How to do it...

Let's start using the Maven Resources plugin by performing the following steps:

  1. Open the command prompt.
  2. Run the following command on the simple project that we created earlier:
    mvn process-resources
    
  3. Observe what happens:
    [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ simple-project ---
    [INFO] skip non existing resourceDirectory C:projectsapache-maven-cookbooksimple-projectsrcmain
    esources
    

How it works...

When we specify the process-resources phase, Maven executes the resources goal of maven-resources-plugin, which is bound to the process-resources lifecycle phase.

In the earlier project, there are no resources and hence, resources are not copied.

If you add a file in srcmain esources (as in the case of the project-with-resources project), you will see the following output:

[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ project-with-properties ---
[INFO] Copying 1 resource

You could also explicitly invoke the plugin's goal as follows:

mvn resources:resources

You could also invoke any phase following the process-resources phase, which will trigger resource processing as well:

mvn compile

There is a separate goal to copy test resources to provide separation of the main and test resources. Like project resources, the test resource processing can be invoked in three ways, which are as follows:

  • By specifying a phase that will automatically invoke phases before it:
    mvn process-test-resources
    
  • By explicitly stating the plugin's goal:
    mvn resources:testResources
    
  • By a phase following process-test-resources:
    mvn test
    

There's more...

What if we had resources in additional folders? The Maven Resources plugin allows us to configure these additional folders.

Let's say we have an additional resources folder, namely src/main/additional. We can configure the pom.xml file as follows:

<build>
   <resources>
     <resource>
         <directory>src/main/resources</directory>
     </resource>
     <resource>
         <directory>src/main/additional</directory>
     </resource>
   </resources>
   </build>

Now, run the following command:

mvn process-resources

Observe the output:

[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ project-with-additional-resources ---
[INFO] Copying 1 resource
[INFO] Copying 1 resource

The line Copying 1 resource repeats twice, indicating the copying happening from two folders.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10