Make Maven to copy dependencies into target/lib

303,984

Solution 1

This works for me:

<project>
  ...
  <profiles>
    <profile>
      <id>qa</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

Solution 2

mvn install dependency:copy-dependencies 

Works for me with dependencies directory created in target folder. Like it!

Solution 3

The best approach depends on what you want to do:

  • If you want to bundle your dependencies into a WAR or EAR file, then simply set the packaging type of your project to EAR or WAR. Maven will bundle the dependencies into the right location.
  • If you want to create a JAR file that includes your code along with all your dependencies, then use the assembly plugin with the jar-with-dependencies descriptor. Maven will generate a complete JAR file with all your classes plus the classes from any dependencies.
  • If you want to simply pull your dependencies into the target directory interactively, then use the dependency plugin to copy your files in.
  • If you want to pull in the dependencies for some other type of processing, then you will probably need to generate your own plugin. There are APIs to get the list of dependencies, and their location on disk. You will have to take it from there...

Solution 4

Take a look at the Maven dependency plugin, specifically, the dependency:copy-dependencies goal. Take a look at the example under the heading The dependency:copy-dependencies mojo. Set the outputDirectory configuration property to ${basedir}/target/lib (I believe, you'll have to test).

Hope this helps.

Solution 5

If you want to do this on an occasional basis (and thus don't want to change your POM), try this command-line:

mvn dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib

If you omit the last argument, the dependences are placed in target/dependencies.

Share:
303,984

Related videos on Youtube

Michael
Author by

Michael

Updated on January 18, 2022

Comments

  • Michael
    Michael over 2 years

    How do I get my project's runtime dependencies copied into the target/lib folder?

    As it is right now, after mvn clean install the target folder contains only my project's jar, but none of the runtime dependencies.

    • Alexandre Victoor
      Alexandre Victoor over 15 years
      Why do you need this ? What is the type of your maven project ? jar ?
    • Michael
      Michael over 15 years
      The type of my maven project is JAR. I need this because there are a lot of dependencies and I'm trying deploy the jar as an executable.
    • demaniak
      demaniak over 8 years
      Caution with assemblies - if you have overlapping packages/classes between the deps, you will probably have a bad time.
  • Cuga
    Cuga almost 14 years
    Alternatively, you could use ${project.build.directory}/lib rather than ${basedir}/target/lib
  • P.M
    P.M over 12 years
    maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e. :-(
  • Dan Halbert
    Dan Halbert over 11 years
    If you want this to happen all the time, remove the <profiles>...<profile> wrappers and make the <build> tag be just under <project>
  • Stevan Trajkoski
    Stevan Trajkoski over 11 years
    @Georgy this does not coy the jars in lib/ , but includes the classes in the compiled project
  • Alfonso Nishikawa
    Alfonso Nishikawa over 10 years
    This is fine, but it is copying test dependencies too. I add to myself the excludeScope option (maven.apache.org/plugins/maven-dependency-plugin/…).
  • Thomas
    Thomas over 10 years
    main class not found in (3)
  • Julien BRENELIERE
    Julien BRENELIERE about 10 years
    Work well but it is not required to place the build tag inside the profile tags.
  • Duncan Jones
    Duncan Jones over 9 years
    Your code example doesn't solve the problem, it just bundles everything into a single JAR. Yes, the assembly plugin can be used to achieve this goal, but not like this.
  • Duncan Jones
    Duncan Jones over 9 years
    Although, on further reading, maybe you are responding to this comment.
  • RubyTuesdayDONO
    RubyTuesdayDONO over 9 years
    it's been so long that i don't really remember … plus i've gotten rather rusty since focusing on Linux administration at my last firm — but thank you for the feedback!
  • Brad Parks
    Brad Parks almost 9 years
    thanks! this is the easiest way to just copy the libs that would be required by a project into a folder somewhere so you can copy them somewhere else if need be, e.g. a non maven based project. Note that of course you can just pass in a hardcoded folder to use if you like, e.g. mvn dependency:copy-dependencies -DoutputDirectory=./lib
  • Gobliins
    Gobliins over 8 years
    Can you do it out of pom.xml?
  • Jesse Chisholm
    Jesse Chisholm over 7 years
    Note: <excludeScope>test</excludeScope> goes inside the configuration node.
  • Searene
    Searene over 7 years
    @Thomas I think it's maven clean install, then you will find lib in target
  • Alan Donizete
    Alan Donizete about 7 years
    what would I have to do to copy only 1 dependency?
  • Martin Pabst
    Martin Pabst about 7 years
    <classpathPrefix>lib/</classpathPrefix> helped me a lot. Thank you!
  • Divyang Shah
    Divyang Shah over 6 years
    @Gobliins use ${project.build.directory}/lib instead of ${targetdirectory}
  • Admin
    Admin almost 6 years
    Wanted to upvote, but found out I had done so 2 years ago :)
  • Searene
    Searene over 5 years
    Maybe it would be more appropriate if the phase were package instead of install
  • Vyacheslav Cotruta
    Vyacheslav Cotruta over 4 years
    Would replace the install phase with process-resources so that the dependencies are copied before the build goal runs
  • jla
    jla almost 4 years
    Thank you for this simple option. I used the package target instead but otherwise did what I was looking for.
  • BraveBoy
    BraveBoy about 3 years
    @JesseChisholm thanks your solution worked
  • niken
    niken about 2 years
    dont know how this is not hte accepted answer? anyway , if you want to send dependencies to specific dir, for example called 'lib' , can use mvn dependency:copy-dependencies -DoutputDirectory=lib/