「maven-shade-plugin」を使用して、JavaまたはSpringプロジェクトに必要なすべての依存関係を含む実行可能jarを1つだけ作成しますか?
公開: 2016-11-16すべての依存関係を含む1つの
Crunchify.jar
ファイルを作成するだけです
先週、 maven-resources-plugin
、 maven-dependency-plugin
、 maven-jar-plugin
を使用して完全な実行可能なJarプロジェクトを生成する方法についてのチュートリアルを書きましたか? その結果、必要なすべてのファイルを作成/コピーして/target/Crunchify
フォルダーに保存します。
Before:
maven-resource、maven-dependency、maven-jarプラグインの使用:
After:
maven-shadeプラグインの使用:
上記の結果を達成する方法を始めましょう:
ご覧のとおり、Java Projectのすべてをjust one file
にバンドルし、 Crunchify.jar
という名前を付けました(すべてのファイルが含まれています)。
数日前、Mavenを使用しrunnable binary distribution of a standalone Java application
を作成したかったので、それを実現する方法がわかりませんでした。 言い換えると:
- MavenでSpringベースの実行可能jarを作成するにはどうすればよいですか?
- Maven for Java + Spring +その他の依存関係を使用した実行可能なバイナリディストリビューションの作成
- MavenでJava + Springプロジェクトをビルドする方法は?
通常、Mavenでは、依存関係の管理に依存しています。 アーティファクトには、それ自体のクラス/リソースが含まれているだけです。 Mavenは、コンパイル時や実行時などに、プロジェクトが依存しているすべてのアーティファクト(JARなど)を見つける責任があります。
maven -shade- plugin
の助けを借りて、私はこの目標を達成することができました。 アーティファクトをuber-jar (One major file jar)
にパッケージ化し、その依存関係を含め、一部の依存関係のパッケージをシェーディング(つまり名前変更)する機能を提供します。
私のプロジェクトには、Spring Framework、JSON、サードパーティツールなど、他にも多くの依存関係があります。以下はサンプルプロジェクトですが、本番環境の.jarファイルのサイズは~5.2MB big
になりました。
uber-jar
は、すべての依存関係を取得し、依存関係のコンテンツを抽出して、プロジェクト自体のクラス/リソースとともに1つのJARに配置するものです。 このようなuber-jarを使用すると、アプリを実行するために必要な小さなJARが大量にあるのではなく、大きなJARが1つだけ必要になるためeasy for execution
なります。 また、場合によっては配布が容易になります。
これを実現するには、pom.xmlファイルを更新する必要があります。
ステップ1
完全なpom.xml
ファイルを見てください。 主に<build>
タグに興味があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
< 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 > CrunchifyMavenBuildPlugins < / groupId > < artifactId > CrunchifyMavenBuildPlugins < / artifactId > < version > 0.0.1 - SNAPSHOT < / version > < name > CrunchifyMavenBuildPlugins < / name > < properties > < project . build . sourceEncoding > UTF - 8 < / project . build . sourceEncoding > < / properties > < dependencies > < dependency > < groupId > log4j < / groupId > < artifactId > log4j < / artifactId > < version > 1.2.17 < / version > < / dependency > < dependency > < groupId > com . googlecode . json - simple < / groupId > < artifactId > json - simple < / artifactId > < version > 1.1 < / version > < / dependency > < dependency > < groupId > axis < / groupId > < artifactId > axis < / artifactId > < version > 1.4 < / version > < / dependency > < dependency > < groupId > commons - beanutils < / groupId > < artifactId > commons - beanutils < / artifactId > < version > 1.8.3 < / version > < / dependency > < dependency > < groupId > commons - collections < / groupId > < artifactId > commons - collections < / artifactId > < version > 3.2.1 < / version > < / dependency > < dependency > < groupId > commons - configuration < / groupId > < artifactId > commons - configuration < / artifactId > < version > 1.10 < / version > < / dependency > < dependency > < groupId > commons - io < / groupId > < artifactId > commons - io < / artifactId > < version > 2.4 < / version > < / dependency > < dependency > < groupId > javax . mail < / groupId > < artifactId > mail < / artifactId > < version > 1.4.7 < / version > < / dependency > < dependency > < groupId > javax . servlet < / groupId > < artifactId > servlet - api < / artifactId > < version > 2.5 < / version > < / dependency > < dependency > < groupId > org . json < / groupId > < artifactId > json < / artifactId > < version > 20140107 < / version > < / dependency > < dependency > < groupId > axis < / groupId > < artifactId > axis - saaj < / artifactId > < version > 1.4 < / version > < / dependency > < dependency > < groupId > wsdl4j < / groupId > < artifactId > wsdl4j < / artifactId > < version > 1.6.3 < / version > < / dependency > < dependency > < groupId > com . google . zxing < / groupId > < artifactId > core < / artifactId > < version > 2.0 < / version > < / dependency > < / dependencies > < build > < pluginManagement > < plugins > < plugin > < groupId > org . apache . maven . plugins < / groupId > < artifactId > maven - compiler - plugin < / artifactId > < version > 2.3.1 < / version > < configuration > < source > 1.7 < / source > < target > 1.7 < / target > < / configuration > < / plugin > < / plugins > < / pluginManagement > < plugins > < plugin > < groupId > org . apache . maven . plugins < / groupId > < artifactId > maven - shade - plugin < / artifactId > < version > 1.7 < / version > < executions > < execution > < phase > package < / phase > < goals > < goal > shade < / goal > < / goals > < configuration > < ! -- Optional Start -- > < finalName > Crunchify < / finalName > < shadedArtifactAttached > true < / shadedArtifactAttached > < shadedClassifierName > jar - with - dependencies < / shadedClassifierName > < ! -- Optional End -- > < transformers > < transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > < mainClass > com . crunchify . tutorial . CrunchifyMain < / mainClass > < / transformer > < transformer implementation = "org.apache.maven.plugins.shade.resource.AppendingTransformer" > < resource > META - INF / spring . handlers < / resource > < / transformer > < transformer implementation = "org.apache.maven.plugins.shade.resource.AppendingTransformer" > < resource > META - INF / spring . schemas < / resource > < / transformer > < transformer implementation = "org.apache.maven.plugins.shade.resource.AppendingTransformer" > < resource > META - INF / spring . tooling < / resource > < / transformer > < / transformers > < / configuration > < / execution > < / executions > < / plugin > < / plugins > < / build > < / project > |
ご覧のとおり、pom.xmlファイルでResource Transformers
を使用しています。 複数のアーティファクトからのclasses/resources
を1つのCrunchify JAR
に集約することは、重複がない限り簡単です。 それ以外の場合は、複数のJARからのリソースをマージするための何らかのロジックが必要です。 これは、リソーストランスフォーマーが始まるところです。

ステップ2
maven-shade-plugin
をpom.xml
ファイルに追加したら、以下のコマンドを使用してプロジェクトをビルドします。
1 |
mvn clean install |
そしてall dependencies mentioned in pom.xml file
作成された1つのCrunchify.jar
(uber-jar)ファイルが表示されます。
ステップ-3
プロジェクトのターゲットフォルダーに移動すると、Crunchify.jarファイルが作成されます。 コマンドを使用するだけ
$bash> java -jar Crunchify.jar
を使用して、プロジェクトを実行します。
追加するものはありますか? 以下のコメントセクションに追加してください。 楽しんで、素晴らしい一日を過ごして、ハッピーコーディングを。