Search code examples
javascalamavensbtmaven-assembly-plugin

Maven Assembly Plugin Merge Strategy


I am trying to convert a Scala/Java project build from sbt to Maven. I have gotten the project running with no issues except when trying to package the fat jar.

I am running into some problems when trying to write a merge strategy. Here is the code I am trying to convert, from build.sbt:

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", "MANIFEST.MF")  => MergeStrategy.discard
  case PathList("META-INF", xs @ _*)        => MergeStrategy.first
  case x                                    => MergeStrategy.first
}

I am trying to use Maven's assembly plugin with a custom descriptor. I have been playing around with the container descriptor handlers provided, but none of them are merging like sbt does. Here is what I have so far:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>my-project</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>runtime</scope>
    </dependencySet>
</dependencySets>

<containerDescriptorHandlers>
    <containerDescriptorHandler>
        <handlerName>metaInf-services</handlerName>
    </containerDescriptorHandler>
    <containerDescriptorHandler>
        <handlerName>metaInf-spring</handlerName>
    </containerDescriptorHandler>
    <containerDescriptorHandler>
        <handlerName>plexus</handlerName>
    </containerDescriptorHandler>
</containerDescriptorHandlers>

Is there any way to mock the MergeStrategy from the sbt build?


Solution

  • During my "adventure" on migrating assembly from Maven to SBT I think I have touched a similar ground and I can provide some insight.

    You can either, or both:

    The summary

    Since the only sbt assembly MergeStrategy you should be worried about is MergeStrategy.first your maven build should be fine because it is the default setting in Maven. The jar will have different content but it will function the same.

    Unless I am missing something, then please provide more detailed info.