Maven: package dependencies along with a JAR project?

I would like Maven to package the project along with its dependencies at runtime. I expect it to create a JAR file with the following manifest:

..... Main-Class : com.acme.MainClass Class-Path : lib/dependency1.jar lib/dependency2.jar ..... 

and create the following directory structure:

 target |-- .... |-- my-project.jar |-- lib |-- dependency1.jar |-- dependency2.jar 

Meaning, I want the main JAR to exclude any dependencies, and I want all transitive dependencies to be copied to the lib subdirectory. Any ideas?

+45
maven-2 manifest dependencies maven-assembly-plugin
Aug 24 '10 at 16:02
source share
3 answers

I like Maven to package a project with runtime dependencies.

This part is unclear (this is not exactly what you described right away). My answer covers what you described.

I expect it to create a JAR file with the following manifest (...)

Set up the Maven Jar Plugin to do this (or more precisely, the Maven Archiver ):

 <project> ... <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.acme.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ... <dependencies> <dependency> <groupId>dependency1</groupId> <artifactId>dependency1</artifactId> <version>XY</version> </dependency> <dependency> <groupId>dependency2</groupId> <artifactId>dependency2</artifactId> <version>WZ</version> </dependency> </dependencies> ... </project> 

And this will create MANIFEST.MF with the following data:

 ... Main-Class: fully.qualified.MainClass Class-Path: lib/dependency1-XYjar lib/dependency2-WZjar ... 

and create the following directory structure (...)

This can be done using the Maven Dependency Plugin and the dependency:copy-dependencies target. From the documentation:

  • dependency:copy-dependencies displays a list of direct project dependencies and possibly transitive dependencies and copies them to the specified location, deleting the version if necessary. This target can also be launched from the command line.

You can bind it to the package phase:

 <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> [...] </project> 
+63
Aug 25 2018-10-10T00:
source share

Add the following plugins to pom.xml. Check the value in the tags mainClass, classpathPrefix, addClasspath.

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass>org.apache.camel.spring.Main</mainClass> <classpathPrefix>lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptors> <descriptor>src/assembly/some-assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

Create some-assembly.xml in the src / assembly section as shown below.

 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>distribution</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <scope>runtime</scope> <outputDirectory>/lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>false</unpack> </dependencySet> </dependencySets> 

Note that the useProjectArtifact flag is false, the unpack flag is false. If the root folder inside the zip file is not required, then you can include includeBaseDirectory in false.

This will create the name-version-distribution.zip file. Inside the zip file there will be a folder with the version name. Inside this folder there will be an executable jar and lib folder containing all banks with dependencies. Check the manifest.MF file of the executable jar. It contains information about the main class and class.

+4
Mar 12 '13 at 11:36
source share
0
Aug 24 '10 at 16:06
source share



All Articles