-

本教程将教您如何使用版本控制系统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应用程序项目结构。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[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	rucks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.016 s
[INFO] Finished at: 2015-09-27T17:47:56+05:30
[INFO] Final Memory: 16M/247M
[INFO] ------------------------------------------------------------------------

现在去C:/ MVN目录。您将看到一个名为卡车的java应用程序项目(在artifactId中指定)。

Java Web应用程序项目结构

Maven使用标准目录布局。使用上面的例子,我们可以理解以下关键概念

文件夹结构 描述
卡车 包含src文件夹和pom.xml
src / main / webapp 包含index.jsp和WEB-INF文件夹。
src / main / webapp / WEB-INF 包含web.xml
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>卡车> src> main> webapp>文件夹,你会看到index.jsp。

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

创建Web应用程序

我们打开命令控制台,执行C: MVN trucks目录,然后执行下面的mvn命令。

C:MVN	rucks>mvn clean package

Maven将开始建设项目。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building trucks Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ trucks ---
[INFO] Deleting C:MVN	rucks	arget
[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) @ tr
ucks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:MVN	ruckssrc	est
esources
[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 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [trucks] in [C:MVN	rucks	arget	rucks]
[INFO] Processing war project
[INFO] Copying webapp resources [C:MVN	ruckssrcmainwebapp]
[INFO] Webapp assembled in [93 msecs]
[INFO] Building war: C:MVN	rucks	arget	rucks.war
[INFO] WEB-INFweb.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.766 s
[INFO] Finished at: 2015-09-27T17:53:05+05:30
[INFO] Final Memory: 11M/247M
[INFO] ------------------------------------------------------------------------

部署Web应用程序

现在,复制trucks.war中创建C:> MVN>货车>目标>文件夹到你的Web服务器的webapps目录下,并重新启动Web服务器。

测试Web应用程序

使用URL运行Web应用程序:http:// <server-name>:<port-number> /trucks/index.jsp

验证输出。

网页