Reputation: 61
I’m new to maven. (I have searched for hours for the answer but without luck. mvn dependency:copy-dependencies do not solve my problem) I need to copy all the dependencies of a project (in the form of jars) and if one of my jars depend on another artifact copy that artifact as well.
Example project1 pom.xml:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project1</groupId>
<artifactId> project1.utils</artifactId>
<version>1.0</version>
<name> project1. utils </name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>project2</groupId>
<artifactId>project2.artifact</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifestEntries>
<Build-Number>${buildNumber}</Build-Number>
<Revision>${Revision}</Revision>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
“project1” have a dependency of project2.artifact.jar. When I use “mvn dependency:copy-dependencies”, I get project2.artifact.jar but I do not get project3.artifact.jar that is a dependency of “project2”.
I do not have the pom of project2, but it is installed in my local repository.
How can I get all the dependencies of project 1 including the second jar (“project3.artifact.jar”)?
Pom of project2 would look something like this, but I don't have it when I go to a client. So I install project2.artifact.jar, project3.artifact.jar manually using "mvn install".
Project2 pom.xml:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project2</groupId>
<artifactId>project2.artifact</artifactId>
<version>2.0</version>
<name>project2.artifact</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>project3</groupId>
<artifactId>project3.artifact</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifestEntries>
<Build-Number>${buildNumber}</Build-Number>
<Revision>${Revision}</Revision>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: 4
Views: 5290
Reputation: 8497
Please use the latest version of dependency plugin:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies
If not works please check:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:list
copy-dependency
goal copies the same artifacts which is resolved and displayed by tree
and list
goals.
For testing I prepare project3 pom.xml:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project3</groupId>
<artifactId>project3.artifact</artifactId>
<version>3.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I have such directory structure:
pr1
pom.xml - from question
pr2
pom.xml - from question
pr3
pom.xml - as shown above
Now I do:
cd pr3
mvn clean innstall
cd ../pr2
mvn clean innstall
mvn dependency:tree
dependency:tree
output:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ project2.artifact ---
[INFO] project2:project2.artifact:jar:2.0
[INFO] \- project3:project3.artifact:jar:3.0:compile
So project2 depends on project3 next:
cd ../pr1
mvn clean install
mvn dependency:tree
dependency:tree
in project1 output:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ project1.utils ---
[INFO] project1:project1.utils:jar:1.0
[INFO] \- project2:project2.artifact:jar:2.0:compile
[INFO] \- project3:project3.artifact:jar:3.0:compile
So it is also ok.
And now copy:
mvn dependency:copy-dependencies
With result:
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ project1.utils ---
[INFO] Copying project3.artifact-3.0.jar to ...\pr1\target\dependency\project3.artifact-3.0.jar
[INFO] Copying project2.artifact-2.0.jar to ...\pr1\target\dependency\project2.artifact-2.0.jar
If you also want to copy project1 artifact add it to dependency in project1 pom.xml
...
<dependencies>
<dependency>
<groupId>project2</groupId>
<artifactId>project2.artifact</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>project1</groupId>
<artifactId>project1.utils</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
...
Now:
mvn clean dependency:copy-dependencies
we have:
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ project1.utils ---
[INFO] Copying project3.artifact-3.0.jar to ...\pr1\target\dependency\project3.artifact-3.0.jar
[INFO] Copying project2.artifact-2.0.jar to ...\pr1\target\dependency\project2.artifact-2.0.jar
[INFO] Copying project1.utils-1.0.jar to ...\pr1\target\dependency\project1.utils-1.0.jar
Upvotes: 1