How to split POM fragments between different POMs

I am currently struggling with Maven: I have a complex project consisting of several nested modules and for some of these modules I have similar configurations in POM.

I want to make it clean. In fact, I would like to define the general runnable-jar configuration and activate it in some modules.

Here is a POM snippet that I would like to split between several projects:

<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- Use a custom descriptor, with suffix "bin" --> <descriptors> <descriptor>src/main/assembly/runnable-jar-assembly.xml</descriptor> </descriptors> <!-- Add main class to manifest --> <archive> <manifest> <mainClass>${mainClass}</mainClass> </manifest> </archive> </configuration> <!-- Add build of this package to lifecycle --> <executions> <execution> <id>make-runnable-jar</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 

In some of the POMS, I would like to do something like:

 <!-- Set the main class --> <properties> <mainClass>my.main.Class</mainClass> </properties> <!-- Activate runnable jar build --> <import>src/main/pom/runnable-jar-pom.xml</import> 

I was looking for a mean to import some XML fragments into the POM or to define the entire macro of the XML nodes.

For what I found, the closest solution would be to identify the profile in the parent POM and activate it in some submodules by checking the file. See this related question . But I ran into the problem that the {basedir} property is not set correctly inherited / set.

It is very surprising to me that I need to do something to crack something basic (= usually). How do you usually deal with this in Maven?

+8
maven profile
source share
4 answers

I just discovered that could solve my problem:

A module does not require to be a submodule of its parent module.

Parental and submodular relationships are separate concepts.

You can specify the POM parent module, which is not the main parent folder in your folder structure, using the relativePath attribute ( as explained in the document )

In my case, I use the following layout:

  • main project
    • utils (parent: main-project)
    • cli-programs (parent: main project)
      • generic-cli (parent: cli-programs; Dummy and an empty POM module)
      • cli-1 (parent: generic-cli)
      • cli-2 (parent: generic-cli)

Then, in generic-cli/pom.xml , I can declare a configuration that is common to all my cli programs (for example, custom test suites, launch packaging, etc.).

+6
source share

One way to do this is to declare the <plugin> code inside the <pluginManagement> parent pom of your multi-module project. Individual modules can then have a <plugin> section that can use this without updating the content.

Parent pom:

 <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> ... all the details... </plugin> ... </plugins> </pluginManagement> 

Child:

  <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> </plugin> </plugin> 
+5
source share

not a general answer, but a solution to the basedir problem is to use a general module layout, for example. root / modules / moduleA root / modules / moduleB.

You can no longer create formm modules in your own directory, only through the thr child project. But you can work with profiles.

0
source share

Maven-tiles allows this. This is also on the roadmap for maven 3.x, tracked here .

0
source share

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


All Articles