将所有 Maven 依赖项添加到 Arquillian [英] Adding all Maven dependencies to Arquillian

查看:34
本文介绍了将所有 Maven 依赖项添加到 Arquillian的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我找到了那行,但我 Maven 在 intellij 中是红色的,因为它没有找到类.我不知道我需要哪些依赖项.或者有更好的方法吗?

解决方案

添加 Arquillian 依赖

将 Arquillian 依赖项添加到您的 pom.xml:

<依赖项><依赖><groupId>org.jboss.arquillian</groupId><artifactId>arquillian-bom</artifactId><version>1.1.8.Final</version><范围>导入</范围><type>pom</type></依赖></依赖项></dependencyManagement>

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

<groupId>org.jboss.shrinkwrap.resolver</groupId><artifactId>shrinkwrap-resolver-impl-maven</artifactId><范围>测试</范围></依赖>

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

<groupId>org.jboss.arquillian.junit</groupId><artifactId>arquillian-junit-container</artifactId><范围>测试</范围></依赖>

导入 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(文件);

如果您使用 JUnit,这就是您的测试类的样子

import org.jboss.arquillian.container.test.api.Deployment;导入 org.jboss.arquillian.junit.Arquillian;导入 org.jboss.shrinkwrap.api.ShrinkWrap;导入 org.jboss.shrinkwrap.api.asset.EmptyAsset;导入 org.jboss.shrinkwrap.api.spec.WebArchive;导入 org.jboss.shrinkwrap.resolver.api.maven.Maven;导入 org.junit.Test;导入 org.junit.runner.RunWith;导入静态 org.junit.Assert.*;导入 java.io.File;@RunWith(Arquillian.class)公共类 MyTestClassWithMavenDependencies {@部署公共静态 WebArchive createDeployment() {//导入Maven运行时依赖文件 [] 文件 = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().解决().withTransitivity().asFile();//创建部署文件WebArchive war = ShrinkWrap.create(WebArchive.class).addClass(MyClass1.class).addClass(MyClass2.class).addClass(MyClass3.class).addAsLibraries(文件);//显示部署结构System.out.println(war.toString(true));回归战争;}//在这里创建你的测试}

<小时>

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

注意 2:有关如何解决依赖项的更多详细信息,请查看 ShrinkWrap Resolvers 文档.

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?

解决方案

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.

这篇关于将所有 Maven 依赖项添加到 Arquillian的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆