更新時間:2022-03-28 09:30:22 來源:動力節(jié)點 瀏覽5039次
使用maven,制作jar文件、war文件、ear文件等非常簡單。Maven 支持非常大的內(nèi)置打包列表。此外,它可以在 maven 中定義用戶定義的包裝。讓我們在這篇文章中了解有關(guān) Maven 打包的所有信息。
Maven 支持構(gòu)建打包,如jar, ear, war...等。在 Maven 中,構(gòu)建什么是由打包屬性決定的。它是在<packaging></packaging>標(biāo)簽之間定義的。如果 pom.xml 中沒有打包標(biāo)簽,那么默認情況下它會將打包類型視為 jar。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.lici.maven</groupId>
<artifactId>maven-packaging</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
讓我們看看maven核心支持哪些封裝。
jar:這是默認的打包方式,這個打包方式是用來構(gòu)建jar文件的。
war:這個包裝有助于創(chuàng)建一個網(wǎng)絡(luò)存檔文件。它用于創(chuàng)建 Web 應(yīng)用程序。
ear:此打包用于創(chuàng)建企業(yè)歸檔文件(ear)。ear 支持創(chuàng)建 j2ee 應(yīng)用程序,即 EJB 支持,JMS對 ear 文件的支持。最后,ear 文件僅在 glassfish 或 JBoss 等應(yīng)用服務(wù)器上運行。
pom:此包裝用于子模塊實現(xiàn)。包裝 pom 主要意味著沒有主要的人工制品。
maven-plugin:用于創(chuàng)建maven插件。
ejb:用于封裝EJB模塊。
rar:用于創(chuàng)建資源適配器存檔(RAR)文件。在 RAR 文件中,它包含帶有實現(xiàn)類的 jar 文件和一個 META-INF 文件夾,其中包含服務(wù)器特定的部署描述符和 ra.xml 文件,它是 rar 配置文件。
在外部插件和項目的幫助下,maven 支持許多額外的包裝。SWF,SWC包裝是其中少數(shù)。
接下來,讓我們繼續(xù)看看如何使用 maven 創(chuàng)建自定義包裝。
在此之前,要了解 maven 自定義打包,需要對 maven 生命周期有一個很好的了解。
現(xiàn)在讓我們開始我們的新包創(chuàng)建編碼。
首先,我們需要為我們的新打包創(chuàng)建一個插件項目,并在其上添加maven插件創(chuàng)建依賴項。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lici.maven</groupId>
<artifactId>maven-anish-package</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<mavenFileManagementVersion>3.0.0</mavenFileManagementVersion>
<mavenArchiverVersion>3.5.0</mavenArchiverVersion>
<mavenVersion>3.1.0</mavenVersion>
<javaVersion>7</javaVersion>
</properties>
<dependencies>
<!-- all dependencies information -->
</dependencies>
</project>
我們的插件項目的 pom.xml 已經(jīng)準備好了。
讓我們繼續(xù)下一步,我們知道它為每個打包創(chuàng)建一個單獨的生命周期,它在META-INF/plexus/components.xml文件中定義。
在我們的例子中,我們正在創(chuàng)建名為“ anish ”的新包裝。'anish'打包實現(xiàn)的 components.xml如下所示。
<?xml version="1.0"?>
<!DOCTYPE component-set>
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>anish</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
</implementation>
<configuration>
<phases>
<process-resources>
org.apache.maven.plugins:maven-resources-plugin:2.6:resources
</process-resources>
<compile>
org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
</compile>
<process-test-resources>
org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
</process-test-resources>
<test-compile>
org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
</test-compile>
<test>
org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
</test>
<package>
com.lici.maven:maven-anish-package:1.0:anish
</package>
<install>
org.apache.maven.plugins:maven-install-plugin:2.4:install
</install>
<deploy>
org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
</deploy>
</phases>
</configuration>
</component>
<component>
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
<role-hint>anish</role-hint>
<implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
<configuration>
<type>anish</type>
<language>java</language>
<addedToClasspath>true</addedToClasspath>
</configuration>
</component>
</components>
</component-set>
在上面的components.xml中,它包含了除了打包階段之外的所有 jar 打包階段。它使用自定義實現(xiàn)的打包階段。
<package>
com.lici.maven:maven-anish-package:1.0:anish
</package>
這里的階段定義com.lici.maven:maven-anish-package:1.0顯示了插件項目的groupId:artifactId:version。最后一個值anish是創(chuàng)建的 mojo bean 名稱,它是使用 @Mojo Annotation 創(chuàng)建的。
以下是我們新插件的整體外觀。
所以我們創(chuàng)造了我們的新包裝。
接下來,讓我們將包應(yīng)用到一個新的 maven 項目上。所以創(chuàng)建了一個新項目,它的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lici.maven</groupId>
<artifactId>maven-packaging</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>anish</packaging>
<build>
<plugins>
<plugin>
<groupId>com.lici.maven</groupId>
<artifactId>maven-anish-package</artifactId>
<version>1.0</version>
<!--declare that this plugin contributes the component extensions-->
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Maven不知道我們新創(chuàng)建的包裝。所以我們需要通過使用插件標(biāo)簽來告訴我們新創(chuàng)建的包裝。在插件內(nèi)部,我們將擴展標(biāo)志定義為 truetrue<extensions>true</extensions>.如果您正在創(chuàng)建新包裝,則擴展標(biāo)志應(yīng)設(shè)置為 true。
添加插件后,可以將打包稱為anishanish<packaging>anish</packaging>.在構(gòu)建執(zhí)行中,輸出基于anish打包配置創(chuàng)建。
現(xiàn)在我們的新包 anish 已經(jīng)準備好了,我對其進行了測試,它運行良好!!!如果大家想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點的Maven教程,里面有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助哦。
初級 202925
初級 203221
初級 202629
初級 203743