Search code examples
maven-2maven-assembly-plugin

What is the right approach for assemblying module resources in Maven project?


I have a project structure like

module-a/src/main/java
module-a/src/main/styles
module-distribution/src/main/assembly

module-distribution depends on module-a, and they both share parent pom and are included as modules in that parent pom.

The idea is to have an assembly layout like

my-final-assembly/lib
my-final-assembly/styles

Where lib directory contains all project artifacts including dependencies (i.e. module-a.jar and its dependency jars), and module-a.jar should not contain its styles directory in it. styles directory should reside in my-final-assembly/styles.

I can fill out the lib directory easily including

<dependencySet>
    <useProjectArtifact>false</useProjectArtifact>
    <outputDirectory>lib</outputDirectory>
</dependencySet>

in my assembly file. But what about module-a/src/main/styles? I can

  1. include it in module-a artifact (it would reside in module-a.jar)
  2. not include it in module-a.jar.

Sure thing, both 1) and 2) are not valid for me. In first case I have to somehow extract styles directory out of my module-a.jar. In second case styles directory would not be copied at all, as copies the artifact only, not entire target folder.

I have a last resort of creating an individual module for styles directory contents, and make module-a depend on it, but that looks like an overkill for me. Though it would be easy to tune my assembly file to get what I want in that case. But a separate module for a bunch of fonts and xmls that a sole module needs? That would be sad, though I am ready if that is what you call Maven Way.


Solution

  • Somehow, you have to put the styles in an artifact at some point to be able to pull and include them in the "final-assembly". I can think of 3 approaches:

    The first approach is what you're describing: putting the styles in a separate module and have bot module-a and module-distribution depend on it (actually, I don't really understand why module-a has a dependency on it since you're not using them/including them in the module-a artifact but this is left at your discretion).

    The second approach would be to keep your current structure but to create an intermediate assembly for module-a. This assembly would contain module-a.jar and the styles (or maybe just the styles). Then, in module-distribution, you would pull this assembly, unpack it, and create the "final-assembly".

    A third approach would be similar to the previous one except that the final-assembly would be created directly from module-a (it's not clear if module-distribution has other dependencies than module-a; if it doesn't, then you could do everything from module-a).