2538

【Maven】如何創建一個簡單可部屬的WAR檔

“
滿重要的一個大功能,在使用JAVA網頁伺服器的時候一定會需要這個WAR檔來進行部屬
„

分享此文連結 //n.sfs.tw/15514

分享連結 【Maven】如何創建一個簡單可部屬的WAR檔@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-03-10 21:51:25 最後編修
2021-11-27 09:39:54 By 過路君子
 

大家好,這裡是正在重新梳理頭緒的小編過路君子

之前有曾經寫過如何使用 Maven來部屬 War檔到 Wildfly裡面,不知道大家是否還記得?

 

 

承如開頭所述,那篇文章在這裡: 【Maven】用Maven來託管Wildfly/jBoss的部屬(deploy)、解部屬(undeploy)@小編過路君子。

算是篇超級極簡版介紹,沒有任何的解釋,只有一堆的設定檔和一個用手畫出來的目錄結構,這篇僅把上方那篇 war檔的打包切割出來。

那如果各位只是單純需要用 Maven將專案打包成可執行的 jar檔的話,可以參考這篇: 【Maven】如何創建一個簡單可執行的JAR檔@小編過路君子

那麼,既然是 Maven那當然要先上的就是目錄結構啦。

(所有圖片點擊都可以放大、變高清)

 

那需要由我們設定的檔案總共有四個,也就是上圖中的四個白色字的檔案。

那我們就一個檔案一個檔案來吧,首先是 jboss-web.xml檔案:

<jboss-web>
    <context-root>/</context-root>
</jboss-web>

 

然後是 web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>index</servlet-name>
        <jsp-file>index.jsp</jsp-file>
    </servlet>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <servlet-mapping>
        <servlet-name>register</servlet-name>
        <url-pattern>/register/*</url-pattern>
    </servlet-mapping>
</web-app>

 

接著就是 index.html的檔案內容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Index</title>
</head>

<body>
    <h1>Hello World!</h1>
</body>
</html>

 

最後就是我們的主角啦,我們的 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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

	<groupId>passing.editor.guild245</groupId>
	<artifactId>pixiJS-web</artifactId>
	<version>1.0.0</version>

	<name>pixiJS-web Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<packaging>war</packaging>
  
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>3.3.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-clean-plugin</artifactId>
					<version>3.1.0</version>
				</plugin>
				<plugin>
					<artifactId>maven-resources-plugin</artifactId>
					<version>3.0.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.22.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-install-plugin</artifactId>
					<version>2.5.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-deploy-plugin</artifactId>
					<version>2.8.2</version>
				</plugin>
				<plugin>
					<artifactId>maven-site-plugin</artifactId>
					<version>3.7.1</version>
				</plugin>
				<plugin>
					<artifactId>maven-project-info-reports-plugin</artifactId>
					<version>3.0.0</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

 

最後只要將整個 Maven專案打包就好囉~

Linux mvn package

 

這時就可以看到我們的 War檔躺在資料夾裡面啦~剩下的要怎麼料理就是各位的需求了。

 

 

 

後記

小編昨天發布完如何打包成可執行的 Jar檔那篇文章之後就上床睡覺了,睡著睡著突然想到,為何不把如何打包成 War檔的文章也發布一下,雖然就寫在那篇介紹 Wildfly部屬的文章裡面,但想想每次要用都要重新切一次就好麻煩。

END

你可能感興趣的文章

【Wickct】如何製作一個簡單的動態響應頁面和傳遞參數至其他頁面 身為一個後端架構程式,這個功能當然是非常重要之一

【Arduino/LinkIt 7697】實作小小的 MQTT Publish & Subscribe 若是要多點對多點傳輸資料,那使用 MQTT 即可快速的達成我們所需的目的

【Docker hub】[Linux]以IPv6來pull容器(container)吧! 在一個 IPv6 還不盛行的年代,做事情總是特別麻煩

【MEGAcmd】[Linux] 如何解決Unable to connect to service: error=2 用了好久的軟體突然停止一切的運作了。

【Discord bot 2.0.1】(discord.ui.View)如何將舊機器人升級至目前最新版本 很多時候升級軟體是為了讓別人覺得你的程式很先進,但這次卻是因為要使用某個新功能

【C++】使用struct array和一維int array模擬二維int array 主要是因為用sort去排序int array的二維陣列小編懶得研究,於是就研究了一種維持一維陣列但是有二維陣列效果的程式,小編就廢~~

隨機好文

【數學】徐氏數學簡明講義(三) 第二章 直線與園 P2.1-17 Q26 26.三角形的兩邊分別在二直線 x-3y+10=0 , 2x-y-8=0 上,且知第三邊中點為(3 , 2),求第三邊所

【中翻英歌詞】(二創歌)密匿的四個季節 Rap Battles (Gap Battles!) of Gensokyo. 幻想鄉饒舌(隙間!)大戰 Secret God, Okina Matara versus Youkai Sage, Yu

【英翻中歌詞】(二創歌)廣重36號 ~ Neo Super-Express (Keep it running on~) All on board, all on board. (繼續奔馳著~) 快

【數學】高二上 三角函數公式 筆記筆記,方便以後複習之用

【影片心得&大意】(空中浩劫)法國航空4590號班機空難 一架飛機若會發生事故,那除了是飛機本身的故障或是飛行員的操作失誤,若前兩者都沒問題,是否有一些外在的因素參雜進來?

精讚::