Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In maven child pom dependencies are not taking version from parent pom dependencies if they are mentioned in a profile

Tags:

maven-2

maven

I have a multi-module project, and I am using profiles in the parent pom , which have specific dependencies mentioned in them. The issue here is that, if in a child pom , I am overriding the dependency element, and mentioning one of the dependencies in the parent pom (which is declared in a profile in parent pom) , the version of that specific dependency needs to be mentioned again.

E.g Parent pom

<dependencies>
   <dependency>
      <groupId>com.mycode.apps</groupId>
      <artifactId>jobs</artifactId>
      <version>4</version>   
   </dependency>
</dependencies>
<profiles>
<profile>
    <id>common-dependencies</id>
<activation>
    <activeByDefault>true</activeByDefault>
</activation>
    <dependencies>
       <dependency>
          <groupId>com.mycode.apps</groupId>
          <artifactId>dao</artifactId>
          <version>4</version>   
       </dependency>
    </dependencies>
</profile>
</profiles>

Now in child pom.xml

<dependencies>
       <!--this one doesnt need a version specified -->
       <dependency>
          <groupId>com.mycode.apps</groupId>
          <artifactId>jobs</artifactId>
       </dependency>
       <!--this one makes maven throw an error(if version is not specified) while compilation -->
       <dependency>
          <groupId>com.mycode.apps</groupId>
          <artifactId>dao</artifactId>
       </dependency>
</dependencies>

Any idea what might be wrong and how can I fix this??

NOTE: The profile is marked as activeByDefault

like image 487
Neeraj Avatar asked Oct 24 '22 16:10

Neeraj


1 Answers

For this kind of requirments you the dependencyManagement which is exactly for such cases. Or take look into the Sonatype Book. This case shouldn't be handled by profiles.

like image 137
khmarbaise Avatar answered Nov 14 '22 22:11

khmarbaise