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
- Open IDEA and create a new project
- Named web_work
- First open IDEA and select File --> Settings --> search maven, you will see the following interface
- Modify the default configuration configuration
2. Create a Maven project
After configuring maven in IDEA, next we use maven to quickly build a JavaWeb project
-
After the project is created, choose to create a module
-
Select to create a maven project
-
Click Next to fill in the project information and
make a modification -
The created project looks like this
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
-
Create a webapp folder in the main directory
-
Select project Structure —> facets —> click the + sign to add web —> select the current project hello_maven
-
Modify path information
-
Modified to our webapp directory
After modification -
After clicking ok, the project becomes a web project, create another index.jsp in the webapp directory, and it will be OK
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.
- 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.
- 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>
- A Maven project is made
groupId
,artifactId
andversion
, 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 |
- 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>
- 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/
5.2) After clicking to enter, you can see the information of each version, select 3.1.0
5. Add plugin
- 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
- 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>
-
Configure tomcat, deploy the project
-
Run the project, access index.jsp by default
-
Visit Servlet
http://localhost:8080/hello_maven/demo01