Maven on CentOS 7 チュートリアル

## 参考文献と先人の知恵

- [Maven – Welcome to Apache Maven](https://maven.apache.org/)
- [【超初心者向け】Maven超入門](https://qiita.com/tarosa0001/items/e5667cfa857529900216)
- [よく使うMavenコマンド集](https://qiita.com/KevinFQ/items/e8363ad6123713815e68)
- [java - Can't execute jar- file: "no main manifest attribute" - Stack Overflow](https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute)


## 手順

1. OpenJDK をインストールする。

    ```bash
    sudo yum -y install java-1.8.0-openjdk-devel
    ```

1. [Maven – Welcome to Apache Maven](https://maven.apache.org/) にアクセスする。
1. 画面左側の *About Maven* より, **Download** をクリックする。
1. *Binary tar.gz archive* の **Link** を右クリックし, URL をコピーする。
    - 今回は *apache-maven-3.5.4-bin.tar.gz* とする。
1. Maven をダウンロードする。Tera Term を使用している場合, リンクは右クリックで貼り付けられる。

    ```bash
    cd /usr/local/src; pwd
    sudo curl -LO <URL>
    ```

1. Maven を展開する。

    ```bash
    cd ..; pwd
    gunzip -c src/apache-maven-3.5.4-bin.tar.gz | sudo tar -xpf -
    sudo ln -sv apache-maven-3.5.4 apache-maven
    ```

1. Maven への PATH を設定する。

    ```bash
    cd; pwd
    cp -aiv .bash_profile .bash_profile.`date +%Y%m%d`
    echo 'PATH=/usr/local/apache-maven/bin:${PATH}' >> .bash_profile
    ```

1. 再ログインし, mvn コマンドが使用できることを確認する。

    ```bash
    mvn -version
    ```

    ```
    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T03:33:14+09:00)
    Maven home: /usr/local/apache-maven
    Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre
    Default locale: ja_JP, platform encoding: UTF-8
    OS name: "linux", version: "3.10.0-862.11.6.el7.x86_64", arch: "amd64", family: "unix"
    ```

1. プロジェクトのひな形を作成する。

    ```bash
    mkdir -v /var/tmp/maven
    cd /var/tmp/maven; pwd
    mvn archetype:generate
    ```

    ```
    [INFO] Scanning for projects...
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (3.9 kB at 2.6 kB/s)
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom
    (中略)
    2244: remote -> xyz.luan.generator:xyz-generator (-)
    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1252: ★ Enter キー押下
    Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
    1: 1.0-alpha-1
    2: 1.0-alpha-2
    3: 1.0-alpha-3
    4: 1.0-alpha-4
    5: 1.0
    6: 1.1
    7: 1.3
    Choose a number: 7: ★ Enter キー押下
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.3/maven-archetype-quickstart-1.3.pom
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.3/maven-archetype-quickstart-1.3.pom (1.6 kB at 7.1 kB/s)
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/1.3/maven-archetype-bundles-1.3.pom
    (中略)
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.3/maven-archetype-quickstart-1.3.jar (7.0 kB at 30 kB/s)
    Define value for property 'groupId': ★ "com.myapp" と入力
    Define value for property 'artifactId': ★ "myapp" と入力
    Define value for property 'version' 1.0-SNAPSHOT: : ★ Enter キー押下
    Define value for property 'package' com.myapp: : ★ Enter キー押下
    Confirm properties configuration:
    groupId: com.myapp
    artifactId: myapp
    version: 1.0-SNAPSHOT
    package: com.myapp
     Y: : ★ Enter キー押下
    [INFO] ----------------------------------------------------------------------------
    [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.3
    [INFO] ----------------------------------------------------------------------------
    [INFO] Parameter: groupId, Value: com.myapp
    [INFO] Parameter: artifactId, Value: myapp
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: package, Value: com.myapp
    [INFO] Parameter: packageInPathFormat, Value: com/myapp
    [INFO] Parameter: package, Value: com.myapp
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: groupId, Value: com.myapp
    [INFO] Parameter: artifactId, Value: myapp
    [INFO] Project created from Archetype in dir: /var/tmp/maven/myapp
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 03:48 min
    [INFO] Finished at: 2018-09-28T16:37:18+09:00
    [INFO] ------------------------------------------------------------------------
    [ansible@centos7-02 maven]$
    ```

1. 検証する。

    ```bash
    cd myapp; pwd
    mvn validate
    ```

    ```
    [INFO] Scanning for projects...
    [INFO]
    [INFO] --------------------------< com.myapp:myapp >---------------------------
    [INFO] Building myapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.372 s
    [INFO] Finished at: 2018-09-28T16:38:15+09:00
    [INFO] ------------------------------------------------------------------------
    ```

1. コンパイルする。

    ```bash
    mvn compile
    ```

    ```
    [INFO] Scanning for projects...
    [INFO]
    [INFO] --------------------------< com.myapp:myapp >---------------------------
    [INFO] Building myapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/3.0.2/maven-resources-plugin-3.0.2.pom
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/3.0.2/maven-resources-plugin-3.0.2.pom (7.1 kB at 4.4 kB/s)
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/30/maven-plugins-30.pom
    (中略)
    Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0-M7/qdox-2.0-M7.jar (315 kB at 321 kB/s)
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /var/tmp/maven/myapp/target/classes
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 28.664 s
    [INFO] Finished at: 2018-09-28T16:39:58+09:00
    [INFO] ------------------------------------------------------------------------
    ```

1. テストする。

    ```bash
    mvn test
    ```

    ```
    [INFO] Scanning for projects...
    [INFO]
    [INFO] --------------------------< com.myapp:myapp >---------------------------
    [INFO] Building myapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.20.1/maven-surefire-plugin-2.20.1.pom
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.20.1/maven-surefire-plugin-2.20.1.pom (5.2 kB at 2.9 kB/s)
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire/2.20.1/surefire-2.20.1.pom
    (中略)
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.20.1/surefire-junit4-2.20.1.jar (82 kB at 280 kB/s)
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.myapp.AppTest
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.259 s - in com.myapp.AppTest
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 19.793 s
    [INFO] Finished at: 2018-09-28T16:41:10+09:00
    [INFO] ------------------------------------------------------------------------
    ```

1. パッケージを生成する。

    ```bash
    mvn package
    ```

    ```
    [INFO] Scanning for projects...
    [INFO]
    [INFO] --------------------------< com.myapp:myapp >---------------------------
    [INFO] Building myapp 1.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.0.2/maven-jar-plugin-3.0.2.pom
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.0.2/maven-jar-plugin-3.0.2.pom (6.2 kB at 3.7 kB/s)
    Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.0.2/maven-jar-plugin-3.0.2.jar
    (中略)
    Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.11/commons-compress-1.11.jar (426 kB at 277 kB/s)
    [INFO] Building jar: /var/tmp/maven/myapp/target/myapp-1.0-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 14.051 s
    [INFO] Finished at: 2018-09-28T16:42:26+09:00
    [INFO] ------------------------------------------------------------------------
    ```

1. 実行する: **エラー!**

    ```bash
    ls -l target/myapp-1.0-SNAPSHOT.jar
    LANG=C java -jar target/myapp-1.0-SNAPSHOT.jar
    ```

    ```
    no main manifest attribute, in target/myapp-1.0-SNAPSHOT.jar
    ```

1. pom.xml を修正する。

    ```bash
    cp -aiv pom.xml pom.xml.`date +%Y%m%d`
    vi pom.xml
    diff -u pom.xml.`date +%Y%m%d` pom.xml
    ```

    ```diff
    --- pom.xml.20180928    2018-09-28 16:37:18.062624925 +0900
    +++ pom.xml     2018-09-28 16:46:09.944109262 +0900
    @@ -50,6 +50,15 @@
             <plugin>
               <artifactId>maven-jar-plugin</artifactId>
               <version>3.0.2</version>
    +         <configuration>
    +            <archive>
    +              <manifest>
    +               <addClasspath>true</addClasspath>
    +               <classpathPrefix>lib/</classpathPrefix>
    +               <mainClass>com.myapp.App</mainClass>
    +              </manifest>
    +            </archive>
    +         </configuration>
             </plugin>
             <plugin>
               <artifactId>maven-install-plugin</artifactId>
    ```

1. パッケージを再生成する。

    ```bash
    mvn clean
    mvn package
    ```

1. 実行する: **OK!**

    ```bash
    LANG=C java -jar target/myapp-1.0-SNAPSHOT.jar
    ```

    ```
    Hello World!
    ```


どっとはらい。