55

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>)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rcreswick
  • 16,483
  • 15
  • 59
  • 70

6 Answers6

86

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.

laz
  • 28,320
  • 5
  • 53
  • 50
  • Added this to my pom.xml inside Eclipse. Nothing happens – Baked Inhalf Jan 17 '18 at 14:03
  • I had to download maven outside eclipse, added the /bin folder to my path then ran `mvn dependency:copy-dependencies`. YES! Now it works, Maven does what it is supposed to do! Now it's just to copy all *.jar to my project and add to my build path – Baked Inhalf Jan 17 '18 at 14:25
  • 3
    Maybe it's the 9 years since the original reply, but for me (Windows, Maven 3.5) running 'mvn dependency:copy-dependencies' is working event without the new element on pom.xml – Uri London May 25 '18 at 10:09
9

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>
Gordon Dickens
  • 1,447
  • 12
  • 5
9

Take a look at maven's dependency plugin, specifically the copy-dependencies goal. The usage section describes how to do exactly what you want.

To do it from the command line just do:

$ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR
Amar
  • 1,556
  • 3
  • 18
  • 28
7

Yet another one is appassembler plugin
What I like about it is that it packages the app in a form ready to use (with a .bat file ans such)

Sergey Aldoukhov
  • 22,316
  • 18
  • 72
  • 99
2

It sure can. You need to use the shade plugin which can be done by adding

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.3-SNAPSHOT</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
      </plugin>

to your project.

stimms
  • 42,945
  • 30
  • 96
  • 149
1

Using maven.repo.local one can collect all jars, but, they are collected into a directory with maven hierarchy (.m2).

mvn install -Dmaven.repo.local=./pick/some/folder

You can then collect them (on Linux):

mkdir flat-repo
find ./pick/some/folder -type f -name "*.jar" | xargs -I'{}' cp '{}' flat-repo/
ozma
  • 1,633
  • 1
  • 20
  • 28