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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With