mavenを使用して依存関係のあるjarファイルを一括ダウンロードする方法

mavenを使用して依存関係のあるjarファイルをダウンロードする方法を紹介します。

# 環境

+ Windows
+ apache-maven-3.6.2

# 1. mavenプロジェクトを作成

mavenプロジェクトを作成するディレクトリへ移動します。
（ここでは「C:\Data\mvn_project」配下に作成します。）

```
c:>cd c:\data\mvn_project
c:\Data\mvn_project>
```

以下のコマンドでmavenプロジェクトを作成します。

`mvn archetype:generate -DgroupId=com.sample -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false`

```
c:\Data\mvn_project>mvn archetype:generate -DgroupId=com.sample -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom---
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: c:\Data\mvn_project
[INFO] Parameter: package, Value: com.sample
[INFO] Parameter: groupId, Value: com.sample
[INFO] Parameter: artifactId, Value: test
[INFO] Parameter: packageName, Value: com.sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: c:\Data\mvn_project\test

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  29.821 s
[INFO] Finished at: 2021-01-28T16:59:41+09:00
[INFO] ------------------------------------------------------------------------

c:\Data\mvn_project>
```

`dir`コマンドで「test」ディレクトリが作成されていることを確認します。

```
c:\Data\mvn_project>dir
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は F039-627E です

 c:\Data\mvn_project のディレクトリ

2021/01/28  16:59    <DIR>          .
2021/01/28  16:59    <DIR>          ..
2021/01/28  16:59    <DIR>          test
               0 個のファイル                   0 バイト
               3 個のディレクトリ  159,863,459,840 バイトの空き領域

c:\Data\mvn_project>cd test

c:\Data\mvn_project\test>dir
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は F039-627E です

 c:\Data\mvn_project\test のディレクトリ

2021/01/28  16:59    <DIR>          .
2021/01/28  16:59    <DIR>          ..
2021/01/28  16:59               649 pom.xml
2021/01/28  16:59    <DIR>          src
               1 個のファイル                 649 バイト
               3 個のディレクトリ  159,863,328,768 バイトの空き領域

c:\Data\mvn_project\test>
```

# 2. pom.xml に依存関係を追加

「c:\Data\mvn_project\test」配下のpom.xmlに、依存関係を追加します。

ここでは「Spring Web MVC 5.3.3」で必要なjarファイルをダウンロードします。

mavenリポジトリで依存関係を確認します。

![mvn_01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/292434/4d63fe17-bfc4-0724-42ff-af93e83b2753.png)

```xml:依存関係
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.3</version>
</dependency>
```

「pom.xml」に上記の「依存関係」を追加します。
 
```xml:c:\Data\mvn_project\test\pom.xml（修正前）
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample</groupId>
  <artifactId>test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
```

```xml:c:\Data\mvn_project\test\pom.xml（修正後）
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample</groupId>
  <artifactId>test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>                                   <!-- 追加 -->
      <groupId>org.springframework</groupId>       <!-- 追加 -->
      <artifactId>spring-webmvc</artifactId>       <!-- 追加 -->
      <version>5.3.3</version>                     <!-- 追加 -->
      </dependency>                                <!-- 追加 -->

  </dependencies>
</project>
```

# 3. jarファイル一括ダウンロード

以下のコマンドで「Spring Web MVC 5.3.3」で必要なjarファイルを一括ダウンロードします。

`mvn dependency:copy-dependencies -DoutputDirectory=lib`
（`-DoutputDirectory`でダウンロード先に「lib」ディレクトリを指定）

```
c:\Data\mvn_project\test>mvn dependency:copy-dependencies -DoutputDirectory=lib
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.sample:test >---------------------------
[INFO] Building test 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.3.3/spring-webmvc-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.3.3/spring-webmvc-5.3.3.pom (2.9 kB at 2.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.pom (2.2 kB at 6.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.pom (2.0 kB at 5.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.3.3/spring-core-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.3.3/spring-core-5.3.3.pom (2.0 kB at 5.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.pom (1.8 kB at 5.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.3.3/spring-context-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.3.3/spring-context-5.3.3.pom (2.6 kB at 7.5 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.pom (2.1 kB at 5.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-web/5.3.3/spring-web-5.3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-web/5.3.3/spring-web-5.3.3.pom (2.2 kB at 5.9 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.3.3/spring-webmvc-5.3.3.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.3.3/spring-context-5.3.3.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.jar (374 kB at 148 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.jar (24 kB at 8.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.3.3/spring-webmvc-5.3.3.jar (996 kB at 272 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-web/5.3.3/spring-web-5.3.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.jar (696 kB at 182 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.jar (283 kB at 59 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.3.3/spring-context-5.3.3.jar (1.2 MB at 240 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-web/5.3.3/spring-web-5.3.3.jar (1.6 MB at 179 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar (1.5 MB at 154 kB/s)
[INFO]
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ test --
-
[INFO] Copying spring-webmvc-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-webmvc-5.3.3.jar
[INFO] Copying junit-3.8.1.jar to c:\Data\mvn_project\test\lib\junit-3.8.1.jar
[INFO] Copying spring-jcl-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-jcl-5.3.3.jar
[INFO] Copying spring-beans-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-beans-5.3.3.jar
[INFO] Copying spring-context-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-context-5.3.3.jar
[INFO] Copying spring-core-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-core-5.3.3.jar
[INFO] Copying spring-aop-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-aop-5.3.3.jar
[INFO] Copying spring-expression-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-expression-5.3.3.jar
[INFO] Copying spring-web-5.3.3.jar to c:\Data\mvn_project\test\lib\spring-web-5.3.3.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  31.649 s
[INFO] Finished at: 2021-01-28T17:22:24+09:00
[INFO] ------------------------------------------------------------------------

c:\Data\mvn_project\test>
```

「`lib`」ディレクトリ配下を確認します。

```
c:\Data\mvn_project\test>cd lib

c:\Data\mvn_project\test\lib>dir
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は F039-627E です

 c:\Data\mvn_project\test\lib のディレクトリ

2021/01/28  17:22    <DIR>          .
2021/01/28  17:22    <DIR>          ..
2021/01/28  17:22           121,070 junit-3.8.1.jar
2021/01/28  17:22           374,340 spring-aop-5.3.3.jar
2021/01/28  17:22           695,878 spring-beans-5.3.3.jar
2021/01/28  17:22         1,243,858 spring-context-5.3.3.jar
2021/01/28  17:22         1,467,336 spring-core-5.3.3.jar
2021/01/28  17:22           282,517 spring-expression-5.3.3.jar
2021/01/28  17:22            23,943 spring-jcl-5.3.3.jar
2021/01/28  17:22         1,565,794 spring-web-5.3.3.jar
2021/01/28  17:22           996,262 spring-webmvc-5.3.3.jar
               9 個のファイル           6,770,998 バイト
               2 個のディレクトリ  159,838,789,632 バイトの空き領域

c:\Data\mvn_project\test\lib>
```

「`spring-webmvc-5.3.3.jar`」とそれに必要なjarファイルがダウンロードされていることが確認できます。

---
以上