Make Maven to copy dependencies into target/lib
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
.
Related videos on Youtube
Michael
Updated on January 18, 2022Comments
-
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
thetarget
folder contains only my project's jar, but none of the runtime dependencies.-
Alexandre Victoor over 15 yearsWhy do you need this ? What is the type of your maven project ? jar ?
-
Michael over 15 yearsThe 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 over 8 yearsCaution with assemblies - if you have overlapping packages/classes between the deps, you will probably have a bad time.
-
-
Cuga almost 14 yearsAlternatively, you could use ${project.build.directory}/lib rather than ${basedir}/target/lib
-
P.M over 12 yearsmaven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e. :-(
-
Dan Halbert over 11 yearsIf you want this to happen all the time, remove the <profiles>...<profile> wrappers and make the <build> tag be just under <project>
-
Stevan Trajkoski over 11 years@Georgy this does not coy the jars in lib/ , but includes the classes in the compiled project
-
Alfonso Nishikawa over 10 yearsThis is fine, but it is copying test dependencies too. I add to myself the
excludeScope
option (maven.apache.org/plugins/maven-dependency-plugin/…). -
Thomas over 10 yearsmain class not found in (3)
-
Julien BRENELIERE about 10 yearsWork well but it is not required to place the build tag inside the profile tags.
-
Duncan Jones over 9 yearsYour 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 over 9 yearsAlthough, on further reading, maybe you are responding to this comment.
-
RubyTuesdayDONO over 9 yearsit'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 almost 9 yearsthanks! 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 over 8 yearsCan you do it out of pom.xml?
-
Jesse Chisholm over 7 yearsNote:
<excludeScope>test</excludeScope>
goes inside theconfiguration
node. -
Searene over 7 years@Thomas I think it's
maven clean install
, then you will findlib
intarget
-
Alan Donizete about 7 yearswhat would I have to do to copy only 1 dependency?
-
Martin Pabst about 7 years<classpathPrefix>lib/</classpathPrefix> helped me a lot. Thank you!
-
Divyang Shah over 6 years@Gobliins use ${project.build.directory}/lib instead of ${targetdirectory}
-
Admin almost 6 yearsWanted to upvote, but found out I had done so 2 years ago :)
-
Searene over 5 yearsMaybe it would be more appropriate if the phase were
package
instead ofinstall
-
Vyacheslav Cotruta over 4 yearsWould replace the
install
phase withprocess-resources
so that the dependencies are copied before thebuild
goal runs -
jla almost 4 yearsThank you for this simple option. I used the package target instead but otherwise did what I was looking for.
-
BraveBoy about 3 years@JesseChisholm thanks your solution worked
-
niken about 2 yearsdont 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/