Maven - Web 應用



本章將教你如何使用Maven管理基於Web的專案。你將學習如何建立/構建/部署和執行Web應用程式。

建立Web應用程式

要建立一個簡單的Java Web應用程式,我們將使用maven-archetype-webapp外掛。因此,讓我們開啟命令控制檯,進入C:\MVN目錄並執行以下mvn命令。

C:\MVN>mvn archetype:generate 
-DgroupId = com.companyname.automobile 
-DartifactId = trucks
-DarchetypeArtifactId = maven-archetype-webapp 
-DinteractiveMode = false

Maven將開始處理,並將建立完整的基於Web的Java應用程式專案結構,如下所示:

C:\MVN>mvn archetype:generate -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
...
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: package, Value: com.companyname.automobile
[INFO] Parameter: groupId, Value: com.companyname.automobile
[INFO] Parameter: artifactId, Value: trucks
[INFO] Parameter: packageName, Value: com.companyname.automobile
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\trucks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.381 s
[INFO] Finished at: 2021-12-13T19:00:13+05:30
[INFO] ------------------------------------------------------------------------
C:\MVN>

現在進入C:/MVN目錄。你將看到一個名為trucks(在artifactId中指定)的已建立的Java應用程式專案,如下面的快照所示。以下目錄結構通常用於Web應用程式:

Java web application project structure

Maven使用標準的目錄佈局。使用上面的例子,我們可以理解以下關鍵概念:

序號 資料夾結構和描述
1

trucks

包含src資料夾和pom.xml。

2

src/main/webapp

包含index.jsp和WEB-INF資料夾。

3

src/main/webapp/WEB-INF

包含web.xml

4

src/main/resources

它包含影像/屬性檔案。

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>com.companyname.automobile</groupId>
   <artifactId>trucks</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>trucks Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
       </dependency>
   </dependencies>
   <build>
      <finalName>trucks</finalName>
   </build>
</project>

如果你觀察一下,你會發現Maven也建立了一個示例JSP原始檔。

開啟C:\ > MVN > trucks > src > main > webapp >資料夾,檢視包含以下程式碼的index.jsp:

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

構建Web應用程式

讓我們開啟命令控制檯,進入C:\MVN\trucks目錄並執行以下mvn命令。

C:\MVN\trucks>mvn clean package

Maven將開始構建專案。

C:\MVN\trucks>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.companyname.automobile:trucks >------------------
[INFO] Building trucks Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ trucks ---
[INFO] Deleting C:\MVN\trucks\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ trucks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ trucks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\trucks\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ trucks ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ trucks ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/intel/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Packaging webapp
[INFO] Assembling webapp [trucks] in [C:\MVN\trucks\target\trucks]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\MVN\trucks\src\main\webapp]
[INFO] Webapp assembled in [50 msecs]
[INFO] Building war: C:\MVN\trucks\target\trucks.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.494 s
[INFO] Finished at: 2021-12-13T19:02:15+05:30
[INFO] ------------------------------------------------------------------------
C:\MVN\trucks>

部署Web應用程式

現在將C:\ > MVN > trucks > target >資料夾中建立的trucks.war複製到你的Web伺服器webapp目錄中,然後重啟Web伺服器。

測試Web應用程式

使用URL執行Web應用程式:http://<伺服器名稱>:<埠號>/trucks/index.jsp

驗證輸出。

web page
廣告
© . All rights reserved.