【问题标题】:Adding all Maven dependencies to Arquillian将所有 Maven 依赖项添加到 Arquillian
【发布时间】:2012-10-11 16:03:20
【问题描述】:

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

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

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

【问题讨论】:

  • 你想达到什么目的?
  • 我的代码所具有的所有依赖项(ToStringBuilder、EqualsBuilder...)。我已经说过我需要 POM 中的所有依赖项。
  • 这如何回答我的问题?
  • 它没有。如果您的代码有一些依赖项,那么它应该从某个地方获取它们,对吗?您提到的两个类来自commons-lang。我不知道 arquillian 在这里做什么。
  • 拜托,当你无法回答时停下来。

标签: java jakarta-ee maven jboss-arquillian


【解决方案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 进行了测试。在documentation 上查看最新版本的 Arquillian 工件。

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

【讨论】:

    【解决方案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>
    

    最初的想法来自这里:How to I add Maven artifacts to my ShrinkWrap archives?

    【讨论】:

      【解决方案3】:

      这篇 dzone 文章 Using the ShrinkWrap Maven Resolver for Arquillian Tests 可能有用。它建议:

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

      当你不知道它在哪个工件中时,另一种查找类的方法是使用search.maven.org。这是您要查找的课程的搜索:

      它显示了包含该类的工件。但是,该类位于 API jar 中,您还需要上述和上述文章中提到的实现 jar。

      【讨论】:

        【解决方案4】:

        shrinkwrap-resolver-impl-maven 2.0.0-beta-1 或更高版本包含 org.jboss.shrinkwrap.resolver.api.maven.Maven 类。

        【讨论】:

          猜你喜欢
          • 2015-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-17
          • 2012-11-28
          • 1970-01-01
          • 2021-09-07
          • 2013-05-16
          相关资源
          最近更新 更多