The P2 profile is not activated because the path under its exists tag does not allow the existing path, even if the directory ${project.basedir}/src/main/whatever exists. If you rewrite the property ${project.basedir} as ${basedir} , it should activate the P2 profile.
This should mean that ${project.basedir} does not resolve the project’s base directory, as it should . However, help:effective-pom shows that it does. I reported this ( MNG-5516 ).
Also I think that P1 will not be active if P2.
It is right. Quoting documentation for activeByDefault :
This profile (P1 in this example) will be automatically active for all assemblies if another profile in the same POM is not activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in POM is activated on the command line or through the activation configuration.
Inheritance inheritance confused me because "profile inheritance" works in project aggregation , but not in project inheritance .
To make everything clear, I simulated this situation. Empty pom means that it is empty, with the exception of the standard model, group, artifact, and version tags.
Simple script
Directory structure:
simple \-pom.xml
Pom content:
<profiles> <profile> <id>P1</id> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>P2</id> <activation> <file> <exists>${basedir}/dir/</exists> </file> </activation> </profile> </profiles>
If there is no dir directory mvn help:all-profiles exits:
Profile Id: P1 (Active: true , Source: pom) Profile Id: P2 (Active: false , Source: pom)
If dir exists, the mvn help:all-profiles directory mvn help:all-profiles returns:
Profile Id: P2 (Active: true , Source: pom) Profile Id: P1 (Active: false , Source: pom)
Project Inheritance
Directory structure:
inheritance |--child | \-pom.xml // child pom \-pom.xml // parent pom
The child pom is empty, and the parent pom has profiles as in a simple scenario. Regardless of the existence of the inheritance/child/dir directory working with mvn help:all-profiles from the child directory:
Profile Id: P1 (Active: false , Source: pom) Profile Id: P2 (Active: false , Source: pom)
When you start the mvn help:effective-pom directory from child it shows that profiles are not really inherited. It behaves as documented :
The elements of the combined POM are as follows:
- dependencies
- developers and contributors
- plugin lists (including reports)
- execution of plugins with corresponding identifiers
- Resources
There are no profiles here.
Project aggregation
Directory structure:
aggregation |--module | \-pom.xml // module pom \-pom.xml // aggregator pom
The pom module is empty, and the pom aggregator has profiles as in a simple scenario. If the aggregation/module/dir directory does not work mvn help:all-profiles from module :
Profile Id: P1 (Active: true , Source: pom) Profile Id: P2 (Active: false , Source: pom)
If the aggregation/module/dir directory exists with the mvn help:all-profiles directory from the module directory:
Profile Id: P2 (Active: true , Source: pom) Profile Id: P1 (Active: false , Source: pom)
When you start the mvn help:effective-pom directory from module it shows that the profiles are inherited. This is not explicitly documented :
Project Inheritance
If you have several Maven projects, and they all have similar configurations, you can reorganize your projects by pulling out these similar configurations and creating a parent project. Thus, all you have to do is let your Maven projects inherit this parent project, and these configurations will be applied to all of them.
Notes:
- This does not apply to profiles as shown.
- Runnnig a maven build from the
inheritance directory will only run the parent assembly.
Project aggregation
And if you have a group of projects that are created or processed together, you can create a parent project and create this parent project as your modules. By doing so, you only need to create the parent element, and the rest will follow.
Notes:
- Runnnig a maven build from the
aggregation directory will run the assembly of each module and aggregator (the actual order is determined by maven based on different criteria).
Conclusion
Profiles can be defined globally for each user or for each project. Since aggregated projects are built together (in the same assembly), you need to run a specific profile resolution to calculate active ones. So this is the confusing part:
- When projects are inherited, profiles not inherited from the parent pom to child pom.
- When projects are aggregated profiles , inherited from the pom aggregator to the pom module.
This has been verified using Maven 3.1.0. and 3.0.5.