2.1 maven-resources-plugin copy resources plugin
Yen sampeyan nggawe aplikasi web, sampeyan mung bakal duwe macem-macem sumber daya. Iki minangka perpustakaan jar, servlet jsp, file setelan. Lan mesthi, iki akeh file statis kaya html
, css
, js
, uga macem-macem gambar.
Kanthi gawan, nalika mbangun proyek, Maven mung bakal nyalin kabeh file saka folder src/main/resources
menyang direktori target. Yen sampeyan pengin ngowahi prilaku iki, plugin bakal mbantu sampeyan maven-resources-plugin
.
Kode conto kanggo plugin kasebut:
<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/resources
</outputDirectory>
<resources>
<resource> instructions for copying a resource 1 </resource>
<resource> instructions for copying a resource 2 </resource>
<resource> instructions for copying a resource N </resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Plugin iki bakal diarani sajrone fase validasi. Nggunakake tag, <outputDirectory>
sampeyan bisa nyetel direktori ing ngendi plugin kudu nyalin sumber daya sing ditemtokake ing file <resources>
. Lan ing kene plugin kasebut bisa mbukak kabeh kekuwatane.
2.2 Nyaring sumber daya karo maven-resources-plugin
Sumber daya plugin bisa ditemtokake ora mung ing wangun file, nanging langsung ing wangun direktori. Kajaba iku, topeng bisa ditambahake menyang direktori, sing nemtokake file sing bakal dilebokake ing sumber kasebut.
Tuladha:
<resource>
<directory>src/main/resources/images</directory>
<includes>
<include>**/*.png</include>
</includes>
</resource>
Rong tanda bintang minangka topeng makili sawetara direktori . Ing conto ing ndhuwur, kabeh file png sing ana ing direktori src/main/resources/images
(lan subdirektori) bakal dijupuk minangka data sumber daya.
Yen sampeyan pengin ngilangi sawetara file, sampeyan bisa nggunakake file exclude
. Tuladha:
<resource>
<directory>src/main/resources/images</directory>
<includes>
<include>**/*.png</include>
</includes>
<excludes>
<exclude>old/*.png</exclude>
</excludes>
</resource>
Tag ditrapake kanthi urutan: pisanan, file kalebu sing ditemtokake ing file kalebu bakal ditambahake menyang sumber daya, banjur file sing ora kalebu bakal dibuwang saka dhaptar iki.
Nanging ora mung kuwi. Plugin bisa katon ing file (yen teks, mesthi). Lan, contone, nambah application.properties
versi perakitan sing dibutuhake menyang file kasebut. Supaya plugin bisa ngolah isi file, sampeyan kudu nemtokake parameter <filtering>true</filtering>
.
Tuladha:
<resource>
<directory>src/main/resources/properties</directory>
<filtering>true</filtering>
<includes>
<include>**/*. properties </include>
</includes>
</resource>
Rincian liyane babagan plugin iki bisa ditemokake ing link: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
2.3 plugin inklusi sumber maven-source-plugin
Plugin liyane sing migunani - maven-source-plugin
ngidini sampeyan nyakup kode sumber file java ing perakitan. Kanggo apa?
Bab kasebut yaiku saliyane aplikasi web, akeh perpustakaan sing diklumpukake nggunakake Maven. Kathah proyèk Jawa ngetutake konsep open-source lan disebarake menyang komunitas Jawa kanthi sumber dhewe.
Napa sampeyan butuh plugin sing kapisah? Apa sampeyan ora bisa nyalin sumber lan mung iku?
Kaping pisanan, ing sembarang proyek rumit, sumber bisa disimpen ing sawetara panggonan.
Kapindho, generasi sumber adhedhasar spesifikasi xml asring digunakake, sumber kasebut uga kudu dilebokake ing perakitan.
Lan kaping telune, sampeyan bisa mutusake ora kalebu file rahasia ing perakitan sampeyan.
Conto nggunakake plugin maven-source-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
2.4 maven-dependency-plugin dependency copy plugin
Sampeyan uga kudu pinter nyalin dependensi (perpustakaan) nalika mbangun proyek. Plugin digunakake kanggo iki maven-dependency-plugin
.
Tuladha:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<outputDirectory>
${project.build.directory}/lib/
</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
Ing conto iki, prilaku standar plugin ditulis - nyalin perpustakaan menyang ${project.build.directory}/lib
.
Bagean eksekusi nyatakake yen plugin bakal diarani sajrone fase mbangun - paket, gol - dependensi salinan.
Umumé, plugin iki duwe target sing cukup gedhe, ing ngisor iki sing paling populer:
1 | gumantung: nganalisis | analisis dependensi (digunakake, ora digunakake, ditemtokake, ora ditemtokake) |
2 | dependensi: nganalisis-duplikat | ngenali dependensi duplikat |
3 | gumantung: mutusake | resolusi (definisi) kabeh dependensi |
4 | ketergantungan:resolve-plugin | ijin (definisi) kabeh plugins |
5 | ketergantungan: wit | nampilake wit dependensi |
Sampeyan uga bisa nyetel paramèter tambahan ing bagean konfigurasi:
1 | outputDirektori | Direktori ing ngendi dependensi bakal disalin |
2 | overWriteRleases | Gendéra kanggo nimpa dependensi nalika nggawe rilis |
3 | overWriteSnapshots | Gendéra kanggo nimpa dependensi non-terminal sing duwe SNAPSHOT |
4 | overWriteIfNewer | Gendéra kanggo nimpa perpustakaan nganggo versi sing luwih anyar |
Tuladha:
<configuration>
<outputDirectory>
${project.build.directory}/lib/
</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
Kanthi gawan <overWriteReleases>
, lan <overWriteSnapshots>
salah, amarga <overWriteIfNewer>
bener.