Eclipse创建一个Maven Web项目
在这篇文章中,我们将演示如何在Eclipse IDE中使用maven创建一个动态Web项目。
使用的工具和技术
- Eclipse Jee Oxygen
- Maven 3.3.3
- JavaSE 1.8
- Servlet API 3.1.0
- Apache Tomcat 7.0.47 (Embeded)
下面开始一步一步在Eclipse中使用maven创建一个web项目。
第1步
转到 New 菜单 Other.. -> Maven -> Maven Project ,然后单击 Next 。如下图所示 -
第2步
在New Maven Project向导中,选择Create a simple project
并指定项目保存的目录(如:F:\worksp\Maven\webproject),然后单击Next
。如下图所示 -
第3步
在下一个向导中,添加项目信息,如下所示,然后单击Finish
。
第4步
打开pom.xml
文件并在其中添加servlet依赖项和Tomcat maven插件,如下代码所示,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.lzr</groupId> <artifactId>Carpet</artifactId> <packaging>war</packaging> <version>1.0.0</version> <name>Carpet Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.13.RELEASE</spring.version> <mybatis.version>3.2.2</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mysql.connector.version>5.1.34</mysql.connector.version> <commons.fileupload.version>1.2.2</commons.fileupload.version> <hibernate.validator.version>4.3.2.Final</hibernate.validator.version> <jstl.version>1.2</jstl.version> <log4j.version>1.2.17</log4j.version> <aspectj.version>1.6.9</aspectj.version> <fastjson.version>1.2.5</fastjson.version> </properties> <dependencies> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- 文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons.fileupload.version}</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0.1</version> </dependency> <!-- add org.json.jar --> <dependency> <groupId>org.json</groupId> <artifactId>org.json</artifactId> <version>chargebee-1.0</version> </dependency> <!-- <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <!-- springframe start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- springframe end --> <!-- 任务调度定时器 --> <!-- 加入MyBatis 依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 引用插件依赖:MyBatis整合Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MySQL依赖 start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</version> </dependency> <!-- Log4j start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- alibaba JSON --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.validator.version}</version> </dependency> <!-- jsp servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.12</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.19</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> </dependencies> <build> <finalName>Carpet</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>${basedir}/src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <uriEncoding>UTF-8</uriEncoding> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> <path>/${project.artifactId}</path> <server>tomcat7</server> <username>admin</username> <password>aeec657da9</password> </configuration> </plugin> </plugins> </build> </project>
在更改pom.xml
文件之后,更新maven web项目。
要更新web maven项目,请右键单击项目名称(如:webproject
)-> Maven
-> Update Project...
来更新项目。
第5步
查看maven web项目结构,如下图所示 -
第6步
现在,创建一个包:com.yiibai.servlet
,并在此包下创建一个HelloWorldServlet
类,并在其中写入以下代码。文件:HelloWorldServlet.java -
package com.yiibai.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author maxsu */ @WebServlet("/hello") public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.getWriter().write("Hello World! Maven Web Project Example."); } }
第7步
使用以下maven命令构建项目。
mvn clean install
要在Eclipse IDE中执行maven命令,请右键点击项目名称,打开Run
菜单 -> Run Configuration..
以创建新配置。
在运行配置向导中,双击Maven Build
并提供配置信息(Name,Base directory和Goals),如下图所示。
然后点击运行(Run
)并在控制台中输出结果如下 -
第8步
使用以下命令运行嵌入式tomcat服务器。
mvn tomcat7:run
要在Eclipse IDE中执行maven命令,请创建一个新的配置,如下图所示 -
现在运行配置启动tomcat服务器。 控制台输出如下图所示 -
第9步
打开浏览器并在地址栏中输入URL: http://localhost:8080/hello
,得到以下结果 -
原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/maven/create-a-maven-web-project-with-eclipse.html