トッカンソフトウェア

Maven Jar作成

今回は、MavenでJarファイルを作ってみます。
warファイルの作り方は こちらをご参照下さい。


ライブラリを含むJarの作成

JarファイルのなかにライブラリのJarファイルを含み、一つのJarだけで実行できるJarを作成します。

Eclipseを起動し、File→New→Maven Projectを選択。


Create Simple Projectにチェックが付いていることを確認してNext。


Group Id、Artifact Idを入力し、Packagingでjarを選択してNext。


プロジェクトが生成されるので、pom.xmlを編集します。

propertiesの中でJavaのバージョンを指定します。
maven-assembly-pluginでJarファイルを含むJarファイルを作成できます。

C:\eclipseee\workspace\MvnJar\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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>MvnJar</groupId>
	<artifactId>MvnJar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<java.version>1.8</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.3.0</version>
				<configuration>
					<finalName>${project.artifactId}-${project.version}_Fat</finalName>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>test.MvnTest</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>
	</dependencies>
</project>

			
テスト用ソースを作成

C:\eclipseee\workspace\MvnJar\src\main\java\test\MvnTest.java
				
package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MvnTest {

	static Logger logger = LoggerFactory.getLogger(MvnTest.class);

	public static void main(String[] args) {
		logger.debug("Hello World");
	}

}

			

pom.xmlを修正すると以下のエラーがでることがあります。

エラーメッセージ
				
Project configuration is not up-to-date with pom.xml. 
Select: Maven->Update Project... from the project context menu or use Quick Fix.	MvnJar

			
このエラーがでたら、プロジェクトを右クリックしてMaven→UpdatePrjectを選択。

対象プロジェクトを選択してOKボタンを押します。


エラーがなくなったらMavenでJarを作成します。

プロジェクトを右クリックしてRun As→Maven build...を選択します。


Goalsにpackageを入力してRunボタンを押します。

targetの下にJarが出来るので、実行してみます。
				
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
set PATH=%JAVA_HOME%\bin;%PATH%

cd C:\eclipseee\workspace\MvnJar\target
java -jar MvnJar-0.0.1-SNAPSHOT_Fat.jar

			

ライブラリの参照を含むJarの作成

ライブラリのJarは、Jarの中に含めず相対パスのクラスパス設定を持つJarを作成します。

maven-dependency-pluginで使用するJarを指定フォルダに集めます(今回はlibs)。
maven-jar-pluginで実行可能Jarを作成し、クラスパスの設定をします。
				
<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>MvnJar</groupId>
	<artifactId>MvnJar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<java.version>1.8</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.3.0</version>
				<configuration>
					<finalName>${project.artifactId}-${project.version}_Fat</finalName>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>test.MvnTest</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.2</version>
				<executions>
					<execution>
						<id>copy</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/libs</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<finalName>${project.artifactId}-${project.version}_Thin</finalName>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>libs/</classpathPrefix>
							<mainClass>test.MvnTest</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>
	</dependencies>
</project>

			

Jarの作成方法は同じです。targetの下に出来たJarを実行できます。
				
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
set PATH=%JAVA_HOME%\bin;%PATH%

cd C:\eclipseee\workspace\MvnJar\target
java -jar MvnJar-0.0.1-SNAPSHOT_Thin.jar

			

Jarファイルの中のMANIFEST.MFは以下のようになりクラスパスが設定されています。
				
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: aaa
Class-Path: libs/logback-classic-1.2.3.jar libs/logback-core-1.2.3.jar
  libs/slf4j-api-1.7.25.jar
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_73
Main-Class: test.MvnTest


			



ページのトップへ戻る