Including and excluding additional resources

There are many situations where you will need to include additional resource files or folders for compilation or testing. You might also have to exclude specific files or folders. Let us see how we can do this.

Getting ready

Maven is set up on your system and is verified for work. To do this, refer to the first three recipes of Chapter 1, Getting Started.

How to do it...

  1. Open one of the Maven projects for which we need to include or exclude files or folders; for instance, project-with-include-exclude.
  2. Add the following to the build section of your pom file:
    <resources>
          <resource>
            <directory>src/resources/xml</directory>
            <includes>
              <include>*.xml</include>
            </includes>
          </resource>
          <resource>
            <directory>src/resources/json</directory>
            <includes>
              <include>include.json</include>
            </includes>
            <excludes>
              <exclude>exclude.json</exclude>
            </excludes>
          </resource>
    </resources>
  3. Run the following command:
    mvn resources:resources
    
  4. Observe the output:
    [INFO] --- maven-resources-plugin:2.6:resources (default-cli) @ project-with-include-exclude ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 2 resources
    [INFO] Copying 1 resource
    [INFO] ------------------------------------------------------------------------
    
  5. View the contents of the resources folder:
    How to do it...
  6. View the contents of the build output directory:
    How to do it...

How it works...

The resources goal of the Maven Resources plugin copies all the resources required by the source to build the output directory. This goal is bound to the process-resources phase, which is part of the default lifecycle.

By default, the goal copies over the contents of src/main/resources. When the resources tag is specified in the pom file, it copies the contents of the directories specified there, based on the include and exclude filters specified.

In our specific example, we did three things:

  • Included all the XML files in the src/resources/xml folder
  • Included a specific file in the src/resources/json folder
  • Excluded a specific file in the src/resouces/json folder

There's more...

What if we need to copy test resources selectively? For this, we would need to do the following:

  1. Add the following in the build section of your pom file:
    <testResources>
      <testResource>
        <directory>src/resources/xml</directory>
        <includes>
          <include>*.xml</include>
        </includes>
      </testResource>
      <testResource>
        <directory>src/resources/json</directory>
        <includes>
          <include>include.json</include>
        </includes>
        <excludes>
          <exclude>exclude.json</exclude>
        </excludes>
      </testResource>
    </testResources>
  2. Run the following command:
    mvn resources:testResources
    
  3. View the contents of the test-classes folder:
    There's more...

This will now copy over the specified test resources to the test output directory (target/test-classes).

We saw that the resources and testResources goals copied resources to classes and test-classes respectively. What if we need to copy these to specific folders, For instance, xml files to the xml folder and json files to the json folder? The add-resource and add-test-resource goals of the Build Helper Maven plugin come to our assistance here.

  1. Update the pom file with the following code:
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.9.1</version>
      <executions>
        <execution>
          <id>add-resource</id>
          <phase>generate-resources</phase>
          <goals>
            <goal>add-resource</goal>
          </goals>
          <configuration>
            <resources>
              <resource>
                <directory>src/resources/xml</directory>
                <targetPath>xml</targetPath>
              </resource>
              <resource>
                <directory>src/resources/json</directory>
                <targetPath>json</targetPath>
                  <includes>
                    <include>include.json</include>
                  </includes>
                  <excludes>
                    <exclude>exclude.json</exclude>
                  </excludes>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
  2. Run the following command:
    mvn compile
    
  3. Examine the target/classes folder now.

You will see the xml and json subfolders with their respective content.

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

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