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?
Asked
Active
Viewed 3.7k times
62

jFrenetic
- 5,384
- 5
- 42
- 67
-
That sounds wrong in my mind? You need it for compiling and runtime but not for Testing? What are you testing? – khmarbaise Aug 21 '12 at 13:08
-
1@khmarbaise I know it might sound strange. The problem is that I need to use one logback implementation version for compilation and runtime, but another one for tests (which comes as a transitive dependency from embedded-glassfish-all with `test scope`). – jFrenetic Aug 21 '12 at 13:15
-
If you have embedded glassfish your tests whould not tests things like this. This sounds like integration tests. – khmarbaise Aug 21 '12 at 13:38
-
Yep, we're doing some integration testing. But this doesn't really matter. I'm trying to find out if there is a way to exclude a dependency during certain phase. – jFrenetic Aug 21 '12 at 13:44
-
What about one profile for each logback implementation ? – gontard Aug 21 '12 at 15:47
-
@gontard I actually mentioned `profiles` in my question. I just can't figure out how to activate/deactivate a profile for some specific phase in one run. I'm afraid it's hardly possible. – jFrenetic Aug 22 '12 at 06:32
1 Answers
72
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.
-
4This is actually a very good soultion! Considering that unit and integration tests are executed by different plugins (`surefire` and `failsafe`), it's very convenient to manage classpath using plugin-specific configuration. – jFrenetic Aug 30 '12 at 20:41
-
4Heads'up. This does not play well with IntellJ Idea Test Runner. The `maven-surefire-plugin ` Exclude `classpathDependencyExclude ` configuration seems to be ignored. Meaning your only workaround on IntellJ Idea is to use the Terminal to run your test or the Maven Project tool window. [See open ticket](https://youtrack.jetbrains.com/issue/IDEA-122783) . That said this works with Eclipse. – DaddyMoe Jan 16 '18 at 18:58
-
This is now working in 2019.3 Intellij. Refer https://youtrack.jetbrains.com/issue/IDEA-122783 . – tuk Sep 14 '19 at 15:04
-
This doesn't work for me in Eclipse `Version: 2019-12 (4.14.0)` - it does work when running tests from the Maven command line. – Chris Wolf Oct 06 '21 at 19:34