読者です 読者をやめる 読者になる 読者になる

くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

JavaFXでクライアントアプリを作ってみる(ビルド編)

前回の続き。JavaFXの強みとして、各OSのNativeアプリにビルドできることがある。

Windows用にビルドしたときの備忘録。

wannabe-jellyfish.hatenablog.com

JavaFXのでビルドできる形式

形式 インストール場所(デフォルトは太字) OS 事前に必要なソフトウェア
EXE user : %LOCALAPPDATA%
System: %ProgramFiles%
Windows Inno Setup 5 or later
MSI user: %LOCALAPPDATA%
System: %ProgramFiles%
Windows WiX 3.0 or later
DMG user: デスクトップフォルダ
System: /Applications
OS X
PKG user: デスクトップフォルダ
System: /Applications
OS X
RPM user: unsupported
System: /opt
Linux RPMBuild
DEB user: unsupported
System: /opt
Linux Debian packaging tools

ほかにもFatJarとか、WebページやJNLPなどにもパッケージングができる!

refs: Self-Contained Application Packaging

JavaFXでのビルドのやり方

JavaFXでのビルドの方法は2通りある

  1. build.fxbuildというビルドファイルを作成し、ビルドする方法
  2. javafx-maven-pluginjfx:nativeゴールで、ビルドする方法

今回はMavenプロジェクトでのビルドについて。

javafx-maven-pluginjfx:nativeを使ってビルドしてみる

Windows用のインストーラを作成するためには、上記の表の「事前に必要なソフトウェア」をインストール必要がある。

とりあえず、実行してみて、以下のようなエラーが出ると、必要なソフトウェアが足りてないので、確認してみるとよい。

[iscc.exe]バージョン0が検出されましたが、バージョン5が必要です。
構成の問題のため、バンドラEXEインストーラがスキップされました: Inno Setupコンパイラ(iscc.exe)が見つかりません。  
次の修正を行ってください:   Inno Setup 5以降をhttp://www.jrsoftware.orgからダウンロードし、PATHに追加します。
構成の問題のため、バンドラMSIインストーラがスキップされました: WiXツール(light.exe、candle.exe)が見つかりません。  
次の修正を行ってください:   WiX 3.0以降をhttp://wix.sf.netからダウンロードし、PATHに追加します。

ビルド時の注意

JavaFXのビルド自体の仕様として、ビルド時のJAVA_HOMEは、JDK内のJREでなければならない。

そのため、Maven実行時に、JDK内のJREのパスを指定したjava.homeパラメタを渡す必要がある。

Eclipseだとこんな感じ。

f:id:wannabe-jellyfish:20160305222819p:plain

ちなみに、ちゃんと指定していないとこんなエラーメッセージが出る

[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

インストーラ関連の設定もろもろ

アイコンを変える

インストーラのアイコンはsrc/main/deploy/package/windows/java-fx-sample.icoに入っている。

これを別のに差し替えればOK

f:id:wannabe-jellyfish:20160305223958p:plain

ビルドする形式をEXEだけにする

プラグインの設定で変更。「」に<bundler>exe</bundler>を追加すればOK

デフォルトではAllになっているので、MSIも出力されている感じ

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.2.0</version>
    <configuration>
        <mainClass>jp.sample.javafx.App</mainClass>
        <bundler>exe</bundler>
    </configuration>
</plugin>

クラスパスを変更する

プラグインの設定で変更。「」に<bundleArguments/><classpath/>を追加すればOK

ただし、<bundleArguments>を追加する際は、併せて<mainJar>も追加する必要がある。

デフォルトでは、dependencyにあるライブラリへのクラスパスになっている。

以下は、クラスパスを「.」に変更するサンプル

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.2.0</version>
    <configuration>
        <mainClass>jp.sample.javafx.App</mainClass>
        <bundleArguments>
            <mainJar>javafx-sample-jfx.jar</mainJar>
            <classpath>.</classpath>
        </bundleArguments>
    </configuration>
</plugin>

ビルド時のログを出す。

プラグインの設定で変更。「」に<verbose>true</verbose>を追加すればOK

ビルド時のエラーなどがいろいろ見れるので、デバッグが捗る

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.2.0</version>
    <configuration>
        <mainClass>jp.sample.javafx.App</mainClass>
        <verbose>true</verbose>
    </configuration>
</plugin>

リソースファイルを追加する

プラグインの設定で変更。「」に<additionalAppResources/>を追加すればOK

指定したディレクトリの中身がすべて、作成されたJarと同じ場所に配置される。

プロパティファイルなど、jarに含めたくないファイルを扱うときに便利。

ただし、デフォルトのクラスパスには、「.」が含まれないので、クラスパスの変更も必要。

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.2.0</version>
    <configuration>
        <mainClass>jp.sample.javafx.App</mainClass>
        <additionalAppResources>${project.basedir}/additionalResources</additionalAppResources>
    </configuration>
</plugin>

プラグインで設定できる項目を調べる

jfx:list-bundlersゴールで調べることができる。ただし、プラグインのバージョン8.2.0以降

[INFO] --- javafx-maven-plugin:8.2.0:list-bundlers (default-cli) @ sound-commic-vierwer ---
[INFO] Available bundlers:
[INFO] -------------------
[INFO] ID: windows.app
[INFO] Name: Windows Application Image
[INFO] Description: A Directory based image of a windows Application with an optionally co-bundled JRE.  Used as a base for the Installer bundlers
[INFO] Available bundle arguments: 
[INFO]      Argument ID: name
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: App Name
[INFO]      Argument Description: The name of the application.
[INFO] 
[INFO]      Argument ID: appResources
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Resources
[INFO]      Argument Description: All of the files to place in the resources directory.  Including all needed jars as assets.
[INFO] 
[INFO]      Argument ID: arguments
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: Command Line Arguments
[INFO]      Argument Description: Command Line Arguments to be passed to the main class if no arguments are specified by the launcher.
[INFO] 
[INFO]      Argument ID: classpath
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Jar Classpath
[INFO]      Argument Description: The classpath from the main jar of the application, relative to the assembled application directory.
[INFO] 
[INFO]      Argument ID: icon.ico
[INFO]      Argument Type: java.io.File
[INFO]      Argument Name: .ico Icon
[INFO]      Argument Description: Icon for the application, in ICO format.
[INFO] 
[INFO]      Argument ID: jvmOptions
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: JVM Options
[INFO]      Argument Description: JVM flags and options to be passed in.
[INFO] 
[INFO]      Argument ID: jvmProperties
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: JVM System Properties
[INFO]      Argument Description: JVM System Properties (of the -Dname=value variety).
[INFO] 
[INFO]      Argument ID: applicationClass
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Class
[INFO]      Argument Description: The main class for the application.  Either a javafx.application.Application instance or a java class with a main method.
[INFO] 
[INFO]      Argument ID: mainJar
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Main Jar
[INFO]      Argument Description: The main jar of the application.  This jar should have the main-class, and is relative to the assembled application dir.
[INFO] 
[INFO]      Argument ID: preferencesID
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Preferences ID
[INFO]      Argument Description: The preferences node to search for User JVM Options.  The format be a slash delimited version of the main package name, such as "com/example/myapplication".
[INFO] 
[INFO]      Argument ID: preloader
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: JavaFX Preloader Class Name
[INFO]      Argument Description: For JavaFX applications only, this is the Fully Qualified Class Name of the preloader class.  This class needs to exist in the classpath, preferably early in the path.
[INFO] 
[INFO]      Argument ID: userJvmOptions
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: User JVM Options
[INFO]      Argument Description: JVM Options the user may override, along with their default values.
[INFO] 
[INFO]      Argument ID: appVersion
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Version
[INFO]      Argument Description: The version of this application.
[INFO] 
[INFO]      Argument ID: runtime
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: JRE
[INFO]      Argument Description: The Java Runtime to co-bundle.  The default value is the current JRE running the bundler.  A value of null will cause no JRE to be co-bundled and the system JRE will be used to launch the application.
[INFO] 
[INFO] -------------------
[INFO] ID: exe
[INFO] Name: EXEインストーラ
[INFO] Description: InnoIDEを使用したMicrosoft Windows EXEインストーラ。
[INFO] Available bundle arguments: 
[INFO]      Argument ID: name
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: App Name
[INFO]      Argument Description: The name of the application.
[INFO] 
[INFO]      Argument ID: appResources
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Resources
[INFO]      Argument Description: All of the files to place in the resources directory.  Including all needed jars as assets.
[INFO] 
[INFO]      Argument ID: arguments
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: Command Line Arguments
[INFO]      Argument Description: Command Line Arguments to be passed to the main class if no arguments are specified by the launcher.
[INFO] 
[INFO]      Argument ID: classpath
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Jar Classpath
[INFO]      Argument Description: The classpath from the main jar of the application, relative to the assembled application directory.
[INFO] 
[INFO]      Argument ID: icon.ico
[INFO]      Argument Type: java.io.File
[INFO]      Argument Name: .ico Icon
[INFO]      Argument Description: Icon for the application, in ICO format.
[INFO] 
[INFO]      Argument ID: jvmOptions
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: JVM Options
[INFO]      Argument Description: JVM flags and options to be passed in.
[INFO] 
[INFO]      Argument ID: jvmProperties
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: JVM System Properties
[INFO]      Argument Description: JVM System Properties (of the -Dname=value variety).
[INFO] 
[INFO]      Argument ID: applicationClass
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Class
[INFO]      Argument Description: The main class for the application.  Either a javafx.application.Application instance or a java class with a main method.
[INFO] 
[INFO]      Argument ID: mainJar
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Main Jar
[INFO]      Argument Description: The main jar of the application.  This jar should have the main-class, and is relative to the assembled application dir.
[INFO] 
[INFO]      Argument ID: preferencesID
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Preferences ID
[INFO]      Argument Description: The preferences node to search for User JVM Options.  The format be a slash delimited version of the main package name, such as "com/example/myapplication".
[INFO] 
[INFO]      Argument ID: preloader
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: JavaFX Preloader Class Name
[INFO]      Argument Description: For JavaFX applications only, this is the Fully Qualified Class Name of the preloader class.  This class needs to exist in the classpath, preferably early in the path.
[INFO] 
[INFO]      Argument ID: userJvmOptions
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: User JVM Options
[INFO]      Argument Description: JVM Options the user may override, along with their default values.
[INFO] 
[INFO]      Argument ID: appVersion
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Version
[INFO]      Argument Description: The version of this application.
[INFO] 
[INFO]      Argument ID: runtime
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: JRE
[INFO]      Argument Description: The Java Runtime to co-bundle.  The default value is the current JRE running the bundler.  A value of null will cause no JRE to be co-bundled and the system JRE will be used to launch the application.
[INFO] 
[INFO]      Argument ID: description
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Description
[INFO]      Argument Description: A longer description of the application
[INFO] 
[INFO]      Argument ID: copyright
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Copyright
[INFO]      Argument Description: The copyright for the application.
[INFO] 
[INFO]      Argument ID: licenseFile
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: License
[INFO]      Argument Description: The license file, relative to the assembled application directory.
[INFO] 
[INFO]      Argument ID: win.menuGroup
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Menu Group
[INFO]      Argument Description: The Start Menu group this application should be placed in
[INFO] 
[INFO]      Argument ID: menuHint
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Menu Hint
[INFO]      Argument Description: If the bundler can add the application to the system menu, should it?
[INFO] 
[INFO]      Argument ID: shortcutHint
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Shortcut Hint
[INFO]      Argument Description: If the bundler can create desktop shortcuts, should it make one?
[INFO] 
[INFO]      Argument ID: systemWide
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: System Wide
[INFO]      Argument Description: Should this application attempt to install itself system wide, or only for each user?  Null means use the system default.
[INFO] 
[INFO]      Argument ID: title
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Title
[INFO]      Argument Description: A title for the application.
[INFO] 
[INFO]      Argument ID: vendor
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Vendor
[INFO]      Argument Description: The vendor of the application.
[INFO] 
[INFO]      Argument ID: installdirChooser
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Install Directory Chooser
[INFO]      Argument Description: Adds a dialog to let the user choose a directory where the product will be installed.
[INFO] 
[INFO] -------------------
[INFO] ID: msi
[INFO] Name: MSIインストーラ
[INFO] Description: WiXを使用したMicrosoft Windows MSIインストーラ。
[INFO] Available bundle arguments: 
[INFO]      Argument ID: name
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: App Name
[INFO]      Argument Description: The name of the application.
[INFO] 
[INFO]      Argument ID: appResources
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Resources
[INFO]      Argument Description: All of the files to place in the resources directory.  Including all needed jars as assets.
[INFO] 
[INFO]      Argument ID: arguments
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: Command Line Arguments
[INFO]      Argument Description: Command Line Arguments to be passed to the main class if no arguments are specified by the launcher.
[INFO] 
[INFO]      Argument ID: classpath
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Jar Classpath
[INFO]      Argument Description: The classpath from the main jar of the application, relative to the assembled application directory.
[INFO] 
[INFO]      Argument ID: icon.ico
[INFO]      Argument Type: java.io.File
[INFO]      Argument Name: .ico Icon
[INFO]      Argument Description: Icon for the application, in ICO format.
[INFO] 
[INFO]      Argument ID: jvmOptions
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: JVM Options
[INFO]      Argument Description: JVM flags and options to be passed in.
[INFO] 
[INFO]      Argument ID: jvmProperties
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: JVM System Properties
[INFO]      Argument Description: JVM System Properties (of the -Dname=value variety).
[INFO] 
[INFO]      Argument ID: applicationClass
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Main Class
[INFO]      Argument Description: The main class for the application.  Either a javafx.application.Application instance or a java class with a main method.
[INFO] 
[INFO]      Argument ID: mainJar
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: Main Jar
[INFO]      Argument Description: The main jar of the application.  This jar should have the main-class, and is relative to the assembled application dir.
[INFO] 
[INFO]      Argument ID: preferencesID
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Preferences ID
[INFO]      Argument Description: The preferences node to search for User JVM Options.  The format be a slash delimited version of the main package name, such as "com/example/myapplication".
[INFO] 
[INFO]      Argument ID: preloader
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: JavaFX Preloader Class Name
[INFO]      Argument Description: For JavaFX applications only, this is the Fully Qualified Class Name of the preloader class.  This class needs to exist in the classpath, preferably early in the path.
[INFO] 
[INFO]      Argument ID: userJvmOptions
[INFO]      Argument Type: java.util.Map
[INFO]      Argument Name: User JVM Options
[INFO]      Argument Description: JVM Options the user may override, along with their default values.
[INFO] 
[INFO]      Argument ID: appVersion
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Version
[INFO]      Argument Description: The version of this application.
[INFO] 
[INFO]      Argument ID: runtime
[INFO]      Argument Type: com.oracle.tools.packager.RelativeFileSet
[INFO]      Argument Name: JRE
[INFO]      Argument Description: The Java Runtime to co-bundle.  The default value is the current JRE running the bundler.  A value of null will cause no JRE to be co-bundled and the system JRE will be used to launch the application.
[INFO] 
[INFO]      Argument ID: description
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Description
[INFO]      Argument Description: A longer description of the application
[INFO] 
[INFO]      Argument ID: win.menuGroup
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Menu Group
[INFO]      Argument Description: The Start Menu group this application should be placed in
[INFO] 
[INFO]      Argument ID: menuHint
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Menu Hint
[INFO]      Argument Description: If the bundler can add the application to the system menu, should it?
[INFO] 
[INFO]      Argument ID: win.msi.productVersion
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: 製品バージョン
[INFO]      Argument Description: WindowsおよびMSIに表示されるアプリケーションのバージョン(形式は"1.2.3")
[INFO] 
[INFO]      Argument ID: shortcutHint
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Shortcut Hint
[INFO]      Argument Description: If the bundler can create desktop shortcuts, should it make one?
[INFO] 
[INFO]      Argument ID: systemWide
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: System Wide
[INFO]      Argument Description: Should this application attempt to install itself system wide, or only for each user?  Null means use the system default.
[INFO] 
[INFO]      Argument ID: vendor
[INFO]      Argument Type: java.lang.String
[INFO]      Argument Name: Vendor
[INFO]      Argument Description: The vendor of the application.
[INFO] 
[INFO]      Argument ID: licenseFile
[INFO]      Argument Type: java.util.List
[INFO]      Argument Name: License
[INFO]      Argument Description: The license file, relative to the assembled application directory.
[INFO] 
[INFO]      Argument ID: installdirChooser
[INFO]      Argument Type: java.lang.Boolean
[INFO]      Argument Name: Install Directory Chooser
[INFO]      Argument Description: Adds a dialog to let the user choose a directory where the product will be installed.
[INFO] 
[INFO] -------------------
[INFO] ID: jnlp
[INFO] Name: WebStart JNLP Bundler
[INFO] Description: Creates JNLP files and directory layout for WebStart invocation of the application.
[INFO] -------------------

参考にしたサイト様