Come creare un progetto Java che includa tutte le dipendenze usando Maven? Plugin maven-resources, maven-dependency e maven-jar
Pubblicato: 2016-11-26
Stai lavorando su un progetto Java a livello aziendale? Utilizzi il file Maven POM.xml
per mantenere aggiornate tutte le dipendenze? Nel tuo progetto hai la src folder
resources folder
lib folder
, ecc? Bene, cosa succede se si desidera distribuire questo progetto su client di terze parti? Qualche altro hardware autonomo?
Bene, c'è un modo semplice per costruire e creare l'eseguibile del tuo progetto Java con Maven Plugins. Dai un'occhiata al seguente esempio di Java Project.

Iniziamo e lascia che ti spieghi tutte le parti del progetto:
-
CrunchifyMavenBuildPlugins
è un progetto Maven. Se hai un progetto Java e desideri convertirlo in un progetto Maven, segui questo tutorial. - Abbiamo due cartelle.
src
eresources.
- All'interno della cartella
resources
abbiamo una cartella chiamataScripts
che contiene un file di script di shell eseguibile. -
CrunchifyMain.java
è un punto di partenza principale che contiene il metodomain(String args[])
. -
pom.xml
in cui aggiungeremo Maven Plugins che creerà un progetto .jar eseguibile con tutte le dipendenze incluse.
Passo 1
Apri il tuo file pom.xml
e aggiungi sotto <build>
. Nota: ho aggiunto 3 plugin qui sotto.
-
maven-resources-plugin
: il plugin Resources gestisce la copia delle risorse del progetto nella directory di output. Le risorse principali sono le risorse associate al codice sorgente principale. -
maven-dependency-plugin:
il plug-in di dipendenza fornisce la capacità di manipolare gli artefatti. Può copiare e/o decomprimere artefatti da repository locali o remoti in una posizione specificata. -
maven-jar-plugin:
questo plugin offre la possibilità di creare e firmare jar.
Ecco un file pom.xml completo. Principalmente saresti interessato al tag <build>.
Aggiorna la posizione della directory, il nome del file e il percorso in base alle tue necessità di seguito.
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 > |
Passo 2
Fare clic con il tasto destro su Project -> Run As -> Maven Build


Passaggio 3
Fornisci l'argomento " clean install
"

Passaggio 4
Dovresti vedere un risultato simile a questo.

Passaggio 5
Ora controlla la cartella della cartella /target/Crunchify
per controllare tutto sotto.

Passaggio 6
Ora esegui il tuo progetto con il comando sottostante $bash> java -jar Crunchify.jar
Fammi sapere se hai qualche problema con il progetto di costruzione. Divertiti e buona codifica.