Home > Software engineering >  A question about best practice on multiple dependency jar versions when packing war with maven
A question about best practice on multiple dependency jar versions when packing war with maven

Time:03-09

I have a maven war project with submodules. One module uses google-api-client, another use google-cloud-storage. I draw some of their dependencies below

A
|-google-api-client:jar:1.33.1
  |-google-http-client-gson:jar:1.41.1

B
|-google-cloud-storage:jar:2.4.4
  |-google-api-client:jar:1.33.1
  |-google-http-client-gson:jar:1.41.2

When packaging wars, both gson 1.41.1 and 1.41.2 will be packaged. I know maven has a nearest rule to determine which jar to use when compiling. But when the webserver loads my project I have no control to which jar will be loaded first. So I want to keep only a newer version for each jar.

I know that I can add <exclusion> tags to the dependencies and add a new dependency to tell maven to use a specific version of jars. However, I am not sure if that is the best practice because it requires me to go through the dependencies of third-party libraries. There are just too many of them.

Any suggestions on how to handle the multiple versions of jars properly?

CodePudding user response:

A good practice I recommend is to use enforcer Plugin with dependency convergence goal. This way you are forced to decide which version will be on the class path. Of course it might be additional effort because you have to handle conflicts (also by setting exclusions), but in the end it's well defined, which versions you get.

  • Related