ラベル Maven の投稿を表示しています。 すべての投稿を表示
ラベル Maven の投稿を表示しています。 すべての投稿を表示

2020年6月9日火曜日

Maven + Java14 + JavaFX14 + jpackage で Hello, World をパッケージ化する

前提

プロジェクト作成から実行まで

プロジェクトテンプレート作成

> cd ${WORK_DIR}
> mvn archetype:generate
[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
[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)
2: remote -> am.ik.archetype:graalvm-blank-archetype (Blank project for GraalVM)
...(snip)
2332: remote -> org.openjfx:javafx-archetype-fxml (-)
2333: remote -> org.openjfx:javafx-archetype-simple (-)
...(snip)
2720: remote -> xyz.luan.generator:xyz-generator (-)
2721: remote -> za.co.absa.hyperdrive:component-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1615: 2332
Choose org.openjfx:javafx-archetype-fxml version:
1: 0.0.1
2: 0.0.2
3: 0.0.3
4: 0.0.4
5: 0.0.5
Choose a number: 5:
Define value for property 'groupId': jp.dip.oyasirazu.study.javafx.helloworld
Define value for property 'artifactId': helloworld
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' jp.dip.oyasirazu.study.javafx.helloworld: :
[INFO] Using property: javafx-maven-plugin-version = 0.0.4
[INFO] Using property: javafx-version = 13
Confirm properties configuration:
groupId: jp.dip.oyasirazu.study.javafx.helloworld
artifactId: helloworld
version: 1.0-SNAPSHOT
package: jp.dip.oyasirazu.study.javafx.helloworld
javafx-maven-plugin-version: 0.0.4
javafx-version: 13
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: javafx-archetype-fxml:0.0.5
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: jp.dip.oyasirazu.study.javafx.helloworld
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: jp.dip.oyasirazu.study.javafx.helloworld
[INFO] Parameter: packageInPathFormat, Value: jp/dip/oyasirazu/study/javafx/helloworld
[INFO] Parameter: package, Value: jp.dip.oyasirazu.study.javafx.helloworld
[INFO] Parameter: groupId, Value: jp.dip.oyasirazu.study.javafx.helloworld
[INFO] Parameter: javafx-version, Value: 13
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: javafx-maven-plugin-version, Value: 0.0.4
[INFO] Project created from Archetype in dir: C:\Users\mikoto\project\tmp\HelloWorld\helloworld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:23 min
[INFO] Finished at: 2020-06-09T03:13:56+09:00
[INFO] ------------------------------------------------------------------------

> cd helloworld
  • 2332: remote -> org.openjfx:javafx-archetype-fxml (-) : これが FXML を使った JavaFX アプリテンプレート
  • Choose org.openjfx:javafx-archetype-fxml version: : とりあえず最新版を選択
  • Define value for property 'groupId': jp.dip.oyasirazu.study.javafx.helloworld : グループ ID。パッケージ名に使われる。
  • Define value for property 'artifactId': helloworld : アプリ名。 jar ファイルに使われる。
  • Define value for property 'version' 1.0-SNAPSHOT: : : アプリバージョン番号。 デフォルトだと jar ファイル名に追加される。

${WORK_DIR}/helloworld に Maven プロジェクトが作成される。

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>jp.dip.oyasirazu.study.javafx.helloworld</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>14</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>14</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.5</version>
                <configuration>
                    <mainClass>jp.dip.oyasirazu.study.javafx.helloworld.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • maven.compiler.source : 14 へ修正
  • maven.compiler.target : 14 へ修正
  • javafx-controls : version14 へ修正
  • javafx-fxml : version14 へ修正
  • maven-compiler-plugin : configuration/release14 へ修正
  • javafx-maven-plugin : version0.0.5 へ修正

コンパイル・ jar の作成

以下コマンドでコンパイルと jar 作成まで行われる。

mvn package

.\target\helloworld-1.0-SNAPSHOT.jar に jar が生成される。

コンパイルだけなら mvn compile

とりあえず実行

java -p ".\target\helloworld-1.0-SNAPSHOT.jar;$Env:JAVAFX_HOME\lib" -m "jp.dip.oyasirazu.study.javafx.helloworld/jp.dip.oyasirazu.study.javafx.helloworld.App"
図 1: テンプレートで生成されるアプリ

ボタンを押すたびに View が切り替わるアプリ。

最小構成 JRE 生成

依存モジュールを確認

jdeps で依存モジュールを確認。

> $DEPS = jdeps --print-module-deps --ignore-missing-deps --module-path "$Env:JAVAFX_HOME\lib" .\target\helloworld-1.0-SNAPSHOT.jar
> echo $DEPS
java.base,javafx.fxml,javafx.graphics

最小構成 JRE を作成

jlink で最小構成 JRE を生成する。

> jlink --no-header-files --no-man-pages --compress=2 --strip-debug --add-modules $DEPS,jdk.charsets,jdk.localedata,jp.dip.oyasirazu.study.javafx.helloworld --include-locales=ja --module-path ".\target\helloworld-1.0-SNAPSHOT.jar;$Env:JAVAFX_HOME\jmods" --output "./release/app" --launcher helloworld="jp.dip.oyasirazu.study.javafx.helloworld/jp.dip.oyasirazu.study.javafx.helloworld.App"

.\release\app に最小構成 JRE が生成され、.\release\app\bin\helloworld.bat にランチャーが生成される。

最小構成 JRE で実行してみる

.\release\app\bin\helloworld.bat

OK, うごいた。

パッケージング

jpackage でパッケージ化

jpackage -n helloworld --app-image ./release/app --dest ./release/package --vendor "mikoto2000" --win-dir-chooser
  • -n: アプリケーション名
  • --app-image: アプリイメージ。 jlink で作ったディレクトリを指定する
  • --dest: 出力先ディレクトリ。このディレクトリに [アプリケーション名].exe が生成される
  • --vendor: Windows だと入れないとエラーになるようだ
  • --win-dir-chooser: ユーザーにインストール場所を選択させるためのフラグ

.\release\package\helloworld-1.0.exe にインストーラーが生成される。

インストール手順はインストール - jpackage を使って HelloWorld をパッケージ化する - mikoto2000 の日記を参照。

以上。

参考資料

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
>
アプリケーションが終了しました

以上。

参考資料