A JBoss Project
Red Hat

Latest posts

I previously decribed how to use a combination of maven-war-plugin webresources and Maven profiles. 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!

We are working on our plans for JBoss Tools and Eclipse in 2015 and got some high level topics we are looking into working on and was wondering what you would like to see improved.

Overall features

The overall features we are currently planning to work on in 2015 for our core tools (excluding Integration stack plugins) is listed below at a high level.

  • (Hybrid) Mobile Tooling

  • HTML5/JavaScript/AngularJS

  • Making Eclipse Better

  • Docker/Vagrant

  • JBoss Modules support

  • Java EE 7 (Server, Batch, Web Services, JAX-RS, etc.)

  • OpenShift 3 (kubernetes)

  • Persistence tools (hibernate, JPA, querying, etc.)

Make your voice heard

You can make your voice heard by going to http://twtsurvey.com/jbosstools2015 and answer the 2 simple questions!

…​and we do have a 'Other' field where you can let us know if we forgot something above ? :)

Thanks for your input!

Have fun!
Max Rydahl Andersen
@maxandersen

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