halfwarp
halfwarp

Reputation: 1790

Maven same dependency on different scopes

I have a maven (3) project with multiple submodules. One on of them, I want it to explicitly depend on a dependency, without depending on its transitive dependencies. To do this, I'm using the folloing:

<dependency>
  <groupId>org.example</groupId>
  <artifactId>foo</artifactId>
  <version>${project.version}</version>
  <exclusions>
    <exclusion>
      <groupId>*</groupId>
      <artifactId>*</artifactId>
    </exclusion>
  </exclusions>
</dependency>

This works as itended. During runtime, the transitive dependencies of "foo" will already be in the classpath, so everything goes well.

However, I need "foo"'s transitive dependencies for test. I tried to also declare a dependency of "foo" with scope test, but it seems that it conflicts with the one that excludes everything. It either works as expected in tests but fails in runtime, or vice-versa.

Do you known if something like this is possible with maven?

Upvotes: 3

Views: 2060

Answers (2)

carlspring
carlspring

Reputation: 32607

You should check out Maven's <scope>import</scope> dependencies. Create a <profile/> for testing and define a dependency to a pom which contains these test dependencies you're interested in. (This might require some re-working of your pom.xml-s, but would be a good approach).

Check here for more details.

Upvotes: 0

Gab
Gab

Reputation: 8323

Of course it's possible, you need to declare 2 different maven profiles holding your dependencies.

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Different dependencies for different build profiles in maven

Upvotes: 1

Related Questions