I am trying to use the Maven assembly plugin to build a jar-with-dependencies, except those that have provided scope.
I have copied the jar-with-dependencies into an assembly.xml file and configured its use in my pom. Here it is for reference:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>injectable-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
</fileSet>
</fileSets>
</assembly>
I have found out, that if I set the scope to provided, then I can build a jar that contains exactly what I don’t want, but I cannot figure out how to get inverse behavior of that.
This is a bit clunky, but you can use the maven-dependency-plugin to copy/unpack all the dependencies into your project, then use the assembly plugin to do the packaging.
The
copy-dependenciesandunpack-dependenciesgoals both have an optional excludeScope property you can set to omit theprovideddependencies. The configuration below copies all dependencies into target/lib, your assembly plugin descriptor can be modified to use a fileSet to include those jars.Update: Just tested this to confirm it works. Added the configuration for binding the assembly plugin to the package phase, and the relevant modifications to the assembly descriptor.
The fileSet section of the
my-assemblydescriptor would look like this: