Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build order of Maven multimodule project?

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> 
like image 272
Zhao Yi Avatar asked Jun 21 '12 04:06

Zhao Yi


People also ask

How do you create a multi-module project?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

Does order of dependencies matter in POM xml?

The order of dependencies does matter because of how Maven resolves transitive dependencies, starting with version 2.0.

How does Maven build a project?

Maven will start building the project. We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar (package). Packaged jar is available in consumerBanking\target folder as consumerBanking-1.0-SNAPSHOT.


1 Answers

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.

like image 198
Kai Sternad Avatar answered Oct 08 '22 20:10

Kai Sternad