Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Maven collect all the dependent JARs for a project to help with application deployment?

I'm just starting to use Maven, (evaluating it, really) and I need to be able to quickly generate a JAR file for my application and a directory with all the dependencies (for example, lib) so that I can deploy those two to be run in a stand-alone manner. Generating the JAR file with the proper manifest is easy, but I do not know how to get Maven to copy the dependencies for the current project into a lib directory that I can deploy.

Since this is for a stand-alone Java applications, I am not interested in deploying to a Maven repository, that is also fairly trivial, or at least easily googleable.

I've found out how to do everything except copy the dependent JAR files into some specified directory. This is the workflow I'm looking for:

$ mvn clean $ mvn package $ cp -r target/{lib,myApp.jar} installLocation 

Then, running myApp.jar from installLocation as a JAR file should "just work" regardless of my $CLASSPATH.

To try and pre-empt some answers:

  • I do have a Main-class: set, and it works fine.
  • I've also set the classpath in the MANIFEST.MF, and that works fine too.
  • I've found out how to use <classpathPrefix> and <classpathMavenRepositoryLayout> to make this work -- but only on my machine. (via: <classpathPrefix>${settings.localRepository}</classpathPrefix>)
like image 391
rcreswick Avatar asked Feb 20 '09 01:02

rcreswick


People also ask

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.

Which Maven command creates an executable jar file with all dependencies of the project?

Use the maven-shade-plugin to package all dependencies into one über-JAR file. It can also be used to build an executable JAR file by specifying the main class.

How does Maven work with dependencies?

Dependencies are external JAR files (Java libraries) that your project uses. If the dependencies are not found in the local Maven repository, Maven downloads them from a central Maven repository and puts them in your local repository.

How do you get dependencies of a jar?

Main class in the tools. jar file. Use the -verbose:class option to find class-level dependencies or use the -v or -verbose option to include dependencies from the same JAR file. Use the -R or -recursive option to analyze the transitive dependencies of the com.


2 Answers

What you want to investigate is Maven's dependency plugin. Add something similar to the following to pom.xml:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-dependency-plugin</artifactId>     <configuration>         <outputDirectory>             ${project.build.directory}         </outputDirectory>     </configuration> </plugin> 

Then run mvn clean dependency:copy-dependencies to copy perform the copy. Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.

like image 94
laz Avatar answered Oct 18 '22 17:10

laz


I did not care for the Shade plugin since it rolls all the packages from all the jars together.

To include all of the external libs you can use the Dependency Plugin as mentioned above.

This example will create a "lib" directory under "target/classes" before the "package" phase.

 <plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-dependency-plugin</artifactId>   <version>2.6</version>   <executions>     <execution>       <id>copy-dependencies</id>       <phase>prepare-package</phase>       <goals>         <goal>copy-dependencies</goal>       </goals>       <configuration>         <outputDirectory>target/classes/lib</outputDirectory>         <overWriteIfNewer>true</overWriteIfNewer>         <excludeGroupIds>           junit,org.hamcrest,org.mockito,org.powermock,${project.groupId}         </excludeGroupIds>       </configuration>     </execution>     <execution>       <phase>generate-sources</phase>       <goals>         <goal>sources</goal>       </goals>     </execution>   </executions>   <configuration>     <verbose>true</verbose>     <detail>true</detail>     <outputDirectory>${project.build.directory}</outputDirectory>   </configuration> </plugin> 
like image 31
Gordon Dickens Avatar answered Oct 18 '22 15:10

Gordon Dickens