0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Maven 実行可能 jar ファイルを tar.gz にパッケージング

Last updated at Posted at 2018-11-17

1. Manifest に Main クラスと Class パスを定義する設定

maven-jar-plugin を使う

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.blueskyarea.Main</mainClass>							 
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib</classpathPrefix>
            </manifest>
            <manifestEntries>
                <Class-Path>etc/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
  • manifest.mainClass で メインクラスを指定
  • manifest.addClasspath を true にすることで、依存ライブラリをクラスパスに追加
  • 依存ライブラリは lib ディレクトリに入れる予定なので、manifest.classpathPrefix に lib を指定
  • プロパティファイルを etc ディレクトリに入れる予定なので、manifestEntries.Class-Path に etc/ を指定(/ が必要)

2. tar.gz にパッケージングする設定

maven-assembly-plugin を使う

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptor>src/main/assembly/bin.xml</descriptor>
        <finalName>${project.artifactId}-${project.version}</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

(追記)maven3.3.0 では、descriptor を descriptors で囲む

maven3.3.0
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/bin.xml</descriptor>
        </descriptors>
        <finalName>${project.artifactId}-${project.version}</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • descriptor を別ファイル(bin.xml)で指定

3. descriptor の設定

bin.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
	<id>bin</id>
	<formats>
		<format>tar.gz</format>
	</formats>
	<fileSets>
		<fileSet>
			<directory>src/main/config</directory>
			<outputDirectory>etc</outputDirectory>
			<includes>
				<include>*.properties</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>target</directory>
			<outputDirectory></outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<unpack>false</unpack>
			<scope>runtime</scope>
			<outputDirectory>lib</outputDirectory>
		</dependencySet>
	</dependencySets>
</assembly>
  • fileSet にて、src/main/config に含まれる *.properties を etc にコピーするように指定
  • fileSet にて、target に含まれる *.jar を tar.gz にコピーするように指定
  • dependencySet にて、依存ライブラリを lib にコピーするように指定

4. パッケージングして実行してみる

tar.gz ファイル生成

$ mvn clean package
$ tree
└── target
    ├── sample-project-bin.tar.gz

tar.gz ファイル解凍

$ tar zxvf target/sample-project-1.0-SNAPSHOT-bin.tar.gz
$ tree
├── sample-project-1.0-SNAPSHOT
│   ├── etc
│   │   └── sample-project.properties
│   ├── sample-project-SNAPSHOT.jar
│   ├── lib
│   │   ├── commons-configuration-1.10.jar
│   │   ├── commons-lang-2.6.jar

以下のようにパッケージングされていたことがわかる。

  • ディレクトリ直下に jar ファイル
  • lib 以下に依存ライブラリ
  • etc 以下にプロパティファイル

Manifest ファイルの中身

$ jar -xvf sample-project-1.0-SNAPSHOT/sample-project-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF
$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Built-By: mh
Class-Path: lib/commons-configuration-1.10.jar lib/commons-lang-2.6.ja
 r lib/commons-logging-1.1.1.jar lib/jetty-server-9.4.12.v20180830.jar
  lib/javax.servlet-api-3.1.0.jar lib/jetty-http-9.4.12.v20180830.jar 
 lib/jetty-util-9.4.12.v20180830.jar lib/jetty-io-9.4.12.v20180830.jar
  lib/jetty-webapp-9.4.12.v20180830.jar lib/jetty-xml-9.4.12.v20180830
 .jar lib/jetty-servlet-9.4.12.v20180830.jar lib/jetty-security-9.4.12
 .v20180830.jar lib/gson-2.2.4.jar etc/
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_144
Main-Class: com.blueskyarea.Main

以下が定義されていることがわかる。

  • Main-Class にメインクラスのパッケージ
  • Class-Path に依存ライブラリとプロパティファイルへのパス

jar 実行

$ java -jar sample-project-1.0-SNAPSHOT/sample-project-1.0-SNAPSHOT.jar
2018-11-17 12:52:49.579:INFO::main: Logging initialized @482ms to org.eclipse.jetty.util.log.StdErrLog
2018-11-17 12:52:49.775:INFO:oejs.Server:main: jetty-9.4.12.v20180830; built: 2018-08-30T13:59:14.071Z; git: 27208684755d94a92186989f695db2d7b21ebc51; jvm 1.8.0_144-b01
2018-11-17 12:52:49.889:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2018-11-17 12:52:49.889:INFO:oejs.session:main: No SessionScavenger set, using defaults
2018-11-17 12:52:49.896:INFO:oejs.session:main: node0 Scavenging every 600000ms
2018-11-17 12:52:49.918:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@27efef64{/,null,AVAILABLE}
2018-11-17 12:52:49.952:INFO:oejs.AbstractConnector:main: Started ServerConnector@1ef400fa{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2018-11-17 12:52:49.953:INFO:oejs.Server:main: Started @882ms

-jar で jar ファイルを指定するだけで実行可能。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?