Multiple versions of the same dependency in Maven

前端 未结 6 1985
抹茶落季
抹茶落季 2020-11-29 06:19

Is it possible to declare multiple versions of the same dependency in a Maven repo?

I need these dependencies all at once:

    

        
6条回答
  •  离开以前
    2020-11-29 07:01

    No, you can't depend 2 versions of the same artifact, normally.
    But you can include them so they end up in the resulting application.

    That requirement is sometimes valid - for instance, when the maintenance of that library is poor, and they rename some packages and release that as a minor version of the same artifact. Then, other projects have it as a 3rd party dependency and need the same classes under different FQCN.

    For such cases, you can for instance use the maven-shade-plugin.

    • Create a maven project with a single dependency, one of the versions you need.
    • Add the shade plugin and let it create a shaded jar. It will basically re-wrap the classes under a different artifact G:A:V.
    • Do this for all versions you need.
    • You may use classifier to differentiate the shaded versions.
    • In your project, depend on these artifacts.
    • Finally, exclude the original dependencies, give them the scope "provided".

    You can use different variations of the same, which, in the end, will put those classes to your classpath. For instance, use the dependency:copy-dependency plugin/goal, and install that jar to your local repo during the build. Or unzip the classes right into your ${project.build.outputDirectory} (target/classes).

提交回复
热议问题