Maven Web 應(yīng)用
maven web 應(yīng)用
本章節(jié)介紹如何使用版本控制系統(tǒng) maven 來管理一個基于 web 的項目,如何創(chuàng)建、構(gòu)建、部署已經(jīng)運行一個 web 應(yīng)用。
1. 創(chuàng)建 web 應(yīng)用
我們可以使用 maven-archetype-webapp 插件來創(chuàng)建一個簡單的 java web 應(yīng)用。
打開命令控制臺,進(jìn)入到 c:\mvn 文件夾,然后執(zhí)行以下的 mvn 命令:
c:\mvn>mvn archetype:generate -dgroupid=com.companyname.automobile -dartifactid=trucks -darchetypeartifactid=maven-archetype-webapp -dinteractivemode=false
執(zhí)行完后 maven 將開始處理,并且創(chuàng)建完整的于java web 項目的目錄結(jié)構(gòu)。
[info] scanning for projects... [info] searching repository for plugin with prefix: 'archetype'. [info] ------------------------------------------------------------------- [info] building maven default project [info] task-segment: [archetype:generate] (aggregator-style) [info] ------------------------------------------------------------------- [info] preparing archetype:generate [info] no goals needed for project - skipping [info] [archetype:generate {execution: default-cli}] [info] generating project in batch mode [info] -------------------------------------------------------------------- [info] using following parameters for creating project from old (1.x) archetype: maven-archetype-webapp:1.0 [info] -------------------------------------------------------------------- [info] parameter: groupid, value: com.companyname.automobile [info] parameter: packagename, value: com.companyname.automobile [info] parameter: package, value: com.companyname.automobile [info] parameter: artifactid, value: trucks [info] parameter: basedir, value: c:\mvn [info] parameter: version, value: 1.0-snapshot [info] project created from old (1.x) archetype in dir: c:\mvn\trucks [info] ------------------------------------------------------------------- [info] build successful [info] ------------------------------------------------------------------- [info] total time: 16 seconds [info] finished at: tue jul 17 11:00:00 ist 2012 [info] final memory: 20m/89m [info] -------------------------------------------------------------------
執(zhí)行完后,我們可以在 c:/mvn 文件夾下看到 trucks 項目,查看項目的目錄結(jié)構(gòu):
maven 目錄結(jié)構(gòu)是標(biāo)準(zhǔn)的,各個目錄作用如下表所示:
文件夾結(jié)構(gòu) | 描述 |
---|---|
trucks | 包含 src 文件夾和 pom.xml 文件。 |
src/main/webapp | 包含 index.jsp 文件和 web-inf 文件夾. |
src/main/webapp/web-inf | 包含 web.xml 文件 |
src/main/resources | 包含圖片、properties資源文件。 |
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>
接下來我們打開 c:\ > mvn > trucks > src > main > webapp > 文件夾,可以看到一個已經(jīng)創(chuàng)建好的 index.jsp 文件,代碼如下:
<html> <body> <h2>hello world!</h2> </body> </html>
2. 構(gòu)建 web 應(yīng)用
打開命令控制臺,進(jìn)入 c:\mvn\trucks 目錄,然后執(zhí)行下面的以下 mvn 命令:
c:\mvn\trucks>mvn clean package
maven 將開始構(gòu)建項目:
[info] scanning for projects... [info] ------------------------------------------------------------------- [info] building trucks maven webapp [info] task-segment: [clean, package] [info] ------------------------------------------------------------------- [info] [clean:clean {execution: default-clean}] [info] [resources:resources {execution: default-resources}] [warning] using platform encoding (cp1252 actually) to copy filtered resources,i.e. build is platform dependent! [info] copying 0 resource [info] [compiler:compile {execution: default-compile}] [info] no sources to compile [info] [resources:testresources {execution: default-testresources}] [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] [compiler:testcompile {execution: default-testcompile}] [info] no sources to compile [info] [surefire:test {execution: default-test}] [info] no tests to run. [info] [war:war {execution: default-war}] [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[77 msecs] [info] building war: c:\mvn\trucks\target\trucks.war [info] ------------------------------------------------------------------- [info] build successful [info] ------------------------------------------------------------------- [info] total time: 3 seconds [info] finished at: tue jul 17 11:22:45 ist 2012 [info] final memory: 11m/85m [info] -------------------------------------------------------------------
3. 部署 web 應(yīng)用
打開 c:\ < mvn < trucks < target < 文件夾,找到 trucks.war 文件,并復(fù)制到你的 web 服務(wù)器的 web 應(yīng)用目錄,然后重啟 web 服務(wù)器。
4. 測試 web 應(yīng)用
訪問以下 url 運行 web 應(yīng)用:
http://:/trucks/index.jsp
驗證結(jié)果:
相關(guān)文章
- JDBC 教程
- JDBC 驅(qū)動類型
- JDBC 連接數(shù)據(jù)庫范例
- JDBC 連接數(shù)據(jù)庫步驟
- JDBC Statement, PreparedStatement 和 CallableStatement
- JDBC ResultSet 結(jié)果集
- JDBC Resultset 結(jié)果集范例
- JDBC 事務(wù)保存點范例
- Scala 教程
- Scala 簡介
- Scala 類和對象
- Scala 文件 I/O
- Spring 教程
- Spring 模塊
- Spring 依賴注入
- Spring 自動裝配
- Spring MVC教程
- Spring MVC表單標(biāo)簽庫
- Spring security