The detailed steps of creating a maven project in IDEA are very clear

Preface

To create a Maven project in IDEA, the premise is that the Maven environment has been installed and configured.
If you have not yet configured and installed Maven, please download and install it first. For how to download and install, please refer to my other article:
Maven Download and Installation Tutorial
This tutorial is based on the creation of a servlet-based JavaWeb project as an example. For the Spring series framework, the relevant dependencies of the pom.xml file need to be adjusted.

1. Configure Maven in IDEA

  1. Open IDEA and create a new project
    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-JJPg8P8t-1614244226788)(..\02_图片\17.jpg)]
  2. Named web_work
    Insert picture description here
  3. First open IDEA and select File --> Settings --> search maven, you will see the following interface
    Insert picture description here
  4. Modify the default configuration configuration
    Insert picture description here

2. Create a Maven project

After configuring maven in IDEA, next we use maven to quickly build a JavaWeb project

  1. After the project is created, choose to create a moduleInsert picture description here

  2. Select to create a maven project
    Insert picture description here

  3. Click Next to fill in the project information and
    Insert picture description here
    make a modificationInsert picture description here

  4. The created project looks like this
    Insert picture description here
    Maven directory description:

src/main/java 		 —— 存放项目的.java 文件 
src/main/resources 	 —— 存放项目资源文件,如数据库的配置文件 
src/test/java 		—— 存放所有单元测试.java 文件,如 JUnit 测试类 
target 			    —— 项目输出位置,编译后的class 文件会输出到此目录 
pom.xml              ——maven 项目核心配置文件 

3. Maven engineering transformation

The currently created maven project is an ordinary Java project, not a web project, we need to make a transformation

  1. Create a webapp folder in the main directoryInsert picture description here

  2. Select project Structure —> facets —> click the + sign to add web —> select the current project hello_maven
    Insert picture description here

  3. Modify path informationInsert picture description here

  4. Modified to our webapp directory
    After modificationInsert picture description here

  5. After clicking ok, the project becomes a web project, create another index.jsp in the webapp directory, and it will be OK
    Insert picture description here

4. Pom core configuration file

A maven project has a pom.xml file, through which the pom.xml file defines project information, project dependencies, import plug-ins, and so on.

  1. Create a Servlet, lack of a jar package and report an error. To solve the problem, put the servlet-api-xxx.jar package in. As a maven project, you should add the servlet coordinates to import its jar.
    Insert picture description here
  2. Introduce the coordinates of dependent packages in the pom.xml file
<?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.lagou</groupId>
    <artifactId>hello_maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>
  1. A Maven project is made groupId, artifactIdand version, when we refer to other third-party libraries, but also as a unique identifier determined by these three variables.
  • The concept of coordinates

    • The coordinates in maven are to locate a unique jar package.
    • The maven world has a large number of builds. We need to find a unified specification that uniquely identifies a build. With a unified specification, the search can be handed over to the machine.
  • The main components of Maven coordinates (GAV)-determine the location of a jar on the Internet

label meaning
groupId Define the current Maven organization name, usually the company name
artifactId Define the actual project name
version Define the current version of the current project
packaging Package type
jar: execute package and it will be marked as jar package war: execute package and it will be marked as war package
dependency Use <dependency>After declaring a dependency, Maven will automatically download the dependent packages
  1. Maven's dependency management is a unified management of the jar packages that the project depends on.
label meaning
dependencies Express dependencies
dependency Use <dependency>After declaring a dependency, Maven will automatically download the dependent packages
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>	
  1. The source method of coordinates
    Adding dependencies requires specifying the coordinates of the dependent jar package, but in many cases we don’t know the coordinates of the jar package. You can query it in the following way:
    search from the website.

5.1) Enter the URL, enter the URL, and query

https://mvnrepository.com/

Insert picture description here5.2) After clicking to enter, you can see the information of each version, select 3.1.0

Insert picture description hereInsert picture description here

5. Add plugin

  1. Add compile plugin, set jdk compile version

This tutorial uses jdk11, and you need to set the compilation version to 11. Here you need to use the maven plug-in to set
it. Add the following configuration to the pom:

	<!-- properties 是全局设置,可以设置整个maven项目的编译器 JDK版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 重点  -->
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <!-- 在build中 我们需要指定一下项目的JDK编译版本,maven默认使用1.5版本进行编译
    注意 build 与 dependencies是平级关系,标签不要写错位置  -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

6. Run the Maven project

  1. Improve the project code
    ServletDemo01.java
@WebServlet("/demo01")
public class ServletDemo01 extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    

        System.out.println("hello maven!!!!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req, resp);
}
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>这是我的第一个maven工程!</h1>
</body>
</html>
  1. Configure tomcat, deploy the projectInsert picture description hereInsert picture description here

  2. Run the project, access index.jsp by default
    Insert picture description here

  3. Visit Servlet

http://localhost:8080/hello_maven/demo01

Guess you like

Origin blog.csdn.net/u012660464/article/details/114093066