Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pom files for a maven project

i have a project which has multiple maven project and 1 parent pom which has all other projects as maven modules.

pom.xml

<modules>
    <module>abc-codegen</module>
    <module>stellar-codegen</module>
    <module>abc-engine</module>
    <module>stellar-engine</module>
</modules>

out of those maven projects few projects(abc-codegen, stellar-codegen) are used for code generation. the codegen artifacts are used by other maven modules such as abc-engine.

the codegen modules may not be built everytime other application modules(abc-engine etc) are built and released.

pom-codegen.xml

<modules>
    <module>abc-codegen</module>
    <module>stellar-codegen</module>
</modules>

However, the codegen projects should remain inside the parent folder (svn/my-service/trunk) for the sake of consistency with other such applications.

One of the idea i have is to create another pom file called as pom-codegen.xml under the parent folder and generate code when required by explicitely calling

mvn -f pom-codegen.xml clean deploy

The only i have to do is tell my CI to execute the above command and trigger deploy the artifact when required. whilst Other build (mvn clean install) will check the sanity of the code. can we do this any other approach?

like image 925
Selvakumar Esra Avatar asked Mar 17 '14 10:03

Selvakumar Esra


People also ask

Can a Maven project have multiple POM files?

Maven's Multi-Module Project The submodules are regular Maven projects, and they can be built separately or through the aggregator POM. By building the project through the aggregator POM, each project that has a packaging type different from pom will result in a built archive file.

Can a Maven POM have 2 parents?

You can achieve multiple inheritance with profiles: This is not ideal because child projects cannot control the activation of profiles defined in the parent POM. This is not remotely related to inheritance. But you can use them to setup some parameters to turn on/off with a switch (enabling/disabling the profiles).

Is POM xml is the most important file in a Maven project?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.

Does Maven support multi project build?

The mechanism in Maven that handles multi-module projects is referred to as the reactor. This part of the Maven core does the following: Collects all the available modules to build. Sorts the projects into the correct build order.


1 Answers

Yes you can use Maven Profiles to manage this.

E.g. in the parent POM create a profiles section

<profiles>
    <profile>
        <id>aggregate-all</id>
        <modules>
             <module>abc-codegen</module>
             <module>stellar-codegen</module>
             <module>abc-engine</module>
             <module>stellar-engine</module>
        </modules>
    </profile>
    <profile>
        <id>aggregate-codegen</id>
        <modules>
             <module>abc-codegen</module>
             <module>stellar-codegen</module>
        </modules>
    </profile>
</profiles>

Now to build everything you run:

mvn clean install -P aggregate-all

To build just the code generation projects you run

mvn clean install -P aggregate-codegen

Obviously you can tweak this approach to suit your needs however works best. So you could create a profile that builds just the engine projects.

like image 89
cowls Avatar answered Sep 21 '22 13:09

cowls