Maven - web 实例
版权所有,未经授权,禁止转载
章节
Maven提供了不少项目模板,例如 web项目模板:maven-archetype-webapp、Java应用模板:maven-archetype-quickstart等等,开发人员可以通过执行maven的archetype:generate
基于这些项目模板生成项目。
生成项目命令格式:
mvn archetype:generate -DgroupId=com.qikegu.com -DartifactId=qikegu-maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
-DarchetypeArtifactId=maven-archetype-webapp
指定了项目模板maven-archetype-webapp
。
1. 创建项目
我们将基于项目模板maven-archetype-webapp
,生成一个web项目。
执行命令
mvn archetype:generate -DgroupId=com.qikegu.com -DartifactId=qikegu-maven-web -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] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
Downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-web
app/1.0/maven-archetype-webapp-1.0.pom
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-weba
pp/1.0/maven-archetype-webapp-1.0.pom (533 B at 875 B/s)
Downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bun
dles/2/maven-archetype-bundles-2.pom
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bund
les/2/maven-archetype-bundles-2.pom (1.5 kB at 3.5 kB/s)
Downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-pare
nt/1/maven-archetype-parent-1.pom
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-paren
t/1/maven-archetype-parent-1.pom (1.3 kB at 2.6 kB/s)
Downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.
pom
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.p
om (10.0 kB at 24 kB/s)
Downloading from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-web
app/1.0/maven-archetype-webapp-1.0.jar
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-weba
pp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 7.9 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: G:\project\test
[INFO] Parameter: package, Value: com.qikegu.com
[INFO] Parameter: groupId, Value: com.qikegu.com
[INFO] Parameter: artifactId, Value: qikegu-maven-web
[INFO] Parameter: packageName, Value: com.qikegu.com
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: G:\project\test\qikegu-maven-web
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.654 s
[INFO] Finished at: 2019-04-22T16:33:38+08:00
[INFO] ------------------------------------------------------------------------
Kevin@KEVIN-PC G:\project\test
项目结构
进入生成的项目目录,查看项目目录结构如下:
G:\PROJECT\TEST\QIKEGU-MAVEN-WEB
│ pom.xml
│
└─src
└─main
├─resources
└─webapp
│ index.jsp
│
└─WEB-INF
web.xml
可以看到,maven自动创建了3个文件:pom.xml、index.jsp和web.xml。
让我们快速浏览一下这些文件:
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.qikegu.com</groupId>
<artifactId>qikegu-maven-web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>qikegu-maven-web 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>qikegu-maven-web</finalName>
</build>
</project>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
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>
2. 构建项目
在命令行中,切换到项目目录,执行以下命令
> mvn package
可以看到项目目录下多了一个target目录,target目录下已经生成qikegu-maven-web.war
包。
3. 部署项目
把qikegu-maven-web.war
包拷贝到webserver(如tomcat)下的webapp目录下,重启服务器。
然后就可通过浏览器访问了,通常url如下(根据服务器配置):
http://localhost:80/qikegu-maven-web/index.jsp