HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

超訳:Apache MavenでJavaアプリケーション開発

いまさら、人生初のApache Maven使用ということで。

詳細は以下のサイトに譲るとして。

このサイトの内容をベースにして、Javaアプリケーションの作成から実行までを超訳しておく。

Apache Mavenのセットアップ

環境がCentOS 7ということで、パッケージでインストールする。

# yum -y install maven

私の環境では、以下の依存ライブラリが合わせてインストールされた。トータル1+58個。

f:id:hhelibex:20211107222255p:plain

プロジェクトの作成・ビルド

まず、プロジェクトを作成する。

[~]$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> am.ik.archetype:elm-spring-boot-blank-archetype (Blank multi project for Spring Boot + Elm)
  (中略)
1832: remote -> org.apache.maven.archetypes:maven-archetype-profiles (-)
1833: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
1834: remote -> org.apache.maven.archetypes:maven-archetype-simple (An archetype which contains a simple Maven project.)
  (中略)
1838: remote -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.) 
  (中略)
3003: remote -> za.co.absa.hyperdrive:component-archetype_2.12 (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1833: 
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
8: 1.4
Choose a number: 8: 
Define value for property 'groupId': hhelibex
Define value for property 'artifactId': sample
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' hhelibex: : 
Confirm properties configuration:
groupId: hhelibex
artifactId: sample
version: 1.0-SNAPSHOT
package: hhelibex
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: hhelibex
[INFO] Parameter: artifactId, Value: sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: hhelibex
[INFO] Parameter: packageInPathFormat, Value: hhelibex
[INFO] Parameter: package, Value: hhelibex
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: hhelibex
[INFO] Parameter: artifactId, Value: sample
[INFO] Project created from Archetype in dir: /home/hhelibex/sample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2:00.363s
[INFO] Finished at: Sun Nov 07 22:56:52 JST 2021
[INFO] Final Memory: 13M/55M
[INFO] ------------------------------------------------------------------------
[~]$ 

基本的に、入力するのは以下の2行のみで、あとは単に[Enter]を押せばよい。

Define value for property 'groupId': hhelibex
Define value for property 'artifactId': sample

そしてビルド。

[~]$ cd sample
[sample]$ mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/hhelibex/sample/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/hhelibex/sample/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/hhelibex/sample/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/hhelibex/sample/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ sample ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running hhelibex.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.988 s - in hhelibex.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ sample ---
[INFO] Building jar: /home/hhelibex/sample/target/sample-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:28.550s
[INFO] Finished at: Sun Nov 07 23:05:15 JST 2021
[INFO] Final Memory: 15M/86M
[INFO] ------------------------------------------------------------------------
[sample]$ 

アプリケーションの実行

pom.xmlを編集する。pluginsタグの中に以下を追記する。

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
          <configuration>
            <mainClass>hhelibex.App</mainClass>
          </configuration>
        </plugin>

そして実行。

[sample]$ mvn exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ sample ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.067s
[INFO] Finished at: Sun Nov 07 23:12:56 JST 2021
[INFO] Final Memory: 9M/29M
[INFO] ------------------------------------------------------------------------
[sample]$ 

以上!