views:

1152

answers:

3

I'm just starting to use Maven, (evaluating it, really) and I need to be able to quickly generate a jar for my app and a directory with all the dependencies (eg: lib) so that I can deploy those two to be run in a stand-alone manner. Generating the jar 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 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 jars 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 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>)

Thanks!

+2  A: 

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
+9  A: 

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
+2  A: 

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