Change maven dependency's version by using different maven profiles
Asked Answered
J

1

7

I have two projects, project A is depending on project B, so normally, I'd have the following section in my projectA/pom.xml:

<dependency>
   <artifactId>projectB</artifactId>
   <groupId>blabla</groupId>
   <version>version1</version>
</dependency>

What I am trying to achieve is very straight forward, does maven profile allow me to do anything like:

if(profileA) {
    <version>version1</version>
}
else {
    <version>version2</version>
}
Jann answered 11/5, 2012 at 14:36 Comment(0)
C
9

Yes, this can be done (put activeByDefault to whichever profile you need to be default).

<dependency>
    <artifactId>projectB</artifactId>
    <groupId>blabla</groupId>
    <version>${dependency.version}</version>
</dependency>
...
<profiles>
    <profile>
        <id>first</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>            
        <properties>
            <dependency.version>1.2.3</dependency.version>
        </properties>
    </profile>
    <profile>
        <id>second</id>
        <properties>
            <dependency.version>2.3.4</dependency.version>
        </properties>
    </profile>
</profiles>
Christhood answered 11/5, 2012 at 14:41 Comment(2)
Great, thanks, I had a further question posted here, if you can take a look would be fantastic. https://mcmap.net/q/1621552/-using-build-number-plugin-in-maven-profile-to-build-modules/1275937Jann
I have a slightely different situation, I need to change multiple dependencies versions(hive, aws, etc.) depending on a profile. Is this the recommended way? Currently I have specified <dependencies> tag in <profile>.Preconcerted

© 2022 - 2024 — McMap. All rights reserved.