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

mavenmaven-2

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

Best Answer

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.

Related Topic