gpt4 book ai didi

maven - 复制项目的所有依赖项和传递依赖项

转载 作者:行者123 更新时间:2023-12-04 17:58:21 24 4
gpt4 key购买 nike

我是 maven 的新手。
(我已经搜索了几个小时的答案但没有运气。 mvn dependency:copy-dependencies 不能解决我的问题 )
我需要复制一个项目的所有依赖项(以 jar 的形式),如果我的一个 jar 依赖于另一个 Artifact ,则复制该 Artifact

示例 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”依赖于project2.artifact.jar
当我使用“mvn dependency:copy-dependencies”时,我得到了 project2.artifact.jar 但我没有得到 project3.artifact.jar,它是“project2”的依赖项 .

∎ 我没有 project2 的 pom,但它安装在我的本地存储库中。

如何获取项目 1 的所有依赖项,包括第二个 jar(“project3.artifact.jar”)?

project2 的 pom 看起来像这样:
但是当我去客户那里时我没有它,所以我使用“mvn install”手动安装project2.artifact.jar,project3.artifact.jar

项目2 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>

非常感谢高级

最佳答案

请使用最新版本的依赖插件:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies

如果不起作用,请检查:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:list
copy-dependency目标复制由 tree 解析和显示的相同 Artifact 和 list目标。

为了测试,我准备了 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>

我有这样的目录结构:
pr1
pom.xml - from question

pr2
pom.xml - from question

pr3
pom.xml - as shown above

现在我这样做:
cd pr3
mvn clean innstall

cd ../pr2
mvn clean innstall
mvn dependency:tree
dependency:tree输出:
[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

所以 project2 依赖于 project3
下一个:
cd ../pr1
mvn clean install
mvn dependency:tree
dependency:tree在 project1 输出中:
[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

所以也没关系。

现在复制:
mvn dependency:copy-dependencies

结果:
[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

如果您还想复制 project1 Artifact ,请将其添加到 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>
...

现在:
mvn clean dependency:copy-dependencies我们有:
[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

关于maven - 复制项目的所有依赖项和传递依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38431006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com