Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including dependencies in a jar with Maven

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file?

I have a project the builds into a single jar file. I want the classes from dependencies to be copied into the jar as well.

Update: I know that I cant just include a jar file in a jar file. I'm searching for a way to unpack the jars that are specified as dependencies, and package the class files into my jar.

like image 461
racer Avatar asked Nov 13 '09 12:11

racer


People also ask

Does Maven package dependencies into jar?

2.2. The difference is that the Maven Assembly Plugin will automatically copy all required dependencies into a jar file. In the descriptorRefs part of the configuration code, we provided the name that will be added to the project name.

Does jar contain all dependencies?

java is a main starting point which has main(String args[]) method inside. pom. xml file in which we will add Maven Plugins which will build executable . jar project with all included dependancies.

How do I add a dependency in Maven?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.


1 Answers

You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor. Here's the relevant chunk from one of our pom.xml's that does this:

  <build>     <plugins>       <!-- any other plugins -->       <plugin>         <artifactId>maven-assembly-plugin</artifactId>         <executions>           <execution>             <phase>package</phase>             <goals>               <goal>single</goal>             </goals>           </execution>         </executions>         <configuration>           <descriptorRefs>             <descriptorRef>jar-with-dependencies</descriptorRef>           </descriptorRefs>         </configuration>       </plugin>     </plugins>   </build> 
like image 114
John Stauffer Avatar answered Oct 13 '22 10:10

John Stauffer