Maven - Child Module Profiles

Problem

I have a maven project that has a similar structure with the following:
(simplified to explain goals)

--parent |-- child A (inherits from parent) |-- child B (inherits from parent) |-- child B1 (inherits from B) |-- child B2 (inherits from B) |-- child B3 (inherits from B) 

Only children B1 and B2 should be created using a specific profile, which includes some additional assemblies. As a result, the profile was specified in module B.

Logically, the modules belong to module B and additionally inherit some dependencies, etc. (aggreagtion + inheritance).
(imagine something like B = Frontend, B1 = UI, B2 = Themes, B3 = Something else)


Question

  • Is it possible to use the full assembly from the parent pom and activate the corresponding profile only in the correct children? (would be my favorite decision)
  • Or do I need separate assemblies using the advanced options reactor (-pl, etc.)?
  • Or is there a completely different approach for such scenarios?

Edit

Edit, since the question has been identified as duplicate : The problem is that the solution mentioned in the corresponding question does not work.

If I activate an auxiliary profile using a property, it will also be activated for module B (parent) and for ALL children.

I want it to be active for child B1 and B2 .

+5
source share
1 answer

If you want to have a profile that should be applied to submodules (and only some of them), defined centrally in their parent project (pom) and not apply the profile to the parent itself, here is the proposed approach:

Define the profile in the parent pom, pattern one:

 <profiles> <profile> <id>sample-profile</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>print-hello</id> <phase>${phase.prop}</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <echo message="hello there!" /> </target> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> 

Please note that we do:

  • the plugin phase parameter is parameterized, therefore the binding of the plugin to the assembly phase will also be parameterized
  • Now we can decide through the property to which the phase attaches this plugin execution.
  • Now we can decide through the property to attach this plugin execution to the phase (or empty phase)

So, in the same parent pom, define a property:

 <properties> <phase.prop>none</phase.prop> </properties> 

Thus, the assembly performed on the parent pom will attach the execution of the plugin to the phase ( none is a keyword or a symbolic value, but simply the de facto standard used for a nonexistent phase, you can leave it empty or put any value that you like, it will have the same effect) and therefore skip it. It will also be skipped by default in all declared modules. The profile will still be active in each module, but will be harmless, because by default it will not execute any plug-in.

Then in the modules in which you want the actions of the plugin to be active, you could only override the corresponding property and the necessary phase:

 <properties> <phase.prop>package</phase.prop> </properties> 

If you then activate the profile from the main assembly (parent)

 mvn clean install -Psample-profile 

The parent will apply it to all modules, but the effective execution of the effect will occur only where the value of phase.prop has the significant maven value.

If you need different phases for different plugins, you can define more than one property and apply the same template.

If you do not have a plugin, but rather the global plugin configuration for a plugin already executed by Maven (as part of its default packaging and assembly), you could then override its execution by overriding the default execution identifier .

Note that we could do the same with the skip configuration element, but:

  • not all plugins provide a skip configuration entry
  • using the phase element is not related to any configuration and can be reused for execution connected to the same phase
+8
source

Source: https://habr.com/ru/post/1242750/


All Articles