A JBoss Project
Red Hat

How to deploy your web application test resources during development

posted by Fred Bricon on Dec 08, 2014.

jbosscentral maven m2e m2e-wtp web-resources

Comments

I previously decribed how to use a combination of maven-war-plugin webresources and Maven profiles, in order to deploy minified resources. In this article, we’ll expand on these concepts a bit, to demonstrate how to deploy test resources, in a somewhat elegant and portable way.

User story

In some cases, you want your web application to deploy test resources, whether it’s logging or database configuration files, or even some test mocks during development.

When you’re using Eclipse Java EE, m2e-wtp explicitely prevents test resources from being deployed (in order to limit behavior discrepancies between Maven CLI and Eclipse), and if you try to mess with Eclipse’s Deployment Assembly page, its settings will be reset next time you perform a Maven > Update projet configuration.

So you need a solution that works for both Eclipse, command line and even other IDEs as well. Let’s see how you can achieve that…​

Pre-requisites

You only need :

  1. a Java EE based Eclipse distribution for the following to work: stock Eclipse, JBoss Developer Studio or SpringSource Tools Suite for instance, as long as m2e-wtp is installed with it.

  2. a Maven project with packaging war

Configure a new dev profile

In order to deploy test resources with WTP, we need to add a new Maven dev profile to the <profiles> section of your pom.xml. This can easily be done with Ctrl+space assist. Selecting the m2e profile template and changing the id to dev will get you started.

The profile is automatically enabled when running in m2e, via the m2e.version property.

Then, you need to add a maven-war-plugin configuration to the <build><plugins> section of the new profile, and configure <webResources> so that test resources from the test output directory are copied to WEB-INF/classes, using a regexp to only include specific files.

Eventually, this is how your dev profile should look :

<profile>
  <id>dev</id>
  <activation>
    <property> <!-- this will automatically be enabled when using m2e -->
    <name>m2e.version</name>
    </property>
  </activation>
  <build>
    <plugins>
    ...
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <!-- this will inherit existing maven-war-plugin configuration-->
        <configuration>
          <webResources>
            <resource>
              <directory>${project.build.testOutputDirectory}</directory>
              <includes>
                <include>**/some/test/resources/**</include>
              </includes>
              <targetPath>WEB-INF/classes/</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

In Eclipse, the matching test resources are not served directly but are actually copied/processed to target/m2e-wtp/web-resources/WEB-INF/classes and deployed from there.

If the original configuration already defines <webResources>, use <webResources combine.children="append"> in the dev profile, so all resources get deployed. You can learn more about merging plugin configuration on the Sonatype blog.

If you decide to declare this profile in a parent pom, don’t forget to put the <plugins> node under the <pluginManagement> section, or else, these configurations will not be inherited by your war projects.

This extra configuration will be merged to your existing maven-war-plugin configuration (check the maven-war-plugin configuration in the Effective POM tab of the pom.xml editor).

Now, using m2e-wtp, every time a test resource is modified, it will automatically be deployed to your server, on-the-fly. This is borderline magic, I know.

Conclusion

Hopefully, this article gave you a glimpse of the powerful Maven capabilities m2e-wtp brings to Eclipse, showing how you can easily deploy your test resources in your development environment in a portable way.

Take it easy!

Latest posts

JBoss Tools 4.29.1.Final for Eclipse 2023-09

by Stéphane Bouchet on Jun 13, 2024.

JBoss Tools 4.29.0.Final for Eclipse 2023-09

by Stéphane Bouchet on Nov 02, 2023.

JBoss Tools 4.28.0.Final for Eclipse 2023-06

by Stéphane Bouchet on Jul 03, 2023.

JBoss Tools for Eclipse 2023-06M2

by Stéphane Bouchet on Jun 05, 2023.

JBoss Tools 4.27.0.Final for Eclipse 2023-03

by Stéphane Bouchet on Apr 07, 2023.

Looking for older articles ? See the Archived entries.
back to top