4
votes

I recently learned that gradle has api/implementation "scopes" for dependencies, and I was trying to figure out if there's a maven equivalent of the implementation in gradle. None of the maven dependency scopes seem exactly right for this - provided makes it not a runtime dependency, compile/runtime don't seem do the correct thing, ... and so it seems that there's not a direct equivalent.

Basically, I have a dependency of my library that is required at compile-time (for my library)/runtime (for code that uses my library), but I don't want to be in the compile classpath of code that depends on my library. Is this possible to do with maven?

3
If I correctly understand your requirement the scope runtime is exactly what you are searching for...khmarbaise

3 Answers

2
votes
  • Such dependency should have scope=compile when declared in your lib. This way it will be available during compilation of the lib.
  • But it should have scope=runtime when declared in dependencyManagement section of other modules that depend on your lib. This way it won't be present in the classpath when compiling other modules.
0
votes

AFAIK this is not possible.

If you want to avoid unintentional use of transitive dependencies in your code, you can use dependency:analyse or dependency:analyze-only.

The latter one allows you to fail the build if you directly use classes from transitive dependencies.

0
votes

Can you be more specific and gives an example of the architecture (Module, sub-modules)?

If I have the below modules:

  • A: has a dependency to dependency x scope compile
  • B: has a dependency to dependency A scope compile

Now I want x to be in the classpath and compiled during A build but i don't want x in the B classpath

Well, you will get x in B because it's a transitive dependency but you can easily exclude it (when you declare the dependency A into B), so it wont be in the classpath.

<dependencies>
    <dependency>
      <groupId>test.test</groupId>
      <artifactId>B</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>test.test</groupId>
          <artifactId>x</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

For more info: https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html