[TIP] TomEE/OpenEJB Arquillian and Maven resolver


New ShrinkWrap Maven resolver is awesome to avoid to handle all dependencies manually but it can be a bit long to resolve dependencies. We just introduced a hack in TomEE and OpenEJB adapters to make it less painful. The idea is quite simple: do the resolution while the container is starting up.

To use it simply use the property “preloadClasses” in arquillian.xml listing some classes to preload. You’ll typically initialize in a static block your dependencies in a Future.

Here is a sample arquillian.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jboss.org/schema/arquillian
                      http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
  <container qualifier="openejb" default="true">
    <configuration>
      <property name="preloadClasses">org.superbiz.Dependencies</property>
    </configuration>
  </container>
</arquillian>

And here is the Dependencies class:

package org.superbiz;

import org.jboss.shrinkwrap.resolver.api.maven.Maven;

import java.io.File;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Dependencies {
    private static final Future<File[]> DEPENDENCIES;
    static {
        System.out.println("GO");
        final ExecutorService es = Executors.newSingleThreadExecutor();
        DEPENDENCIES = es.submit(new Callable<File[]>() {
            @Override
            public File[] call() throws Exception {
                return Maven.resolver()
                    .offline()
                    .loadPomFromFile("pom.xml")
                    .importCompileAndRuntimeDependencies()
                    .resolve().withTransitivity()
                    .asFile();
            }
        });
        es.shutdown();
    }

    public static File[] get() {
        System.out.println(new Date());
        try {
            return DEPENDENCIES.get(); // block if not done or return immediately
        } catch (final Exception e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println(new Date());
        }
    }
}

Of course you can use this idea for anything taking time in your client tests!

Have a nice hacking 🙂

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s