0

So I have a dependency:

<dependency>
            <groupId>com.foo</groupId>
            <artifactId>foo</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
</dependency>

I need this dependency to be in version 2.4 at some times and at 2.2 at other times. Is there a way to choose just before runtime which version it's going to run, or maybe a maven command to do so?

And given the scope is provided, if I just set the version to 2.2, when provided with the 2.4 version will it work?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    At runtime? No. Reason: the project is compiled against a specific dependency version. You could, however, set the dependency through a [profile](https://maven.apache.org/guides/introduction/introduction-to-profiles.html). The question is why would you want to change the version of a dependency? – Turing85 Aug 07 '20 at 16:13
  • Does this answer your question? [How to refer environment variable in POM.xml?](https://stackoverflow.com/questions/10463077/how-to-refer-environment-variable-in-pom-xml) – Deepak Kumar Aug 07 '20 at 16:15
  • It might be possible to leverage profiles for this. – Polygnome Aug 07 '20 at 17:55

1 Answers1

4

No, there is no way to do this.

Even if you did walk down the path of dynamic class loading, the problem remains that you would have two versions of a library that would compete and/or conflict with one another in ways you don't desire.

The main issue here is that you seem to want some features in 2.2 that aren't available in 2.4. I would strongly encourage you instead to look at what it would take to migrate to 2.4 and update your code so that it behaves better with 2.4 instead of having to go through this pain.

Makoto
  • 104,088
  • 27
  • 192
  • 230