Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude maven dependency for tests

Tags:

maven

maven-3

I have a dependency that is needed for a compilation and runtime but I want to exclude it when running tests. Is this possible? Maybe, by setting up a profile? But how do I deactivate it only for test lifecycle phase?

like image 506
jFrenetic Avatar asked Aug 21 '12 10:08

jFrenetic


People also ask

How you can exclude dependency in Maven?

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows. The dependency is also excluded from the Project and Maven tool windows.

How do I exclude test cases in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

How do I make Maven dependency optional?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.

How do you exclude a transitive dependency in Maven?

There we can exclude all transitive dependencies without specifying groupId and artifactId of the dependencies. So need to use astric(*) character as groupid and artifactid of the dependency. This wildcard transitive dependencies ignoring is available with maven 3.2.


1 Answers

You could (re)configure the classpath during the test phase thanks to the maven surefire plugin. You can add classpath elements or exclude dependencies.

<project>   [...]   <build>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-surefire-plugin</artifactId>         <version>2.12.2</version>         <configuration>           <additionalClasspathElements>             <additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>             <additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>           </additionalClasspathElements>           <classpathDependencyExcludes>             <classpathDependencyExclude>org.apache.commons:commons-email</classpathDependencyExclude>           </classpathDependencyExcludes>         </configuration>       </plugin>     </plugins>   </build>   [...] </project> 

As noted by @jFrenetic you could do the same with maven-failsafe-plugin.

like image 166
gontard Avatar answered Oct 23 '22 11:10

gontard