malejpavouk
malejpavouk

Reputation: 4445

Maven build section deduplication

Our application consists of many microservices built by Maven. Their services have following structure:

core and client have a different stack, hence their build sections do not match. But the build sections of all cores and clients across all services are the same.

We want to deduplicate these build sections to one external file (so when we change something, we do not need to modify it on many places).


We have considered some inheritance, but thats not easily possible as core and client do not share the same build procedure, we are also using Maven modules to build individual service as a whole (parent + all submodules).

Other possibility that we have tried were XML entities + import, but entities are intentionally forbidden in Maven (https://issues.apache.org/jira/browse/MNG-3556).

Is there any other possibility?

Thanks in advance!

Upvotes: 1

Views: 128

Answers (1)

Gerold Broser
Gerold Broser

Reputation: 14782

Maven supports build profiles:

Profiles are specified using a subset of the elements available in the POM itself (plus one extra section), and are triggered in any of a variety of ways. They modify the POM at build time, and are meant to be used in complementary sets to give equivalent-but-different parameters for a set of target environments.

In your case there could be a services project that contains the profiles declarations for core and client projects in its POM:

+- services
   +- service_1
      +- core
      +- client
   +- service_2_
      +- core
      +- client
   +- service_3
      +- ...
   +- ...

UPDATE

Without any profile:

+- services  ... just aggregators here and below, build here and below only
   +- service_1_builder
      +- core
      +- client
   +- service_2_builder
      +- core
      +- client
   +- service_3_builder
      +- ...
   +- ...
+- main  ... parent containing declarations common to all descendant projects 
   +- cores  ... parent containing declarations for core builds 
      +- service_1
      +- service_2
      +- service_3
      +- ...
   +- clients  ... parent containing declarations for client builds 
      +- service_1
      +- service_2
      +- service_3
      +- ...

POM services contains:

<modules>
  <module>service_1_builder</module>
  <module>service_2_builder</module>
  <module>service_3_builder</module>
  <module>...</module>
</modules>

POMs of services/service_n_builder contain:

<modules>
  <module>core</module>
  <module>client</module>
</modules>

POMs of services/service_n_builder/core contain:

<modules>
  <module>../../../main/cores/service_n</module>
</modules>

POMs of services/service_n_builder/client contain:

<modules>
  <module>../../../main/clients/service_n</module>
</modules>

Upvotes: 1

Related Questions