'Cannot find profile dependent dependency in a child pom

Could anyone advice on the following:

I have parent maven xml like this:

<project>
...
    <modules>
        <module>Module_1</module>
    </modules>

    <profiles>
        <profile>
           <id>p1</id>
        
           <modules>
               <module>Module_2</module>
           </modules>
        <profile>
    </profiles>
...
</project>

And I have child pom dependent on parent one and on the Module_2 because it has beans that I want to use in my child module:

<project>
...
    
    <profiles>
        <profile>
           <id>p1</id>
        
           <dependencies>
               <dependency>
                    <groupId>some.group</groupId>
                    <artifactId>Module_2</module>
                    <version>${project.version}</version>
               </dependency>
           </dependencies>
        <profile>
    </profiles>
...
</project>

So, I want Module_2 built and included as dependency into child module only with -PModule_2 key. But I cannot build the project because it cannot find Module_2 dependency in the child module.

Is there any way to solve this?

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source