maven · 2020-04-18 0

maven-dependency-plugin插件的使用

一、goal

maven-dependency-plugin 插件最常用的 goal 有:copy、copy-dependencies、unpack、unpack-dependencies

  1. copy:拷贝指定jar包到指定目录,与当前工程的依赖没有关系
  2. copy-dependencies:拷贝依赖jar包到指定目录
  3. unpack:解压指定jar包到指定目录,与当前工程的依赖没有关系
  4. unpack-dependencies:解压依赖jar包到指定目录

二、copy

copy 可以的配置的项比较多

<artifactItems> 是必须配置的,指定 jar 包
<outputDirectory> 默认值 ${project.build.directory}/dependency

1.pom文件

<groupId>org.example</groupId>
<artifactId>maven-haha</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </plugin>
    </plugins>
</build>

2.使用 dependency:copy

mvn clean dependency:copy

zxm@zxm-pc:~/IdeaProjects/java-test/maven-haha$ mvn clean dependency:copy
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< org.example:maven-haha >-----------------------
[INFO] Building maven-haha 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-haha ---
[INFO] Deleting /home/zxm/IdeaProjects/java-test/maven-haha/target
[INFO] 
[INFO] -----------------------< org.example:maven-haha >-----------------------
[INFO] Building maven-haha 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy (default-cli) @ maven-haha ---
[INFO] Configured Artifact: junit:junit:4.11:jar
[INFO] Copying junit-4.11.jar to /home/zxm/IdeaProjects/java-test/maven-haha/target/dependency/junit-4.11.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.976 s
[INFO] Finished at: 2020-04-18T18:57:47+08:00
[INFO] ------------------------------------------------------------------------

结果:

zxm@zxm-pc:~/IdeaProjects/java-test/maven-haha$ tree .
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   └── test
│       └── java
└── target
    └── dependency
        └── junit-4.11.jar

8 directories, 2 files

三、copy-dependencies

1.pom文件

<groupId>org.example</groupId>
<artifactId>maven-haha</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
        </plugin>
    </plugins>
</build>

2.使用 dependency:copy-dependencies

mvn clean dependency:copy-dependencies

zxm@zxm-pc:~/IdeaProjects/java-test/maven-haha$ mvn clean dependency:copy-dependencies
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< org.example:maven-haha >-----------------------
[INFO] Building maven-haha 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-haha ---
[INFO] Deleting /home/zxm/IdeaProjects/java-test/maven-haha/target
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ maven-haha ---
[INFO] Copying junit-4.13.jar to /home/zxm/IdeaProjects/java-test/maven-haha/target/dependency/junit-4.13.jar
[INFO] Copying hamcrest-core-1.3.jar to /home/zxm/IdeaProjects/java-test/maven-haha/target/dependency/hamcrest-core-1.3.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.997 s
[INFO] Finished at: 2020-04-18T19:02:22+08:00
[INFO] ------------------------------------------------------------------------

结果:

zxm@zxm-pc:~/IdeaProjects/java-test$ tree maven-haha/
maven-haha/
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   └── test
│       └── java
└── target
    └── dependency
        ├── hamcrest-core-1.3.jar
        └── junit-4.13.jar

8 directories, 3 files

四、unpack、unpack-dependencies

unpack、unpack-dependencies 是解压到指定目录,与 copy、copy-dependencies 操作类似