Eclipse构建Maven项目的完整过程–普通web项目

  • Post author:
  • Post category:其他

进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件)

一、Maven项目的新建

1、鼠标右键—->New—–>Other…

 2、直接点击下一步

 3、选中 maven-archetype-webapp 后点击下一步

4、 Group Id 中输入项目的基本包名。

      Artifact Id 中输入项目名。

      Version 中的值默认就行,不进行选择。

      Package 中写的是默认生成的一个包名,不写也可以。

   接着点击完成就可以了。

 

5、创建好项目后结构如下:

 

 ——web.xml 的版本是2.3的,是老版本

  ——新生成的工程默认的 jdk 和 compiler 都是1.5 的

  ——而且没有src/main/java 这样结构的 maven 目录

二、Maven项目的配置

1、修改web.xml

由于Maven中的 maven-archetype-webapp 样板项目的 web.xml 是2.3版本的,我们需要替换成新版本:

可以在Eclipse中新建一个web工程,将它 WebContent/WEB-INF 目录下的 web.xml 文件复制替换掉我们的 Maven 项目中 src/main/webapp/WEB-INF/ 目录下的web.xml 文件。

原来的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>

替换后的web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         id="WebApp_ID" version="3.1">
  <display-name>mydemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 pom.xml 里面build 标签下添加 maven编译插件

 <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

3、添加缺少的目录并指定输出路径

Maven规定,必须创建以下几个包,并且分别对应相应的输出路径

  src/main/resources

  src/main/java

  src/test/resources

  src/test/java

Eclipse构建Maven项目的完整过程–普通web项目_NEWS_VERSION的博客-CSDN博客_eclipse创建maven项目