更新時間:2022-03-28 09:30:22 來源:動力節點 瀏覽5213次
使用maven,制作jar文件、war文件、ear文件等非常簡單。Maven 支持非常大的內置打包列表。此外,它可以在 maven 中定義用戶定義的包裝。讓我們在這篇文章中了解有關 Maven 打包的所有信息。
Maven 支持構建打包,如jar, ear, war...等。在 Maven 中,構建什么是由打包屬性決定的。它是在<packaging></packaging>標簽之間定義的。如果 pom.xml 中沒有打包標簽,那么默認情況下它會將打包類型視為 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:這是默認的打包方式,這個打包方式是用來構建jar文件的。
war:這個包裝有助于創建一個網絡存檔文件。它用于創建 Web 應用程序。
ear:此打包用于創建企業歸檔文件(ear)。ear 支持創建 j2ee 應用程序,即 EJB 支持,JMS對 ear 文件的支持。最后,ear 文件僅在 glassfish 或 JBoss 等應用服務器上運行。
pom:此包裝用于子模塊實現。包裝 pom 主要意味著沒有主要的人工制品。
maven-plugin:用于創建maven插件。
ejb:用于封裝EJB模塊。
rar:用于創建資源適配器存檔(RAR)文件。在 RAR 文件中,它包含帶有實現類的 jar 文件和一個 META-INF 文件夾,其中包含服務器特定的部署描述符和 ra.xml 文件,它是 rar 配置文件。
在外部插件和項目的幫助下,maven 支持許多額外的包裝。SWF,SWC包裝是其中少數。
接下來,讓我們繼續看看如何使用 maven 創建自定義包裝。
在此之前,要了解 maven 自定義打包,需要對 maven 生命周期有一個很好的了解。
現在讓我們開始我們的新包創建編碼。
首先,我們需要為我們的新打包創建一個插件項目,并在其上添加maven插件創建依賴項。
<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 已經準備好了。
讓我們繼續下一步,我們知道它為每個打包創建一個單獨的生命周期,它在META-INF/plexus/components.xml文件中定義。
在我們的例子中,我們正在創建名為“ anish ”的新包裝。'anish'打包實現的 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 打包階段。它使用自定義實現的打包階段。
<package>
com.lici.maven:maven-anish-package:1.0:anish
</package>
這里的階段定義com.lici.maven:maven-anish-package:1.0顯示了插件項目的groupId:artifactId:version。最后一個值anish是創建的 mojo bean 名稱,它是使用 @Mojo Annotation 創建的。
以下是我們新插件的整體外觀。
所以我們創造了我們的新包裝。
接下來,讓我們將包應用到一個新的 maven 項目上。所以創建了一個新項目,它的 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不知道我們新創建的包裝。所以我們需要通過使用插件標簽來告訴我們新創建的包裝。在插件內部,我們將擴展標志定義為 truetrue<extensions>true</extensions>.如果您正在創建新包裝,則擴展標志應設置為 true。
添加插件后,可以將打包稱為anishanish<packaging>anish</packaging>.在構建執行中,輸出基于anish打包配置創建。
現在我們的新包 anish 已經準備好了,我對其進行了測試,它運行良好!!!如果大家想了解更多相關知識,可以關注一下動力節點的Maven教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習