How to define common configurations in maven projects

Viewed 78

I hope this is the right place and right discussion to start.

What I want

I am looking for a good/the right way to handle multiple maven projects, that share a common configuration, which is in my case

  1. Set the maven-compiler-plugin version with source/target compilation version
  2. Set the maven-source-plugin to attach sources to artifacts
  3. Define local repository server
  4. Define common dependencies (via dependencyManagement and dependencies).

What I did

My first approach was to create a multi module project. But as the modules are actually independant, when it comes to release (so not the same release cycle), it seemed to be more a burden as a gain. I also failed to make it that I can independently work with a submodule in IntelliJ and have for each submodule it's own Jenkins job. For me it was always all or nothgin

The second approach was/is to have seperate projects. This makes coding in IntelliJ simple and Jenkins can have jobs for each project.

But now changes to parent pom, which affects the children, are tedious to delegate as all other projects must adopt to the change to the new parent.

It is doable and might be the way to work with this, but as I am afraid I misuse the parent concept here, I was wondering, whether there is a better way to use common settings in multiple maven projects ?

Thanks for reading and I hope this was understandable

2 Answers

You can re-use dependencies management section from different pom using <scope>import</scope>, described here, but that would work only for dependencies and you cannot do that for properties, repos and etc.

I'm afraid there is no better way to solve you problem then the parent-pom. Actually this is widely adopted approach. Spring boot, for example, uses that for a long time already.

One thing you want to do to is a good release management for that parent pom. Be sure that you are not dependent on SNAPSHOT version to not brake existing projects.

Just create a parent POM with the common settings and keep the rest separate. Publish that POM to the (local) repository separately as a standalone entity with pom packaging (no submodules at this level).

Have a look at the Spring Boot parent POM as an example.

The only change in your projects would then be to use your new POM as the parent POM.

<project ... >
  <parent>
      <groupId>my-group</groupId>
      <artifactId>my-parent</artifactId>
      <version>1</version>
  </parent>
  ...

Note however that it is generally not a good idea to hardcode dependencies in the parent POM (defining versions via dependencyManagement is OK, but let each project specify explicitly which dependency it actually needs).

Related