pom.xmlをmavenをインストールせずビルドする方法



maven使うんですが、自分の環境に入れずにDockerのコンテナ内でビルドしようと言う話です。

https://hub.docker.com/_/maven
にある通り、pom.xmlがあるディレクトリで以下コマンド実行すればビルドされます。

```
docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install
```

実施例：
適当にpom.xmlがあるmavenのプロジェクトを用意してください。

今回はこれ使います：
https://github.com/javaee/cargotracker


```
[~/lab/apps]
> git clone https://github.com/javaee/cargotracker.git
Cloning into 'cargotracker'...
remote: Enumerating objects: 1300, done.
remote: Total 1300 (delta 0), reused 0 (delta 0), pack-reused 1300
Receiving objects: 100% (1300/1300), 8.57 MiB | 1.72 MiB/s, done.
Resolving deltas: 100% (552/552), done.
[~/lab/apps]
> cd cargotracker/
[~/lab/apps/cargotracker]
> ls
CONTRIBUTING.md              eventLogger-flow.NavData     license.txt                  nbactions.xml                readme - CARGOTRACKER-54.txt readmeWebLogic
LICENSE                      faces-config.NavData         nb-configuration.xml         pom.xml                      readme.txt                   src
[~/lab/apps/cargotracker]
> docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn package
[INFO] Scanning for projects...
Downloading: http://repository.primefaces.org/org/jboss/arquillian/arquillian-bom/1.1.10.Final/arquillian-bom-1.1.10.Final.pom
Downloading: https://repo.maven.apache.org/maven2/org/jboss/arquillian/arquillian-bom/1.1.10.Final/arquillian-bom-1.1.10.Final.pom
Downloaded: https://repo.maven.apache.org/maven2/org/jboss/arquillian/arquillian-bom/1.1.10.Final/arquillian-bom-1.1.10.Final.pom (11 KB at 14.7 KB/sec)
(省略）
Downloaded: https://repo.maven.apache.org/maven2/com/thoughtworks/xstream/xstream/1.4.4/xstream-1.4.4.jar (473 KB at 124.8 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [cargo-tracker] in [/usr/src/mymaven/target/cargo-tracker]
[INFO] Processing war project
[INFO] Copying webapp resources [/usr/src/mymaven/src/main/webapp]
[INFO] Webapp assembled in [7971 msecs]
[INFO] Building war: /usr/src/mymaven/target/cargo-tracker.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:12 min
[INFO] Finished at: 2020-04-10T07:42:20+00:00
[INFO] Final Memory: 26M/152M
[INFO] ------------------------------------------------------------------------

[~/lab/apps/cargotracker]
> ls -tlr
total 192
-rw-r--r--   1 nobuhiko.sekiya  staff   1551 Apr 10 16:34 CONTRIBUTING.md
-rw-r--r--   1 nobuhiko.sekiya  staff  39395 Apr 10 16:34 LICENSE
-rw-r--r--   1 nobuhiko.sekiya  staff    205 Apr 10 16:34 eventLogger-flow.NavData
-rw-r--r--   1 nobuhiko.sekiya  staff    211 Apr 10 16:34 faces-config.NavData
-rw-r--r--   1 nobuhiko.sekiya  staff   1092 Apr 10 16:34 license.txt
-rw-r--r--   1 nobuhiko.sekiya  staff   1686 Apr 10 16:34 nb-configuration.xml
-rw-r--r--   1 nobuhiko.sekiya  staff    466 Apr 10 16:34 nbactions.xml
-rw-r--r--   1 nobuhiko.sekiya  staff   9595 Apr 10 16:34 pom.xml
-rw-r--r--   1 nobuhiko.sekiya  staff    598 Apr 10 16:34 readme - CARGOTRACKER-54.txt
-rw-r--r--   1 nobuhiko.sekiya  staff   9711 Apr 10 16:34 readme.txt
-rw-r--r--   1 nobuhiko.sekiya  staff    687 Apr 10 16:34 readmeWebLogic
drwxr-xr-x   5 nobuhiko.sekiya  staff    160 Apr 10 16:34 src
drwxr-xr-x  10 nobuhiko.sekiya  staff    320 Apr 10 16:42 target
[~/lab/apps/cargotracker]
> ls target/
cargo-tracker/          classes/                generated-test-sources/ maven-status/
cargo-tracker.war       generated-sources/      maven-archiver/         test-classes/
[~/lab/apps/cargotracker]
```

一からリポジトリから全てダウンロードするので時間はかかりますが、簡単にビルドできました。

時間がかかるので、２回目を効率化する場合はこんな感じでしょうか

```
# 1回目は--rmは入れずにdocker run
docker run -it --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install

# 2回目はstartで。
docker start -a my-maven-project
```

以上