Mavenを使ってjarファイルを作成する方法

このチュートリアルでは、Mavenビルドツールを使用して単一の実行可能ファイルJarを作成する方法と、プロジェクトの依存関係を処理する方法を説明します。
使用されるツール:
-
Maven 3.1.1
-
JDK 1.7
-
log4j 1.2.17
-
Joda-time 2.5
-
Eclipse 4.3
1.単純なJavaプロジェクトを作成する
MavenクイックスタートテンプレートからJavaプロジェクトを作成します。
$ mvn archetype:generate -DgroupId=com.mkyong.core.utils -DartifactId=dateUtils -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
以下のファイルとフォルダ構造が作成されます。
. |________dateUtils | |________pom.xml | |________src | | |________main | | | |________java | | | | |________com | | | | | |________mkyong | | | | | | |________core | | | | | | | |________utils | | | | | | | | |________App.java | | |________test | | | |________java | | | | |________com | | | | | |________mkyong | | | | | | |________core | | | | | | | |________utils | | | | | | | | |________AppTest.java
上記のフォルダ構造だけでは不十分です。
log4j.properties`ファイルを作成し、
src/main/resources/log4j.properties`に入れてください。リソースフォルダを手動で作成してください。
log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Eclipseに対応してください。
$ mvn eclipse:eclipse
Eclipse IDEにプロジェクトをインポートすると、最終的なプロジェクト構造は次のようになります。

2. Pom.xmlを更新する
pom.xml`を更新してlog4jとjodatimeの依存関係を宣言し、出力を
jar`形式にするには、パッケージが “jar”に設定されていることを確認してください。
コメントを読んで自明です。
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
http://maven.apache.org/maven-v4__0__0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.core.utils</groupId>
<artifactId>dateUtils</artifactId>
<!-- Output to jar format -->
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dateUtils</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<jodatime.version>2.5</jodatime.version>
<junit.version>4.11</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>dateutils</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a JDK compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- DO NOT include log4j.properties file in your Jar -->
<excludes>
<exclude>** ** /log4j.properties</exclude>
</excludes>
<archive>
<manifest>
<!-- Jar file entry point -->
<mainClass>com.mkyong.core.utils.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. App.javaを更新する
生成された `App.java`を以下の内容で更新してください:
App.java
package com.mkyong.core.utils;
import org.apache.log4j.Logger;
import org.joda.time.LocalDate;
public class App {
private static final Logger logger = Logger.getLogger(App.class);
public static void main(String[]args) {
System.out.println(getLocalCurrentDate());
}
private static String getLocalCurrentDate() {
if (logger.isDebugEnabled()) {
logger.debug("getLocalCurrentDate() is executed!");
}
LocalDate date = new LocalDate();
return date.toString();
}
}
今、このプロジェクトにはlog4jとjodatimeという2つの依存関係があります。
4.依存関係を扱う
__4.1。 jarファイルに依存関係を追加するにはどうしたらいいですか?
4.2
ソリューション
ワンジャーのソリューションは本当に良いですが、私はカスタムクラスローダーとファットジャーの概念が嫌いです。私の最も簡単で常用のソリューションは、プロジェクト全体の依存関係をあらかじめ定義されたフォルダにコピーし、jarのマニフェストファイルに依存クラスパスを定義することです。
下のは
maven-dependency-plugin`を使ってすべての依存関係を
target/dependency-jars/
フォルダにコピーし、
maven-jar-plugin`を使って依存関係クラスパスを追加する
pom.xml
。
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
http://maven.apache.org/maven-v4__0__0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.core.utils</groupId>
<artifactId>dateUtils</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dateUtils</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<jodatime.version>2.5</jodatime.version>
<junit.version>4.11</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>dateutils</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>** ** /log4j.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mkyong.core.utils.App</mainClass>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Copy project dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5.最後のJarファイル
5.1プロジェクトをパッケージ化する。
$ mvn package
ターゲットフォルダのフォルダ構造を確認する

dateutils.jar`が作成され、プロジェクトランタイムの依存関係(除外されたjunit)が
target/dependency-jars/`フォルダにコピーされます。
5.2 `dateutils.jar`の内容をリストします:
$ jar tf target/dateutils.jar META-INF/META-INF/MANIFEST.MF com/com/mkyong/com/mkyong/core/com/mkyong/core/utils/com/mkyong/core/utils/App.class META-INF/maven/META-INF/maven/com.mkyong.core.utils/META-INF/maven/com.mkyong.core.utils/dateUtils/META-INF/maven/com.mkyong.core.utils/dateUtils/pom.xml META-INF/maven/com.mkyong.core.utils/dateUtils/pom.properties
5.3
MANIFEST.MF`の内容を抽出してレビューします。依存関係は
Class-Path`に追加されます。
META INF/MANIFEST.MF
Manifest-Version: 1.0 Built-By: mkyong Build-Jdk: 1.7.0__05 Class-Path: dependency-jars/joda-time-2.5.jar dependency-jars/log4j-1.2.17.jar Created-By: Apache Maven 3.1.1 Main-Class: com.mkyong.core.utils.App Archiver-Version: Plexus Archiver
5.4実行する
$ java -jar target/dateutils.jar log4j:WARN No appenders could be found for logger (com.mkyong.core.utils.App). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 2014-10-19
Oppss …
5.5
log4j.properties`はどこにありますか? jarファイルの `log4j.properties`を除外し、クラスパスに複数の
log4j.properties`ファイルのような問題を避けることは、良い習慣です。
あなたは `log4j.configuration`システムプロパティを使って以下のようにlog4jプロパティを渡すことができます:
$ java -jar -Dlog4j.configuration=file:/full__path/log4j.properties target/dateutils.jar 17:09:15,385 DEBUG App:18 - getLocalCurrentDate() is executed! 2014-10-19
-
Note ** +将来、
dateUtils.jar`を移動する場合は、
dependency-jars`フォルダも必ずコピーしてください。あなたが良いアイデアを持っているなら、感謝してください。
完了しました。
ソースコードをダウンロードする
ダウンロード:
maven-create-a-jar.zip
(7 KB)
参考文献
公式ページ。]。 link://java/実行可能ファイル作成の手引き/[作成する方法
実行可能なjarファイル]。
One-JAR
-
リンク://maven/maven-exclude-log4j-jar-properties-inファイル/[Maven –
Jarファイル内のlog4j.propertiesを除外する]。リンク://maven/maven-create-a-fat-jar-file-one-jar-example/[Maven-
ファットJarファイルを作成する – 1つのJARの例]