Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a module and its child modules from a Maven reactor build?

the question was asked many times but I didnt find a solution, how to disable also the child modules of a module!?!

For example: I want to disable projects BB and CC AND also their child modules

A
|-AA
|  |--AAA
|-BB
|  |-BBB
|-CC
   |-CCC

But when I run this

mvn -pl \!AA,\!BB clean install

I see that also BBB and CCC are executed. This here is only a sample. We have a multi module project with about a hundred projects ... soo I dont want to list all project, only the parents. Is this possible?

Thank you

like image 373
pan40 Avatar asked Oct 18 '22 17:10

pan40


1 Answers

You could use Maven profiles for your purpose.

In the parents projects you could define a profile which overrides the modules section providing all the required modules. Such a profile would be active by default, so no changes on the expected behavior.

However, the modules section of the normal POM (out of profiles) will be empty. Note that we cannot do the opposite (which would make more sense): I just tried it and it didn't work.

So, such a sample POM would look like:

<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>com.sample</groupId>
<artifactId>test3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
</modules>

<profiles>
    <profile>
        <id>aggregator</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>..\test</module>
            <module>..\test2</module>
        </modules>
    </profile>
</profiles>

Executing:

mvn clean install

Maven will also activate the profile and perform the aggregator build as expected. So, no regressions.

However, when executing:

mvn clean install -P!aggregator

Maven will deactivate the profile above and as such perform a reactor build with no modules, executing only the parent as you wished.
Pity the reverse approach (profile with empty modules) didn't work on my test, but the approach should hopefully give you enough hints to reach your goal.


Update
You are actually using this new feature available since Maven 3.2.1. From the comments on the ticket, somebody had the same question:

how about excluding nested modules? I tried the new feature and it seems as though:
1. when a top module is excluded, its nested modules are not.
2. it is not possible to exclude nested modules directly.

Answer was:

Nested modules are not excluded by parent module. The exclusion method mimics the inclusion method and its matcher does not support wildcards etc. So to cascade exclusion would necessitate an extra flag like -amd. The dependency graph is filtered in the following order when using -pl:
1. included projects + possibly upstream and downstream
2. excluded projects
3. skipping for resume projects
So it should be possible to directly exclude nested modules as they should be present in the intial dependency graph before filtering starts.

like image 138
A_Di-Matteo Avatar answered Oct 22 '22 01:10

A_Di-Matteo