Question

I need to create a library which depends on Spring 3. But this library will be used in some projects which use Spring 3, and simultaneously in some newer projects which use Spring 4. How should I configure the Spring dependency in my library and the dependencies in the project which use them?

I don't want to have two separate versions of the lib for Spring 3 and Spring 4.

Was it helpful?

Solution

Maven has a concept of the nearest definition, which means that you can declare your preferred version of a dependency in your POM and it will override any different versions used transitively by your other dependencies.

So your newer projects should declare a Spring 4 dependency in their POM. Provided the groupId and artifactId are the same between Spring 3 and 4 (which I believe they are), the new project will only have Spring 4 classes in its classpath.

So then you have to hope that Spring 4 is backwards-compatible with Spring 3, otherwise your library may encounter runtime problems. Perhaps see Spring 4.0.0 backward compatibility.

OTHER TIPS

One option is to define a property like spring.version on your parent pom.xml.

Your library then uses this library from the parent, which you can set according to your needs.

Take also in Introduction to the Dependency Mechanism. Dependency management might be a good option too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top