如何将 Maven Java Project 转换为 Maven Web Project
前言
开发工具采用 eclipse ,具体如何创建 Maven Java Project 这里不在重复。
一、实现步骤
1、更改 pom.xml packing模式为 war ,并且新建 WEB-INF 文件夹,并且在web app 下新建 index.html 文件
2、在 pom.xml 中添加 Tomcat 配置与相关依赖
2.1、添加相关依赖
<!-- 添加 servlet 配置 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2.2、添加Tomcat 配置
<!-- Tomcat 配置 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>8888</port>
<path>/first</path>
</configuration>
</plugin>
2.3、附上完整 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>yishan</groupId>
<artifactId>maven_hiber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven_hiber Maven Webapp</name>
<url>http://maven.apache.org</url>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate 配置 dependency begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.0.GA</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- Hibernate 配置 dependency end -->
<!-- 添加 servlet 配置 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>maven_hiber</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Tomcat 配置 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>8888</port>
<path>/first</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、添加相关配置文件
3.1、在 web app 文件夹下,新建 index.html
3.2、通过 eclipse 新建 Helloservlet (eclipse 会自动生成与配置 web.xml 文件)
4、在工程项目右键,选择 Run As -> Maven build…,在弹出对话框输入,tomcat:run,运行
4.1、出现以下内容,代表 tomcat 运行成功
4.2、如果运行不成功,很可能是由于 端口被占用导致,打开 CMD ,输入 netstat -ano | findstr 8888 查询出占用此端口的进程 PID ,通过 taskkill /F /PID 15836 结束掉此进程
5、打开浏览器,输入对应地址 http://localhost:8888/first/HelloServlet,出现以下结果,代表配置正确