使用Arquillian Shrinkwrap Resolver Maven从pom.xml读取依赖项

时间:2013-11-27 07:32:03

标签: java maven jboss-arquillian shrinkwrap dependency-resolver

我正在尝试使用ShrinkWrap Dependency Resolver将库添加到我的ShrinkWrap Archive中,以将其部署到服务器进行Arquillian测试。

我正在使用以下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>

我的测试看起来像这样:

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");
    }
}

如果我不解决itextpdf的依赖性,它会给我发现NoDefClass错误,到目前为止还可以。因此,它希望从pom的依赖项中添加库。从pom中获取它非常重要,因为测试在不同的机器上运行。

使用ShrinkWrap Resolver 2.0.0时出现的错误如下:

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)
[...]

当我捕捉到所有异常时,还会抛出以下内容:

RuntimeException: Could not create object from user view

我能找到它的来源: https://github.com/shrinkwrap/resolver/blob/master/api/src/main/java/org/jboss/shrinkwrap/resolver/api/ResolverSystemFactory.java

到目前为止,我不知道为什么会收到此错误,因为Google发现的所有消息都告诉我,系统正在运行。任何想法?

1 个答案:

答案 0 :(得分:1)

看起来这个问题是因为WildFly POM指定了maven-aether-provider的更新版本。在3.1.0(来自WildFly),它依赖org.eclipse.aether:aether-[api|impl|spi],其中3.0.5(来自Shrinkwrap Resolvers)使用org.sonatype.aether:aether-[api|impl|spi]。如果查看已解析的依赖项,则会在类路径中同时包含ClassCastException。添加以下依赖项应该可以解决此问题:

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