将所有 Maven 依赖项添加到 Arquillian

2022-09-01 16:56:15

如何将 POM 中的所有依赖项添加到 arquillian 中?

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

我找到了那条线,但我在intellij中是红色的,因为它找不到该类。我不知道我需要哪些依赖项。还是有更好的方法?Maven


答案 1

添加 Arquillian 依赖项

将 Arquillian 依赖项添加到您的 :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>

将 ShrinkWrap 解析器(Maven 实现)添加到您的 :pom.xml

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

如果您使用的是 JUnit,请将 Arquillian JUnit 容器添加到您的 :pom.xml

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

导入 Maven 依赖项

在测试类中,在 带 注释的方法中,使用以下行导入运行时依赖项:@Deployment

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

并使用以下方法将依赖项添加到部署中:addAsLibraries(files)

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

这是使用 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
}

注1:上述解决方案已经过Arquillian 1.1.8.Final的测试。在文档中查看最新版本的 Arquillian 工件。

注2:有关如何解析依赖项的更多详细信息,请查看 ShrinkWrap 解析程序文档


答案 2

我和你有同样的错误。这是因为不正确的收缩包装版本。请参阅第二段代码中的正确版本。

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()) ;

    }
}

我在解决 maven 依赖关系方面度过了一段艰难的时光。以下是工作解决方案,但我相信它可以进一步简化。

<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>

最初的想法来自这里:如何将Maven工件添加到我的ShreshWrap存档中?


推荐