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

时间:2016-07-18 07:35:23

标签: maven

我是maven的新手。 (我已经搜索了几个小时的答案,但没有运气。 mvn依赖:复制依赖不能解决我的问题) 我需要复制项目的所有依赖项(以jar形式),如果我的一个罐子依赖于另一个工件副本那个工件

示例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

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>

非常感谢先进的

1 个答案:

答案 0 :(得分:1)

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

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目标会复制由treelist目标解析和显示的相同工件。

为了测试我准备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
project1输出中的

dependency:tree

[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工件,请将其添加到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