Java – Build order of Maven multimodule project

buildjavamavenmulti-module

The situation is, I have two Maven multimodule projects with the same structure:

Parent
    - Module 1
    - Module 2

When I build project 1, I see that parent is built first (order is parent->module1->module2). However for project 2, parent is built at last (order is module1->module2->parent). Why are the two projects have different build orders? Furthermore, how can I manually control the build order?

Update 1:
Both parent projects are simple POM projects without source code, so I can't explain the build order as per the dependency graph.

Update 2:
The parent POMs are the same except the GAV and child module names:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>parent-group-id</groupId>
    <artifactId>parent-artifact-id</artifactId>
    <version>parent-version</version>
    <packaging>pom</packaging>
    <name>parent-name</name>
    <modules>
        <module>module-1</module>
        <module>module-2</module>
    </modules>
</project>

Best Answer

The build order is determined by the Maven reactor which is a mechanism that automatically ensures correct build order for multimodule builds by sorting the projects.

See the official documentation for how it works.

It says:

The following relationships are honoured when sorting projects:

  • a project dependency on another module in the build
  • a plugin declaration where the plugin is another modules in the build
  • a plugin dependency on another module in the build
  • a build extension declaration on another module in the build
  • the order declared in the element (if no other rule applies)

You can't manually control the build order. If you want to change the order you have to make changes to your project that influence the reactor sort order.