Summary
Why?
Maven is using <module>
declarations to determine the list of modules to include in the current run. Maven is using <parent>
declaration to generate effective POM for each included module, which is then used to execute the build for that module.
In Project 1, each of module1
and module2
specify parent
module in <parent>
section.
In Project 2, neither module1
nor module2
specify parent
module in <parent>
section.
How can I manually control the build order?
By changing dependencies between modules, including <parent>
, <dependencies>
, plugin dependencies, etc. as described in the official documentation.
Please check also Introduction to the POM for the discussion on aggregation and inheritance.
Details
There are 4 possible scenarios of module relationships in Maven in terms of aggregation (<module>
declaration) and inheritance (<parent>
declaration).
Let's assume a module A
with packaging pom
and a module B
(packaging does not matter). The module A
could either be a parent or an aggregator. The module B
could either reference A
as parent or not.
Case 1:
Neither A
nor B
reference each other - the modules are independent.
The Maven can not control build order, each module gets built independently, with manual build order control.
Case 2:
The module A
includes the module B
in <modules>
. The module B
does not declare A
as parent. The module A
is an aggregator for the module B
in this case.
- Maven invocation
mvn -f A/pom.xml
will first build B
, and then A
.
- Maven invocation
mvn -f B/pom.xml
will build B
only.
Case 3:
The module A
includes the module B
in <modules>
. The module B
declares A
as parent. The module A
is both the parent and the aggregator (reactor) for the module B
in this case.
- Maven invocation
mvn -f A/pom.xml
will first build A
, and then B
.
- Maven invocation
mvn -f B/pom.xml
will build B
only, but will use the POM of A
to generate effective POM of B
by resolving it either from the local repository or by following /project/parent/relativePath
.
Case 4:
The module A
does not include the module B
in <modules>
. The module B
declares A
as parent. The module A
is the parent for the module B
in this case.
- Maven invocation
mvn -f A/pom.xml
will build A
only.
- Maven invocation
mvn -f B/pom.xml
will build B
only, but will use the POM of A
to generate effective POM of B
by resolving it either from the local repository or by following /project/parent/relativePath
.