Search code examples
javamavenjboss-arquillianshrinkwrapdependency-resolver

Using Arquillian Shrinkwrap Resolver Maven reading dependencies from the pom.xml


I am trying to use the ShrinkWrap Dependency Resolver to add libraries to my ShrinkWrap Archive to deploy it to the server for Arquillian Tests.

I am using the following pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>xxx</groupId>
  <artifactId>ArqTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <version.junit>4.11</version.junit>
    <version.arquillian>1.1.1.Final</version.arquillian>
    <version.wildfly.as>8.0.0.Beta1</version.wildfly.as>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.4.4</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-api</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-spi</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-api-maven</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-spi-maven</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.shrinkwrap.resolver</groupId>
      <artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.arquillian.junit</groupId>
      <artifactId>arquillian-junit-container</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jboss.arquillian.protocol</groupId>
      <artifactId>arquillian-protocol-servlet</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-version</artifactId>
        <version>${version.wildfly.as}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-bom</artifactId>
        <version>2.0.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${version.junit}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>${version.arquillian}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-drone-bom</artifactId>
        <version>${version.arquillian}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>arquillian-tutorial</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>jbossas-managed-wildfly-8</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <testFailureIgnore>true</testFailureIgnore>
              <systemPropertyVariables>
                <arquillian.launch>jbossas-managed-wildfly-8</arquillian.launch>
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <dependencies>
        <dependency>
          <groupId>org.wildfly</groupId>
          <artifactId>wildfly-spec-api</artifactId>
          <version>8.0.0.Beta1</version>
          <type>pom</type>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.wildfly</groupId>
          <artifactId>wildfly-arquillian-container-managed</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.wildfly</groupId>
          <artifactId>wildfly-arquillian-protocol-jmx</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.wildfly</groupId>
          <artifactId>wildfly-arquillian-common</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
</project>

My Test looks like this:

import java.io.File;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolverSystem;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.itextpdf.text.Document;

@RunWith(Arquillian.class)
public class ResolverTest
{

    @Deployment
    public static WebArchive createDeployment()
    {
        WebArchive jar = ShrinkWrap.create(WebArchive.class)
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

        File[] f = null;

        try
        {
            f = Maven.resolver().resolve("com.itextpdf:itextpdf:5.4.4").withTransitivity().as(File.class);
            jar.addAsLibraries(f);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            System.exit(1);
        }

        return jar;
    }

    @Test
    public void shouldCreateDocument()
    {
        Document d = new Document();
        d.addAuthor("Marc");
    }
}

If I don't resolve the itextpdf dependcy, it gives me a NoDefClass found error, what is ok so far. Hence, it want to add the library from the dependency entry of the pom. It is important to get it from the pom because the tests are running on different machines.

The error I get when using the ShrinkWrap Resolver 2.0.0 is the following:

java.lang.ClassCastException: Cannot cast org.apache.maven.repository.internal.DefaultVersionResolver to org.sonatype.aether.impl.VersionResolver
    at java.lang.Class.cast(Class.java:3094)
[...]
java.lang.IllegalArgumentException: version resolver has not been specified
    at org.sonatype.aether.impl.internal.DefaultRepositorySystem.setVersionResolver(DefaultRepositorySystem.java:164)
[...]

As I am catching all exceptions, the following is also thrown:

RuntimeException: Could not create object from user view

I was able to locate the source of this in: https://github.com/shrinkwrap/resolver/blob/master/api/src/main/java/org/jboss/shrinkwrap/resolver/api/ResolverSystemFactory.java

By now I don't know why I get this error because all sources found with Google tells me, the system is running. Any Ideas?


Solution

  • It looks like this issue is because the WildFly POMs specify a newer version of the maven-aether-provider. In 3.1.0 (from WildFly) it has a dependency on org.eclipse.aether:aether-[api|impl|spi] where 3.0.5 (from Shrinkwrap Resolvers) uses org.sonatype.aether:aether-[api|impl|spi]. If you look at your resolved dependencies you'll have both of them in your classpath which causes the ClassCastException. Adding the following dependency should clear up this problem:

     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-aether-provider</artifactId>
       <version>3.0.5</version>
       <scope>test</scope>
     </dependency>