Mavenを使用してすべての依存関係を含むJavaプロジェクトをビルドするにはどうすればよいですか? maven-resources、maven-dependencyおよびmaven-jarプラグイン
公開: 2016-11-26
エンタープライズレベルのJavaプロジェクトに取り組んでいますか? Maven POM.xml
ファイルを使用して、すべての依存関係を最新の状態に保ちますか? プロジェクトには、 src folder
、 resources folder
、 lib folder
などがありますか? さて、このプロジェクトをサードパーティのクライアントに展開したい場合はどうでしょうか。 他のスタンドアロンハードウェアはありますか?
そうですね、Mavenプラグインを使用してJavaプロジェクトの実行可能ファイルをビルドおよび作成する簡単な方法があります。 以下のサンプルJavaプロジェクトをご覧ください。

始めて、プロジェクトのすべての部分について説明しましょう。
-
CrunchifyMavenBuildPlugins
はMavenプロジェクトです。 Javaプロジェクトがあり、それをMavenプロジェクトに変換したい場合は、このチュートリアルに従ってください。 - 2つのフォルダがあります。
src
とresources.
-
resources
フォルダー内には、1つの実行可能シェルスクリプトファイルを含むScripts
というフォルダーがあります。 -
CrunchifyMain.java
は、main(String args[])
メソッドを内部に持つ主要な開始点です。 - 含まれているすべての依存関係を含む実行可能.jarプロジェクトをビルドするMavenプラグインを追加する
pom.xml
ファイル。
ステップ1
pom.xml
ファイルを開き、以下を<build>
の下に追加します。 注:以下に3つのプラグインを追加しました。
-
maven-resources-plugin
:Resources Pluginは、プロジェクトリソースの出力ディレクトリへのコピーを処理します。 主なリソースは、主なソースコードに関連付けられているリソースです。 -
maven-dependency-plugin:
依存関係プラグインはアーティファクトを操作する機能を提供します。 ローカルまたはリモートのリポジトリから指定された場所にアーティファクトをコピーおよび/または解凍できます。 -
maven-jar-plugin:
このプラグインは、jarをビルドして署名する機能を提供します。
これが完全な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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
< 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 > 2.15.0 < / 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 > < artifactId > maven - resources - plugin < / artifactId > < version > 2.6 < / version > < executions > < execution > < id > copy - resources < / id > < phase > validate < / phase > < goals > < goal > copy - resources < / goal > < / goals > < configuration > < outputDirectory > $ { basedir } / target / Crunchify < / outputDirectory > < resources > < resource > < directory > resources < / directory > < filtering > true < / filtering > < / resource > < / resources > < / configuration > < / execution > < / executions > < / plugin > < plugin > < groupId > org . apache . maven . plugins < / groupId > < artifactId > maven - dependency - plugin < / artifactId > < executions > < execution > < id > copy - dependencies < / id > < phase > prepare - package < / phase > < goals > < goal > copy - dependencies < / goal > < / goals > < configuration > < outputDirectory > $ { project . build . directory } / Crunchify / 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 > < configuration > < archive > < manifest > < addClasspath > true < / addClasspath > < classpathPrefix > lib / < / classpathPrefix > < mainClass > com . crunchify . tutorial . CrunchifyMain < / mainClass > < / manifest > < manifestEntries > < Class - Path > . < / Class - Path > < / manifestEntries > < / archive > < finalName > Crunchify / Crunchify < / finalName > < / configuration > < / plugin > < / plugins > < / build > < / project > |
ステップ2
Project -> Run As -> Maven Build
を右クリックします。


ステップ-3
引数「 clean install
」を提供します

ステップ-4
このような結果が表示されるはずです。

ステップ-5
次に、フォルダフォルダ/target/Crunchify
をチェックして、その下のすべてをチェックします。

ステップ-6
次に、以下のコマンド$bash> java -jar Crunchify.jar
を使用してプロジェクトを実行します。
プロジェクトの構築に問題がある場合はお知らせください。 楽しんでハッピーコーディング。