Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding all Maven dependencies to Arquillian

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?

like image 693
LuckyLuke Avatar asked Oct 21 '12 19:10

LuckyLuke


People also ask

How do I fix missing Maven dependencies in eclipse?

Right-click on the project and choose Properties, and then Maven. Uncheck the box labeled "Resolve dependencies from Workspace projects" Hit Apply, and then OK. Right-click again on your project and do a Maven->Update Snapshots (or Update Dependencies)

What happens when we add dependency in POM xml?

If you have only the standard maven plugins from the default superpom, adding a dependency will: download the specified version to your local repository. use it to compile. use it to run tests.


1 Answers

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.

like image 106
cassiomolin Avatar answered Sep 23 '22 20:09

cassiomolin