(一)通过mvn命令构建一个maven工程:
1、创建maven的web项目:
进入你想要创建项目的文件夹下,输入命令:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp(该参数指定工程为web工程)
D:>mvn archetype:generate [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) @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode(在这里会卡顿一会儿,因为要从远程下载模板,创建命令中输入-DarchetypeCatalog=internal可以跳过从远程下载) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1426:(直接回车) Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 7: 1.3 8: 1.4 Choose a number: 8:(直接回车) Downloading: http://192.168.21.14:25000/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar Downloaded: http://192.168.21.14:25000/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.jar (7 KB at 44.2 KB/sec) Downloading: http://192.168.21.14:25000/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom Downloaded: http://192.168.21.14:25000/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.4/maven-archetype-quickstart-1.4.pom (2 KB at 13.8 KB/sec) Define value for property 'groupId': : com.cmos Define value for property 'artifactId': : zht Define value for property 'version': 1.0-SNAPSHOT: : 1.0 Define value for property 'package': com.cmos: : zht Confirm properties configuration: groupId: com.cmos(输入groupId,自己定义,一般为org.google这种格式) artifactId: zht(输入项目artifactId,自定义,工程名) version: 1.0(输入项目版本,自定义) package: zht(输入最终package命令打包出来的jar包或war包名称) Y: :(直接回车) [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.cmos [INFO] Parameter: artifactId, Value: zht [INFO] Parameter: version, Value: 1.0 [INFO] Parameter: package, Value: zht [INFO] Parameter: packageInPathFormat, Value: zht [INFO] Parameter: package, Value: zht [INFO] Parameter: version, Value: 1.0 [INFO] Parameter: groupId, Value: com.cmos [INFO] Parameter: artifactId, Value: zht [INFO] project created from Archetype in dir: D:zht [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2:38.580s [INFO] Finished at: Mon Sep 30 10:08:24 CST 2019 [INFO] Final Memory: 13M/124M [INFO] ------------------------------------------------------------------------
当然,你也可以输入命令:mvn archetype:generate -DgroupId=com.cmos -DartifactId=zhyyt -Dversion=0.01-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeCatalog=internal 自动生成项目,省去了上述的输入步骤。
构建好的web项目目录为下图这种目录结构:
2、当构建时没有输入-DarchetypeArtifactId=maven-archetype-webapp,则构建的为普通java工程,构建的工程目录为下图结构:
(二)通过Eclipse构建一个maven工程:
1、 File -> New -> Maven Project -> Next ,在接下来的窗口中会看到一大堆的项目模板,Filter栏里输入“webapp”,选择下图中选项,然后Next
2、 在弹出的窗口中输入下列项目信息,然后Finish:
3、右键生成的项目,选择properties --> Java Build Path,将jdk改为workSpace的jdk
4、Java Compiler和Project Facts设置参考下图
5、Deployment Assembly配置的为package打包发布的内容,去掉test目录
6、点击OK后,至此项目的目录结构应该如下图:
7、src下的index.jsp报错“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”
解决方法,在pom.xml文件中添加依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
8、pom.xml文件报错“org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)”
该报错其实不影响工程,想要去掉报错,参考网上解决方法,要安装插件,试了几次没成功。
根据报错信息,看到maven编译的版本跟JDK版本不匹配,原来我在创建maven的时候指定了一个版本,以致于没有用默认的版本。
但是我不想更改我的maven版本,最终将pom.xml文件中指定的mvn插件版本的配置去掉,成功: