Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy dependencies jars (without test jars) to a directory using maven?

Tags:

maven

I see maven-dependency-plugin does this; however, it seems to copy everything (including test jars) to the destination directory. Anyone know how to configure this plugin to exclude test jars?

like image 369
Mike Avatar asked Nov 29 '11 12:11

Mike


People also ask

Does Maven download dependencies?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.

How can I download all the dependency files?

If you want do download them all, you can try using mvn dependency:copy-dependencies . Then, you'll find all project dependencies in target/dependencies directory of your project. This also includes transitive dependencies. To add them all as eclipse dependencies, you may want to try maven-eclipse-plugin .

Which Maven phase copy a project artifact in the local repository for use as a dependency?

The dependency:copy goal can also be used to copy the just built artifact to a custom location if desired. It must be bound to any phase after the package phase so that the artifact exists in the repository.


1 Answers

Mike answered their own question in a comment above. I think Mike's use case is similar to mine where I want to copy all of the jars I depend upon as well as my own jar in order to create a directory hierarchy sufficient to execute the program without including those dependencies directly into my own jar.

The answer to achieve this is:

<includeScope>compile</includeScope> 

This directive goes into the section of the pom.xml for the maven-dependency plugin. For example:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-dependency-plugin</artifactId>     <version>2.4</version>     <executions>         <execution>             <id>copy-dependencies</id>             <phase>prepare-package</phase>             <goals>                 <goal>copy-dependencies</goal>             </goals>             <configuration>                 <outputDirectory>${project.build.directory}/lib</outputDirectory>                 <includeScope>compile</includeScope>             </configuration>         </execution>     </executions> </plugin> 

excludeScope won't work because excluding test aborts the build and excludes all possible scopes. Instead the included scope needs to be adjusted.

like image 100
fuzzyBSc Avatar answered Nov 10 '22 14:11

fuzzyBSc