maven - Can excluding transitive dependencies break my code? - Stack Overflow

I need artifact A. So, I included it in my pom.xml<dependency><groupId>some.group<grou

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
Add a comment  | 

2 Answers 2

Reset to default 1

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745515277a4630957.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信