Search code examples
mavenjarmaven-assembly-pluginmaven-jar-plugin

Is it possible to build a "sub jar" of a Maven project?


I have a situation at the moment where I have:

Project A which is built into a fat jar using Maven assembly plugin.

Project B which uses the jar built in Project A. It is added to the project as a resource and launched in a separate process using a process builder.

I wonder if it's possible to achieve similar behaviour using just one Maven project. I.e build the jar containing only the classes and dependencies required for project A, and then build the rest of the project with the prebuilt jar.

Sorry if I'm not being very clear here.


Solution

  • This is against a few of Maven's core concepts:

    • One project, one model (POM). Two projects (A, B), two models (POMs).
    • There's one artifactId in a POM. What is a second artifact (jar) supposed to be named?
    • One project leads to one artifact. There is no additional "prebuilt jar" built within the very same project.
    • Dependencies are for the whole project (and possible sub-module projects). I'm not aware of how to "containing only the classes and dependencies required for project A".
    • Artifacts are stored:

      • in <project>/target temporarily
      • in the local Maven repository (default: ~/.m2/repository)
      • possibly in a remote Maven repository


      ... while resources are taken from <project>/src/main/resources during the build.

    There might be some tricky solutions (which have possibly pitfalls, too) to achieve this if one thinks about it thoroughly. But I'd never ever recommend such.