IDEA中配置Maven


一、IDEA 集成 Maven

File---->Settings:設置 maven 安裝主目錄、maven 的 settings.xml 文件和本地倉庫所在位置

還可以繼續設置,maven項目創建時,會聯網下載模版文件,比較大, 使用-DarchetypeCatalog=internal,不用下載, 創建maven項目速度快

當然了,為了之后的項目也可以,繼續這樣設置

這個步驟和上面的類似

二、IDEA 創建 Maven 版 java 工程

1. 創建 maven 版 java 工程

File-->New-->Module

選擇使用的工程模型:maven-archetype-quickstart : 普通的java項目

2. 填寫 maven 工程的坐標

3. 填寫工程名和存儲路徑

然后下一步

這就創建了

4. 創建成功

pom.xml里面的信息

<?xml version="1.0" encoding="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>

  <!--自己項目的坐標(gav)-->
  <groupId>com.md</groupId>
  <artifactId>01-javase-maven</artifactId>
  <version>1.0-SNAPSHOT</version>


  <!--構建項目的配置-->
  <properties>
    <!--maven構建項目使用的utf-8,-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!--編譯java代碼使用的jdk版本-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <!--java項目運行在什么jdk版本上,這兩個一致,看你自己的jdk版本-->
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>


  <!-- 依賴,默認加了單元測試的依賴-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>



  </dependencies>


  <!--插件 build 目前可以不要,后面會用-->

</project>

5. 創建約定的目錄結構

6. 創建測試類

7. Maven的窗口

三、IDEA 創建 Maven 版 web 工程

1. 創建 Maven 版 web 工程

File-->New-->Module

選擇使用的工程模型:maven-archetype-webapp : web工程

剩下的就和創建java的一樣了,這里就不展示了

2. 創建后視圖

顯然,按照 maven archetype 原型創建的 maven web 工程缺少 maven 項目的完整結構:src-main-java / resources,src-test-java/resources,所以需要我們手動添加文件目錄

3. 創建缺省文件夾

src-main-java / resources,src-test-java/resources

注意:我們創建的是普通目錄,可以這樣變成我們需要的目錄


由於這樣比較麻煩,也可以一次性

4. 把文件夾標識為源碼文件夾

File -> Project Structure, 選擇Modules:右邊找到java這層機構,在上面有個“Mask as”, 點下Sources, 表示這里面是源代碼類。

5. pom.xml 添加依賴

主要就是servlet依賴和jsp依賴

<?xml version="1.0" encoding="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.md</groupId>
  <artifactId>02-web-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--加入servlet依賴,servlet的jar-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!--jsp依賴,jsp相關的jar加入進來-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>


</project>

6. 編寫測試類 Servlet


里面寫如下信息:

package com.md.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author MD
 * @create 2020-08-04 11:39
 */
public class HelloServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

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

        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write("Hello Maven Web");
        pw.flush();
        pw.close();
    }
}

還有就是對應的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>
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.md.controller.HelloServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

7. 配置Tomcat





基本上和Java Web的時候配置是一樣的

8. 編寫對應的jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/hello" >訪問servlet</a>
</body>
</html>


然后運行Tomcat,看到上面的就是成功了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM