0
0

必要なJREをだけを抽出しEXE化するPOM

Last updated at Posted at 2024-07-29

必要なJREをだけを抽出しEXE化するPOM

  1. JREを含む実行可能なJARファイルの作成
  2. Windows用のEXEファイルの生成
  3. カスタムJREの作成と配布用パッケージへの組み込み

これらの目標を達成するために、Maven構成(POM)を適切に設定し、必要なプラグインを使用します。

以下が完全なPOMファイルです:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>csv-to-sqlite-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- SQLite JDBC Driver -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.34.0</version>
        </dependency>
        <!-- OpenCSV Library -->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.5.2</version>
        </dependency>
        <!-- JUnit for testing (optional) -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-jre</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/jre</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/jre</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <jar>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
                            <outfile>${project.build.directory}/${project.artifactId}.exe</outfile>
                            <classPath>
                                <mainClass>com.example.Main</mainClass>
                            </classPath>
                            <jre>
                                <path>jre</path>
                                <bundledJre64Bit>true</bundledJre64Bit>
                                <bundledJreAsFallback>false</bundledJreAsFallback>
                                <minVersion>17</minVersion>
                                <jdkPreference>jreOnly</jdkPreference>
                                <runtimeBits>64</runtimeBits>
                            </jre>
                            <dontWrapJar>false</dontWrapJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

詳細な説明:

1. プロジェクト設定:

  • Java 17を使用するように設定されています。
  • 必要な依存関係(SQLite JDBC、OpenCSV、JUnit)が定義されています。

2. maven-compiler-plugin:

  • Javaソースとターゲットバージョンを17に設定しています。

3. maven-assembly-plugin:

  • 全ての依存関係を含む単一の実行可能JARファイルを作成します。
  • jar-with-dependencies デスクリプタを使用して、依存関係を含めます。

4. launch4j-maven-plugin:

  • JARファイルをWindows実行可能ファイル(EXE)に変換します。
  • カスタムJREへのパスを指定しています(<path>custom-jre</path>)。
  • 64ビットJREを使用するように設定されています。
  • アプリケーションのバージョン情報を設定しています。

5. maven-resources-plugin:

  • カスタムJREをtargetディレクトリにコピーします。
  • これにより、EXEファイルと一緒にカスタムJREを配布できます。

カスタムJREの作成(POMファイル外の作業):

  1. jlink コマンドを使用して、必要最小限のモジュールのみを含むカスタムJREを作成します。
  2. 作成したカスタムJREをプロジェクトの custom-jre ディレクトリに配置します。

ビルドプロセス:

  1. Maven's package フェーズが実行されると、依存関係を含むJARファイルが作成されます。
  2. 次に、launch4jプラグインがこのJARファイルをEXEに変換します。
  3. 最後に、カスタムJREがtargetディレクトリにコピーされます。

結果:

  • target ディレクトリに、EXEファイルとカスタムJREが生成されます。
  • これらを一緒に配布することで、Javaがインストールされていない環境でもアプリケーションを実行できます。

この設定により、必要最小限のJREコンポーネントのみを含む、コンパクトでスタンドアロンのWindowsアプリケーションを作成できます。

JRE作成からEXEファイル作成までの手順書

1. カスタムJREの作成

1.1. バッチファイルの準備

  • プロジェクトのルートディレクトリに create_custom_jre.bat という名前で以下の内容のバッチファイルを作成します:
@echo off
REM Check if the jre directory exists and delete it
if exist target\jre (
 rmdir /s /q target\jre
)
REM Create custom JRE
"C:\Program Files\Java\jdk-17\bin\jlink.exe" --module-path "C:\Program Files\Java\jdk-17\jmods" --add-modules java.base,java.desktop,java.sql --output target\jre

1.2. 外部ツールの設定

  • エクリプスで、「Run」→「External Tools」→「External Tools Configurations」を選択します。
  • 「Program」を右クリックし、「New Configuration」を選択します。
  • 以下のように設定します:
    • Name: Create Custom JRE
    • Location: ${workspace_loc:/csv-to-sqlite-app/create_custom_jre.bat}
    • Working Directory: ${workspace_loc:/csv-to-sqlite-app}
    • Arguments: --module-path "C:\Program Files\Java\jdk-17\jmods" --add-modules java.base,java.desktop,java.sql --output ${workspace_loc:/csv-to-sqlite-app/jre/}

1.3. カスタムJREの作成

  • 設定した外部ツールを実行します(「Run」ボタンをクリック)。
  • コンソールにJREの作成過程が表示され、完了するとプロジェクト内に jre フォルダが作成されます。

2. POMファイルの設定

2.1. POMファイルを開き、以下の内容に更新または追加します:

<build>
    <plugins>
        <!-- 他のプラグイン設定 -->
        <plugin>
            <groupId>com.akathist.maven.plugins.launch4j</groupId>
            <artifactId>launch4j-maven-plugin</artifactId>
            <version>2.1.2</version>
            <executions>
                <execution>
                    <id>l4j-clui</id>
                    <phase>package</phase>
                    <goals>
                        <goal>launch4j</goal>
                    </goals>
                    <configuration>
                        <headerType>console</headerType>
                        <jar>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
                        <outfile>${project.build.directory}/${project.artifactId}.exe</outfile>
                        <classPath>
                            <mainClass>com.example.Main</mainClass>
                        </classPath>
                        <jre>
                            <path>jre</path>
                            <bundledJre64Bit>true</bundledJre64Bit>
                            <bundledJreAsFallback>false</bundledJreAsFallback>
                            <minVersion>17</minVersion>
                            <jdkPreference>jreOnly</jdkPreference>
                            <runtimeBits>64</runtimeBits>
                        </jre>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>copy-custom-jre</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/jre</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/jre</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. EXEファイルの作成

3.1. Mavenビルドの実行

  • プロジェクトを右クリックし、「Run As」→「Maven build...」を選択します。
  • Goals に clean package と入力します。
  • 「Run」ボタンをクリックしてビルドを開始します。

3.2. 生成されたファイルの確認

  • ビルドが成功すると、target フォルダ内に以下のファイルが生成されます:
    • ${project.artifactId}.exe(Windows用実行ファイル)
    • jre フォルダ(カスタムJRE)

4. 配布用パッケージの作成

4.1. 以下のファイルとフォルダを配布用フォルダにコピーします:

  • target/${project.artifactId}.exe
  • target/jre フォルダ

4.2. 必要に応じて、設定ファイルやリソースファイルも配布用フォルダにコピーします。

5. テストと確認

5.1. 配布用フォルダ内のEXEファイルをダブルクリックして実行し、正常に動作することを確認します。

5.2. 別のWindows環境(Javaがインストールされていない環境)でも動作することを確認します。

この手順書に従うことで、エクリプス上でカスタムJREを含むWindows用の実行可能ファイル(EXE)を作成し、配布用パッケージを準備することができます。必要に応じて、プロジェクトの具体的な要件に合わせてPOMファイルやバッチファイルの内容を調整してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0