Adding all Maven dependencies to Arquillian

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 10:21:53

问题


How do you add all dependencies in the POM to arquillian?

Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies()
                .as(File.class);

I found that line, but I Maven is red in intellij because it doesn't find the class. I don't know which dependencies I need. Or are there better ways?


回答1:


Adding Arquillian dependencies

Add Arquillian dependencies to your pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.8.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

Add the ShrinkWrap resolver (Maven implementation) to your pom.xml:

<dependency>
    <groupId>org.jboss.shrinkwrap.resolver</groupId>
    <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
    <scope>test</scope>
</dependency>

If you are using JUnit, add the Arquillian JUnit container to your pom.xml:

<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency>

Importing Maven dependencies

In your test class, within the method annotated with @Deployment, import the runtime dependencies with the following line:

File[] files = Maven.resolver().loadPomFromFile("pom.xml")
        .importRuntimeDependencies().resolve().withTransitivity().asFile();

And add the dependencies to your deploy using the method addAsLibraries(files):

WebArchive war = ShrinkWrap.create(WebArchive.class)
                           .addClass(MyClass1.class)
                           .addClass(MyClass2.class)
                           .addClass(MyClass3.class)
                           .addAsLibraries(files);

This is how your test class will look like if you are using JUnit

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.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import java.io.File;

@RunWith(Arquillian.class)
public class MyTestClassWithMavenDependencies {

    @Deployment
    public static WebArchive createDeployment() {

        // Import Maven runtime dependencies
        File[] files = Maven.resolver()
                            .loadPomFromFile("pom.xml")
                            .importRuntimeDependencies()
                            .resolve()
                            .withTransitivity()
                            .asFile();

        // Create deploy file    
        WebArchive war = ShrinkWrap.create(WebArchive.class)
                                   .addClass(MyClass1.class)
                                   .addClass(MyClass2.class)
                                   .addClass(MyClass3.class)
                                   .addAsLibraries(files);

        // Show the deploy structure
        System.out.println(war.toString(true)); 

        return war;
    }

    // Create your tests here
}

Note 1: The above solution has been tested with Arquillian 1.1.8.Final. Check the most recent version of Arquillian artifacts on the documentation.

Note 2: For more details on how to resolve dependencies, have a look at the ShrinkWrap Resolvers documentation.




回答2:


I had the same error as you. It's because of incorrect shrinkwrap versions. See correct versions in the second piece of code.

package alehro.testgf2;

import java.io.File;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import alehro.p1.C1;
import alehro.p1.C2;

@RunWith(Arquillian.class)
public class Test1 {

    @Deployment
    public static Archive<?> createTestArchive()  {
         File[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().asFile();


        WebArchive res = ShrinkWrap.create(WebArchive.class, "test.war");
        for(File file : libs){
            res.addAsLibrary(file);
        }       
        res.addPackages(true, "alehro.p1");

        return res;          

    }
    @Test
    public void test1(){
            //third party lib
        DateTimeFormatter inputDF = DateTimeFormat.forPattern("d MMM yyyy HH:mm:ss Z");
        DateTime dt1 = inputDF.parseDateTime("28 Nov 2012 23:23:23 +04");           
        Assert.assertNotNull(dt1);  

            //all classes from alehro.p1
        Assert.assertEquals("Failure", "I'm C1", C1.getMyName()) ;
        Assert.assertEquals("Failure", "I'm C2", C2.getMyName()) ;

    }
}

I had a harsh time resolving maven dependencies. Below is working solution, but I'm sure it can be simplified more.

<dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <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.container</groupId>
        <artifactId>arquillian-glassfish-remote-3.1</artifactId>
        <scope>test</scope>
        <version>1.0.0.CR3</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-api</artifactId>
        <version>2.0.0-alpha-5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
        <version>2.0.0-alpha-5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.shrinkwrap.resolver</groupId>
        <artifactId>shrinkwrap-resolver-api-maven</artifactId>
        <version>2.0.0-alpha-5</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-bom</artifactId>
            <version>2.0.0-alpha-5</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.0.3.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>


    </dependencies>
</dependencyManagement>

Original idea is from here: How to I add Maven artifacts to my ShrinkWrap archives?




回答3:


This dzone article Using the ShrinkWrap Maven Resolver for Arquillian Tests which might be useful. It suggests:

<dependency>
  <groupId>org.jboss.shrinkwrap.resolver</groupId>
  <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
</dependency>

Another way you can find a class when you don't know which artifact it's in is to use search.maven.org. Here's a search for the class you are looking for:

  • fc:"org.jboss.shrinkwrap.resolver.api.maven.Maven"

It shows the artifacts containing that class. However, the class is in an API jar, and you'll also want the implementation jar as mentioned above and in the aformentioned article.




回答4:


The org.jboss.shrinkwrap.resolver.api.maven.Maven class is included by shrinkwrap-resolver-impl-maven 2.0.0-beta-1 or later.



来源:https://stackoverflow.com/questions/13001371/adding-all-maven-dependencies-to-arquillian

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!