Make Maven to Copy Dependencies into Target/Lib

How to copy the dependency jar in a target folder through Maven

Plugins just declared in <pluginManagement> do nothing. This is just a configuration what to do when the plugin is declared elsewhere (in the same or in a module/child POM). You have to have a declaration in <build>/<plugins> somewhere, too:

However, this only configures plugins that are actually referenced within the plugins element in the children or in the current POM.

dependency:copy-dependencies says:

Goal that copies the project dependencies from the repository to a defined location.

That means dependency:copy-dependencies doesn't copy the JAR you create in the project but all that's declared under <dependencies>.

Furhermore, the prepare-package phase comes long before the install phase, even the JAR is only created in the next phase (package) so there wouldn't be anything to copy from the repository – if it were the proper choice – at the prepare-package phase (though there can be an older version of the JAR from a previous mvn install).

So, you might think to add to your POM:

    <dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

but this gives:

[FATAL] 'dependencies.dependency.[<groupId>:<artifactId>:<version>]'
for <groupId>:<artifactId>:<version> is referencing itself. @ line ..., column ...

So, you have to use one of those in Best practices for copying files with Maven.

Copy dependency of a maven project to specific folder

You need to use the outputDirectory property to define the required location where you would like the jars to be copied to.

Here is an example of the configuration you would add in your POM:

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

Alternatively you can pass this configuration directly via the command line:

mvn -DoutputDirectory=alternativeLocation dependency:copy-dependencies 

Maven copy only specific dependencies

The maven-bundle-plugin allows to embed dependencies:
https://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

<Embed-Dependency>javax.mail|javax.mail-api</Embed-Dependency>

Maven how to copy a specific folder inside a dependency to target/classes/myfolder

You most probably don't need copy-dependencies but unpack. Check the official example for further details.

The following example:

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.1.4</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
<includes>META-INF/resources/**/*.*</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Will copy under ${project.build.directory}/classes/myfolder the content of META-INF/resources which is basically the webjars folder. However you would also get the META-INF/resources tree structure.


To achieve exactly your intent (copy only a subfolder content of a dependency to a certain folder) you need to use two plugins: the maven-dependency-plugin and the maven-resources-plugin as following:

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.1.4</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/tmp</outputDirectory>
<includes>META-INF/resources/**/*.*</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/tmp/META-INF/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

The maven-dependency-plugin would copy our desired directory tree to the target\tmp folder, then the maven-resources-plugin would copy only the subfolder we want to the final directory.

Please note both plugins will be executed during the install phase but their declaration order it really important to have the final desired outcome.

Copy all output jars with dependencies into one folder

working spell is

mvn clean install
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:copy-dependencies -DoutputDirectory=deps -DincludeScope=runtime -DexcludeGroupIds=com.acadiasoft.im

note package -> install

how to copy dependency sources to a directory in maven

I would not use any of those solutions.

Just include the maven-dependeny-plugin to your maven build and adjust the configuration to your needs:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>none</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

You can change several items to suit your needs, for example, in the example I'm giving you, which I think directly gives you a solution, I've specified that in no phase are the dependencies to be copied the alternateLocation in the tmp folder. However I'm also saying that my new goal is copy-dependencies. So in the command line this would be something like:

mvn dependency:copy-dependencies

If you notice I now have the outputDirectory configured twice. Inside the execution it means that it will only be considered when you are running a specified maven build phase like packaging, clean, test, ... . As a first child sibling of the plugin node, it means that it will be considered when the command line explicitly invoques the dependency plugin, which is what you want.

You can find more about the maven-dependency plugin here:

  • https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html

Since you need the dependencies and the sources at the same time, the best way I can think of is by running maven normally without an implicit call to the actual plugin. If your run it through the post-clean phase (i.e. mvn post-clean) it will run the two following goals:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>post-clean</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
<execution>
<id>sources</id>
<phase>post-clean</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<outputDirectory>/tmp/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

It will always make the copy to the destination folder, but won't overwrite if the files are already there. I had to choose a non frequently used phase. post-clean seemed to be a best candidate here. This is only thinking that I want to isolate this kind of build. post-clean also cleans up the build. If you want just to keep using this plugin everytime you make a build then I would recommend putting it on the clean or maybe the install phase. That way it always happens on the background and you don't worry about it.



Related Topics



Leave a reply



Submit