Can Maven compile all the dependent JARs for the project to help deploy the application?

I'm just starting to use Maven (really appreciating it), and I need to be able to quickly generate the <href = "http://en.wikipedia.org/wiki/JAR_file" rel = "noreferrer"> JAR file for my application and a directory with all the dependencies (e.g. lib) so that I can deploy these two, which will run autonomously. Generating a JAR file with the appropriate manifest is simple, but I donโ€™t know how to get Maven to copy the dependencies for the current project into the lib directory that I can deploy.

Since this is for standalone Java applications, I'm not interested in deploying to the Maven repository, which is also pretty trivial or at least easy googleable.

I figured out how to do everything except copy the dependent JAR files to 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 "work" regardless of my $CLASSPATH .

To try to preempt some answers:

  • I have Main-class: set and it works great.
  • I also set the classpath to MANIFEST.MF, and that works fine too.
  • I figured out how to use <classpathPrefix> and <classpathMavenRepositoryLayout> to do this work, but only on my machine. (via: <classpathPrefix>${settings.localRepository}</classpathPrefix> )
+46
java maven-2 deployment desktop-application
Feb 20 '09 at 1:50
source share
6 answers

What you want to explore is the Maven Dependency Plugin . Add something like 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 the execution of the copy. Combine this with the collector and you can pack everything into a standalone archive for distribution.

+70
Feb 20 '09 at 4:03
source share

I did not like the Shade plugin, as it collapses all packages from all cans together.

To include all external libraries, you can use the dependency plugin as described above.

In this example, the "lib" directory will be created in the "target / classes" section 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>
+7
Feb 01 '13 at 22:32
source share

Another is the appassembler plugin
What I like about this is that it packs the application into a form ready for use (with a .bat file, etc.)

+6
Feb 20 '09 at 21:44
source share

Sure. You need to use a 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.

+3
Feb 20 '09 at 2:58
source share

Take a look at the maven dependency plugin, in particular the copy-dependent target. The usage section describes how to do exactly what you want.

To do this from the command line, simply do:

 $ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR 
+2
Dec 16 '15 at 9:03
source share

Using maven.repo.local , you can collect all the banks, but they are collected in a directory with the maven hierarchy (.m2).

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

Then you can build them (on Linux):

 mkdir flat-repo find ./pick/some/folder -type f -name "*.jar" | xargs -I'{}' cp '{}' flat-repo/ 
-one
Aug 17 '16 at 15:05
source share



All Articles