[A-00222] 実行可能jar(Executable jar)の作成方法

実行可能なjarファイルを作る機会があったのでメモ

主にmavenを使用してexecutable jarを作る方法について記載してます。

mavenを使用すればクラスパスオプションを指定することなく実行ができ、大変楽なのでおすすめです。

・プラグインを導入する

下記のプラグインをpom.xmlに追加することで簡単に作れるようです。

maven-dependency-plugin,maven-jar-pluginが必須です。

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>executable-jar-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>executable-jar-example</name>

	<properties>
		<java.version>21</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.17.0</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.8.1</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.4.2</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.example.demo.Main</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

ソースコードは下記の通り

package com.example.demo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList(args);
        list.forEach((element) -> {
            Path file = Paths.get(StringUtils.trim(element));
            try {
                String content = Files.readString(file);
                System.out.println(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}

実行シェルは下記の通り

#!/bin/bash

java -jar target/executable-jar-example-0.0.1-SNAPSHOT.jar ./demo.txt

読み込みファイルは下記の通り

AAAA-1111
BBBB-1111
CCCC-1111

下記のコマンドで結果を確認

mvn clean install
./boot.sh

・Appendix

参考文献はこちら

https://maven.apache.org/shared/maven-archiver/examples/classpath.html

https://www.glamenv-septzen.net/view/1121#ida3bee8

https://qiita.com/blonz3977/items/b632fb2c7df6ae13689b

https://madhawagunasekara.blogspot.com/2014/10/solving-javalangnoclassdeffounderror-in.html

https://stackoverflow.com/questions/10568275/noclassdeffounderror-on-maven-dependency

https://github.com/msakamoto-sf/jar-deployment-examples

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*