1.选中maven,构建maven web
2.项目构建好之后发现一个错误如图,是因为没有servlet的API,我们需要在pom.xml 中对servlet的API进行依赖
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.imooc.Webapp2</groupId> <artifactId>Webapp2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Webapp2 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> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>Webapp2</finalName> </build> </project>
3.我们看项目的结构不是maven的结构,缺少src/main/java和src/test/java的包为此添加这两包
选中maven项目 Build Path , Configuer Build Path
删除图中标识的src/main/java和src/test/java后点击Apply点击OK
最后我们再选中maven项目中的JAVA Resource 右键选中New在选中Source Folder
创建src/main/java
在创建src/test/java
4.在pom.xml中配置tomcat的插件,打开DOS命令到项目的根目录下运行mvn install 即可
<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.imooc.Webapp2</groupId> <artifactId>Webapp2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Webapp2 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> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>Webapp2</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>