Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting a transitive dependency to runtime scope in Maven

Tags:

maven

maven-3

I've got 2 projects using Maven. The first one is a library containing utility classes and methods. The second project is an actual application that has the library as a dependency. My library uses internally a third-party library.

So these are the dependencies:

  • My library: depends on the third-party library
  • My application: depends on my library

However, I don't want the third-party library classes to be available at compile time in my application. This is because the application is supported by a large team and I want to prevent people from accidentally using methods from the third-party library in the application given that some class names and some method names are similar between the two. Of course the third-par ty library will have to be available in my application at runtime.

If the scope for all my dependencies was compile, it wouldn't achieve my goal. Is there a way to achieve this in Maven 3?

like image 800
Juanal Avatar asked Jun 15 '12 03:06

Juanal


People also ask

How do you exclude a transitive dependency in Maven?

Exclude the transitive dependencyOpen the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

Is runtime scope transitive?

Runtime:It is transitive. So, the project packaging includes all the transitive dependencies.

How does Maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

How do you exclude transitive dependency of transitive dependency?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.


Video Answer


2 Answers

Very good question and unfortunately you can't do this using Maven 3, or 2, or any other version, because of its fundamental design. What you're asking about is actually a desired and ideal behaviour since in fact any artifact's compile dependencies should be transitive with runtime scope. However, design like this leads to some problems. As you can read at Maven's Introduction to the Dependency Mechanism about compile scope:

It is intended that [transitive dependencies of a compile dependency which are themselves compile dependencies should be considered] runtime scope instead, so that all compile dependencies must be explicitly listed - however, there is the case where the library you depend on extends a class from another library, forcing you to have available at compile time. For this reason, compile time dependencies remain as compile scope even when they are transitive.

So, as you see, what you require is actually the proper design of this behaviour which is unfortunately impossible to implement.

like image 102
Michał Kalinowski Avatar answered Oct 14 '22 23:10

Michał Kalinowski


Nothing has changed during the last three years, so Michal's answer is still correct: There is no way to limit transitive visibility in Maven.

However, you should consider redesigning your library to split it in an api-artifact that is necessary as compile time dependency and which itself does not depend on the third party library and an implementation artifact which is only needed as runtime-dependency and which depends on the third party library.

like image 23
Reto Gmür Avatar answered Oct 14 '22 23:10

Reto Gmür