美文网首页
创建Maven构建的web项目

创建Maven构建的web项目

作者: 红烧排骨饭 | 来源:发表于2017-04-26 15:47 被阅读0次

MAVEN的安装与更换中央仓库的源

下载

访问 http://maven.apache.org/download.cgi 选择 apache-maven-3.5.0-bin.zip 下载

maven.png

安装

将下载的下来的文件解压到 D:\apache-maven-3.5.0

加入环境变量

新建 M2_HOME,填写 D:\apache-maven-3.5.0
然后修改 Path,加入 ;%M2_HOME%\bin

修改中央仓库源

D:\apache-maven-3.5.0\conf\settings.xml 文件放到 %USER_HOME%\.m2 文件夹中,如果不存在改文件夹就创建一个

修改其中的 <mirrors> 元素,加入

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>

配置 Eclipse 的 maven

选择 Window - Preferences - Maven - Installations,添加刚才安装好的 maven,并勾选

maven.png

然后确保配置文件指向我们刚刚创建的 settings.xml 文件

maven_setting.png

配置 Eclipse 的 JDK 环境

选择 Window - Preferences - Java - Installed JRES,然后勾选 JDK,没有就创建一个

JDK.png

打开 Eclipse 的 Problems 视图

在 Eclipse 中创建 Maven 项目时,会遇到一些错误,从 Problems 视图可以看到具体的错误信息,所以就需要打开它

选择 Window - Show View - Other - Problems

Problems.png

maven 项目的创建

创建 maven 项目时会遇到的问题

按照老套路来创建 maven 项目:File - New - Maven Project,然后选择 maven-archetype-webapp 创建。在 Eclipse 中会出问题,会出现莫名其妙的红叉,而且还不好解决。

archetype-webapp.png

所以,我换了一种方式:创建一个普通的 maven 项目,然后再转换成 web 项目。

创建 Maven 项目的步骤

选择 File - New - Maven Project,勾上 Create a simple project(skip archetype selection)

NewMavenProject.png

点击 next,填写 groupId 和 artifactId。Packaging 不要改,依然是 jar,后面再改

groupId_artifactId.png

右键项目 - Properties - Project Facets,点击 Convert to faced form...

Convert.png

勾上这三个选项,注意一下他们的 version

ProjectFacets.png

点击 Further configuration available...

FurtherCfg.png

填写信息和勾上选项

webModule.png

接在再修改编译文件的目标路径,改成如下的就行

target.png

然后修改部署设置,测试文件不需要部署,remove 掉即可

deploy.png

修改 pom.xml

修改 packaging 的方式为 war;加入 maven-compiler-plugin 插件,并指定 sourcetarget 的版本;指定编码为UTF-8

<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/xsd/maven-4.0.0.xsd">
  
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.okada</groupId>
    <artifactId>mavendemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
  
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

保存之后,在 Problems 视图会提示有错误

problems.png

按照提示,按下 ALT+F5,刷新一下项目即可消除错误

添加依赖

添加几个依赖:

  • junit
  • servlet
  • jsp
  • jstl
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <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>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

添加 tomcat7 插件

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
</plugin>

测试项目是否搭建成功

新建一个 Servlet

package com.okada.mavendemo;

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;

@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

然后右键项目,选择 run as - Maven build

Goals 中填写 tomcat7:run

Paste_Image.png

就可以看到控制台的信息,然后访问 http://localhost:8080/mavendemo/HelloWorldServlet,可以看到正常输出就说明项目搭建成功了

使用 IDEA 创建 maven 构建的 web 项目

上面是使用 Eclipse 来创建项目,如果用 IDEA,步骤不同

设置 Maven

先进入 Default settins,找到 maven 选项,然后选择 maven 的路径和设置 settings.xml 和 仓库的位置

IDEA_Maven.png

创建项目

如图所示,选择 Maven 标签,选择 JDK,然后选择 archetype 为 web-app

New_Project.png

填写 groupId 和 artifactId

groupIdArtifactId.png

选择下一步,确认 maven 的版本

maven.png

选择下一步,确认项目存放的路径

dir.png

然后点击ok,现在项目就创建好了,目前的项目文件夹结构如图

project_structure.png

还不是一个标准的 maven 的文件夹结构,需要补上缺失的文件夹

补全文件夹

一个标准的 maven 的文件夹结构是这样的

src
 ├─main
 │  ├─java
 │  ├─resources
 │  └─webapp
 └─test
     ├─java
     └─resources

选择 File - Project Settings,选择 Modules 标签,如图所示创建 src/main/java 文件夹

new_folder.png

并将它设置为 sources

java_sources.png

同样的操作,创建 src/test/java,并设置为 Test,创建 src/test/resources,并将它设置为 Test resources

确认 Facets 的信息

如图确认 web.xml 和 web resources 的路径是否正确

Facets.png

设置 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.owen</groupId>
  <artifactId>demo</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <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>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</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.2</version>
        <configuration>
          <path>/${project.artifactId}</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

修改 web.xml

按照 servlet 3.0 的规范来修改 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
</web-app>

测试是否创建成功

新建一个 servlet

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/hellMaven")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(new Date());
        req.setAttribute("currentTime",currentTime);
        req.getRequestDispatcher("/WEB-INF/view/hello.jsp").forward(req,resp);
    }
}

新建一个 jsp 文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Hello</h1>
<h2>当前时间:${currentTime}</h2>
</body>
</html>

运行项目

点击右上角

edit_cfg.png

然后选择 + 创建一个配置

maven_cfg.png

为配置去个名字,同时填写信息,点击ok

commond.png

右上角就会出现一个刚刚配置好的配置项,点击 run 就能运行项目了

run.png

参考

相关文章

网友评论

      本文标题:创建Maven构建的web项目

      本文链接:https://www.haomeiwen.com/subject/wfavzttx.html