Maven教程

Maven Web应用程序

我们可以通过执行 mvn工具的 archetype: generate 命令来创建一个简单的maven Web应用程序示例
要使用maven创建一个简单的Java项目,您需要打开命令提示符并运行mvn工具的 archetype: generate 命令。

语法

用于生成项目体系结构的 语法如下:
mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=booleanValue
示例
用于生成项目体系结构的 示例如下所示:
mvn archetype:generate -DgroupId=com.lidihuo -DartifactId=CubeGeneratorWeb 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
注意: 在这里,我们使用maven-archetype-webapp创建简单的maven Web应用程序。如果使用maven-archetype-quickstart,它将生成一个简单的maven核心项目。
输出
现在它将 在命令提示符下生成以下代码:
mvn archetype:generate -DgroupId=com.lidihuo -DartifactId=CubeGeneratorWe
b -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 3.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.8 KB/sec)
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-webapp:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.lidihuo
[INFO] Parameter: packageName, Value: com.lidihuo
[INFO] Parameter: package, Value: com.lidihuo
[INFO] Parameter: artifactId, Value: CubeGeneratorWeb
[INFO] Parameter: basedir, Value: D:\
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\CubeGeneratorWeb
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.273s
[INFO] Finished at: Thu Dec 26 19:25:04 IST 2013
[INFO] final Memory: 10M/24M
[INFO] ------------------------------------------------------------------------
'cmd' is not recognized as an internal or external command,
operable program or batch file.
生成的目录结构
现在转到执行mvn命令的当前目录。例如: d:\CubeGeneratorWeb 。您将看到创建了一个具有以下目录的简单Java项目:
CubeGenerator
-src
--main
---resources
---webapp
----WEB-INF
-----web.xml
----index.jsp
-pom.xml
如您所见,创建了3个文件pom.xml,index.jsp和web.xml。让我们快速看一下这些文件:
1、自动生成的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.lidihuo</groupId>
  <artifactId>CubeGeneratorWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CubeGeneratorWeb 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>CubeGeneratorWeb</finalName>
  </build>
</project>
2、自动生成的index.jsp文件
 
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
3、自动生成的web.xml文件
 
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

部署并运行Maven Web项目

现在,您需要在服务器上部署项目并通过以下URL访问它:
http:// <主机名> : <端口号> /项目名称,例如: http://localhost: 8888/CubeGeneratorWeb
 maven webapp输出

Eclipse中的Maven Web应用程序

您可以在Eclipse中导入Maven Web项目。为此,请执行以下步骤:
1)打开Eclipse IDE
2)导入maven项目
文件菜单->导入-> Maven->现有的Maven项目
maven webapp eclipse import
->下一步 -> 浏览项目
maven webapp eclipse项目
-> 完成。
3)运行maven Web项目
右键单击项目->运行方式->在服务器上运行

Eclipse中的Maven Webapp的目录结构

maven webapp eclipse目录结构
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4