[精讚] [會員登入]
2068

【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

你可能感興趣的文章

【Linux】[CentOS 8]如何更新sudo指令 上次會想要更新sudo這個指令的時間應該是2017年了吧?轉眼間就2021了耶

【JSON In Java】如何快速產生JSON 身為三十年老程式語言,當然對Json有不錯的支援,那究竟該如何使用呢

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

【PaperMC - API】如何發送指令到伺服器內 How to sending or executing commands to server

【Linux】[CentOS 8] How to update sudo instruction The last time I wanted to update the sudo command should be 2017, right? It's 2021 in a blink of an

【Socket】(Linux / Python 3)兩不同系統的主機之間如何使用Socket相互溝通 How to connect two different computer use Socket

隨機好文

高捷少女:耐耐的新年驚喜(終) 他的話說到一半,便被一陣響亮的哭聲打住了,是從產房中的傳來的。聽起來就像嬰兒的哭聲。 婕兒、小穹跟艾米也被哭聲吵醒,婕兒揉揉眼睛,看向呆若木雞的耐耐父女。「剛剛的聲音,該不會是……」

【歌評】蓮台野夜行 -古の冥界寺 古老的冥界寺 冥界?那是什麼地方?該不會是像地獄那樣的地方吧?!

【國文】虬髯客傳 大綱① (無本文、無翻譯) 本書被稱為唐人小說最高傑作 本書是一本符合小說五要素的,所以我們來看看其中的時間、空間以及人物。何謂小說五要素?

【中翻英歌詞】(二創歌)請注意萬年備用傘 LONG AGO… ONE DARK AND STORMY NIGHT… 很久以前... 在

【英翻中歌詞】(二創歌)Reverse Ideology ˙˙˙punoɹɐ ןןɐ ʇı uɹnʇ ‘punoɹɐ ןןɐ ʇı uɹnʇ ...吧切一轉翻,吧切一