Java – How to have parent and child modules in Maven that both package as jar files

javamavenmodules

I have an unresolvable dependency issue in a Maven project; different pieces of code depend on different versions of dependency A (i.e. most code needs A:0.15; some needs A:0.18). Fortunately, the code is completely separable; that is, the parts that depend on the two different versions can be split into two different projects with two different pom files.

But I'd like to take this opportunity to learn about Maven modules. I could split my project into two modules and compile them separately. But, in six months or so, I hope that this dependency issue will be resolved, and the project will go back to being unitary with no modules. So, what I would like to do is to have a parent project that is packaged as a jar, and then a child module that also compiles into a separate jar. The child project will simply swap out the dependencies.

I really don't know how to make this work, or if it is even good practice to do so. All the examples of module-ing with Maven have the parent class packaged as pom instead of jar.

How can I set up my parent and child pom file so that both of them can be packaged into separate jars, and the child pom is identical to the parent except for a single dependency version? Should I be doing this?

Best Answer

You should not be doing this. Parent POMs have pom as their packaging, and even if you make it work technically, you break expectations and many Maven plugins will not forgive you.

I would refactor the code into the two modules (as you said), and if the issue is resolved someday, you can restructure the two modules into one.