2019年4月2日火曜日

Maven で Java プロジェクト作成から jdb デバッグするまで

環境

  • OS: Windows 10 Pro
  • シェル: Windows PowerShell
  • Java: java version "1.8.0_201"
  • Maven: Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T03:41:47+09:00)

プロジェクト作成

mvn archetype:generate -DarchetypeArtifactId=maven -archetype-quickstart -DarchetypeVersion="1.4" を実行することで、対話的にプロジェクト情報を設定できる。

> mvn archetype:generate -DarchetypeArtifactId=maven -archetype-quickstart -DarchetypeVersion="1.4"
[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.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': jp.dip.oyasirazu.study.maven
Define value for property 'artifactId': firststep
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' jp.dip.oyasirazu.study.maven: : jp.dip.oyasirazu.study.maven.firststep
Confirm properties configuration:
groupId: jp.dip.oyasirazu.study.maven
artifactId: firststep
version: 1.0-SNAPSHOT
package: jp.dip.oyasirazu.study.maven.firststep
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: jp.dip.oyasirazu.study.maven
[INFO] Parameter: artifactId, Value: firststep
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: jp.dip.oyasirazu.study.maven.firststep
[INFO] Parameter: packageInPathFormat, Value: jp/dip/oyasirazu/study/maven/firststep
[INFO] Parameter: package, Value: jp.dip.oyasirazu.study.maven.firststep
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: jp.dip.oyasirazu.study.maven
[INFO] Parameter: artifactId, Value: firststep
[INFO] Project created from Archetype in dir: C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  40.167 s
[INFO] Finished at: 2019-04-02T05:32:01+09:00
[INFO] ------------------------------------------------------------------------

ビルド

作成したプロジェクトディレクトリで mvn package を実行すると、コンパイル・テスト・jar 作成までやってくれる。

> cd \PATH\TO\firststep
> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< jp.dip.oyasirazu.study.maven:firststep >---------------
[INFO] Building firststep 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ firststep ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ firststep ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ firststep ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ firststep ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ firststep ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running jp.dip.oyasirazu.study.maven.firststep.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 s - in jp.dip.oyasirazu.study.maven.firststep.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) @ firststep ---
[INFO] Building jar: C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\target\firststep-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.104 s
[INFO] Finished at: 2019-04-02T05:46:55+09:00
[INFO] ------------------------------------------------------------------------

デバッグ

テストでデバッグしたい場合は、 mvn "-Dmaven.surefire.debug" test を実行する。

> cd \PATH\TO\firststep
> mvn "-Dmaven.surefire.debug" test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< jp.dip.oyasirazu.study.maven:firststep >---------------
[INFO] Building firststep 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ firststep ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\s
rc\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ firststep ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ firststep ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\mikoto\project\MiscellaneousStudy\java\Maven\first_step\firststep\s
rc\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ firststep ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ firststep ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Listening for transport dt_socket at address: 5005

localhost:5005 で待ち受けるので、 jdb で接続する。

> cd \PATH\TO\firststep
> jdb -sourcepath .\src\test\java -connect com.sun.jdi.SocketAttach:hostname=localhost,port=5005
捕捉されないjava.lang.Throwableの設定
遅延した捕捉されないjava.lang.Throwableの設定
jdbの初期化中...
>
VMが開始されました: 現在のコール・スタックにフレームがありません

main[1] stop in jp.dip.oyasirazu.study.maven.firststep.AppTest.shouldAnswerWithTrue
遅延したブレークポイントjp.dip.oyasirazu.study.maven.firststep.AppTest.shouldAnswerWithTrue。
クラスがロードされた後に設定されます。
main[1] run
> 遅延したブレークポイントjp.dip.oyasirazu.study.maven.firststep.AppTest.shouldAnswerWithTrueの設定

ヒットしたブレークポイント: "スレッド=main", jp.dip.oyasirazu.study.maven.firststep.AppTest.shouldAnswerWithTrue()、行=18 bci=0
18            assertTrue( true );

main[1] list
14         */
15        @Test
16        public void shouldAnswerWithTrue()
17        {
18 =>         assertTrue( true );
19        }
20    }
main[1] cont
>
アプリケーションが終了しました

以上。

参考資料