Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding "provided" dependencies from Maven assembly

Tags:

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.

like image 574
Chris Vest Avatar asked Sep 22 '09 09:09

Chris Vest


People also ask

How you can exclude dependency in Maven?

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows. The dependency is also excluded from the Project and Maven tool windows.

What is exclude in Maven?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.


Video Answer


1 Answers

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-dependencies and unpack-dependencies goals both have an optional excludeScope property you can set to omit the provided dependencies. 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.

<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-dependency-plugin</artifactId>   <executions>     <execution>       <id>copy-dependencies</id>       <phase>process-resources</phase>       <goals>         <goal>copy-dependencies</goal>       </goals>       <configuration>         <excludeScope>provided</excludeScope>         <outputDirectory>${project.build.directory}/lib</outputDirectory>       </configuration>     </execution>   </executions> </plugin> <plugin>   <artifactId>maven-assembly-plugin</artifactId>   <version>2.2-beta-4</version>   <executions>     <execution>       <id>jar-with-deps</id>       <phase>package</phase>       <goals>         <goal>single</goal>       </goals>     </execution>   </executions>   <configuration>     <descriptors>       <descriptor>src/main/assembly/my-assembly.xml</descriptor>     </descriptors>   </configuration> </plugin> 

The fileSet section of the my-assembly descriptor would look like this:

<assembly>   <fileSets>     <fileSet>       <directory>${project.build.directory}/lib</directory>       <outputDirectory>/</outputDirectory>       <includes>         <include>*.*</include>       </includes>     </fileSet>   </fileSets> ...  </assembly> 
like image 138
Rich Seller Avatar answered Oct 23 '22 10:10

Rich Seller