I need artifact A. So, I included it in my pom.xml
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>some.version</version>
</dependency
The artifact A uses artifact B which I absolutely don't want to be copied to my classpath. So, I used exclusions for B
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>some.version</version>
<exclusions>
<exclusion>
<groupId>dontwant.group</groupId>
<artifactId>dontwant.artifact</artifactId>
</exclusion>
<exclusions>
</dependency
I don't have a dependency on B. But, I do have dependency on A and A depends on B. B is now not being copied to the classpath. Can this break my code ?
I need artifact A. So, I included it in my pom.xml
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>some.version</version>
</dependency
The artifact A uses artifact B which I absolutely don't want to be copied to my classpath. So, I used exclusions for B
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>some.version</version>
<exclusions>
<exclusion>
<groupId>dontwant.group</groupId>
<artifactId>dontwant.artifact</artifactId>
</exclusion>
<exclusions>
</dependency
I don't have a dependency on B. But, I do have dependency on A and A depends on B. B is now not being copied to the classpath. Can this break my code ?
Share Improve this question asked Nov 28, 2024 at 10:29 AnuragAnurag 4512 silver badges13 bronze badges 1- It won't compile if you exclude a dependency your code needs. – Essex Boy Commented Nov 28, 2024 at 11:06
2 Answers
Reset to default 1You use dependency A, A uses dependency B, but you excluded B from A. What is going to happen?
- Your compilation may or may not break, depending on how A exactly depends on B. But this is not the real issue
- At runtime, when you use A, and A tries to call B, the code will break. ClassNotFoundException, to be precise. There are cases in which this does not happen, e.g. if A uses B only in some obscure methods and you never call those methods. But, as a rule of thumb, it will probably fail.
So avoid the exclusions, and put B onto the classpath unless you have very good reason not to (and you should add that reason to the question).
Dependencies include other dependencies as children. You can exclude those children if you don't want them.
Usually you want to exclude some children because you already have this dependency somewhere else.
If you exclude something which is required for compilation the build will fail.
You can look at the effective tree with dependency:tree
mvn dependency:tree
So the answer to your question is no.