❤ "An article teaches you how to solve maven" from environment to application -- it's very worth reading ❤ ️

Keywords: Java IntelliJ IDEA Maven

This article is mainly about maven from environment configuration to being used in IDEA. Basically, the problems encountered are also mentioned. If not mentioned, welcome to communicate

In Java Web development, we need to manually import a large number of jar packages, and maven helps us import and configure this jar package,

1.maven project architecture management tool

We currently use it to import jar packages.

Core idea: agreement is greater than configuration. (don't break the restriction)

Maven will specify how to write our java code, and must follow this specification;

2. Download and install Maven

Official website: https://maven.apache.org/

Unzip bin

3. Configure environment variables

Configure the following configuration in the system environment variable

  • M2_HOME: bin directory under maven directory
  • MAVEN_HOME: directory of maven
  • Configure% Maven in the path of the system_ HOME%\bin

Add in path

Enter mvn -version in cmd and your maven version will be configured

4. Alibaba cloud image

Click to open the setting.xml file in conf to find mirrors

  • Mirroring: mirrors

    • Function: accelerate our download

    • Alibaba cloud image is recommended in China

    • //Place the following in the position shown in the illustration
              <name>aliyun maven</name>
              <name>Nexus aliyun</name>

5. Local warehouse

If there is a local warehouse, there is a remote warehouse.

Set up a local repository: localRepository

The default is the directory under default. You can create a folder for storage

6. Use Maven in idea

6.1 start IDEA

6.2 create a Maven project

sdk: select the java installation directory

Check yes to use Maven's template

The following place is the project name and storage location. It's best to add the project name - otherwise too many will look bad

finish, and then there will be this in the lower right corner. Click enable auto import, which is the automatic import package. Alicloud must download before importing. Otherwise, it will be downloaded through foreign links, which will be very slow

6.3 wait until the project initialization is completed.

6.4 observe changes in maven warehouse

6.5 Maven settings in idea


Note: there is often a problem in idea, that is, MavenHome will use the default idea after the project is automatically created. At this time, it needs to be improved in time.

6.6 when this page appears, Maven project is ok

7. Create a normal Maven project

  • Note that if you add a webapp here, you only need to right-click Add framework support to create it

This is only available in web applications

8. Mark folder

There are two ways to mark

The above target is java class

9. Configure Tomcat in IDEA

  • Resolve warning issues:

The reason why this problem occurs is that when we visit a website, we need to specify the name of a folder

After completing the above steps, Tomcat in the IDEA will light up


The content accessed here is the content in the default index.jsp

  • maven sidebar

clean will clear the target on the left

10.pom file

For the new version of idea 2020.X, automatic import cannot be set. After we modify the pom file, there is a small pattern at the top of the back, which is m refreshed in the lower right corner. Click it.

pom.xml is Maven's core configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!--Maven Version and header files-->
<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">
<!--Previously configured GAV-->
<!--Package: Packaging method of the project
  jar: java application
  war: javaWeb application-->

  <name>javaweb-01-maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->

<!--  to configure-->
<!--    Default build code for the project-->
<!--    Code version, which can be modified to 1.8-->

<!--  Project dependency-->
<!--    Specific dependent jar Package configuration file-->
<!--    Maven The high level of is that he will help you import this jar Other functions on which the package depends jar package-->

<!--  Things for project construction-->
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->

  • Maven's agreement is greater than the configuration. We may encounter the problem that the configuration file we wrote cannot be exported or take effect later. Solution:

    <!-- stay build Medium configuration resources,To prevent the failure of resource export-->  

11. Operation in idea

12. Solve problems encountered

  • Maven 3.8.2 red explosion

    • We found that in the setting.xml file, we configured the local warehouse and alicloud image. After seeing that there is no problem, we directly modify the environment variable
    • Lower the version. The position to be changed is M2_HOME and MAVEN_HOME
  • Tomcat flash back

    • Click start.bat and click on it. When we look down, we will see some configurations of the java environment. Errors will be caused due to the wrong environment
    • You can add a pause before the last: end
  • MAVEN should be configured repeatedly every time in the IDEA

    Configure in the global default configuration in IDEA.

    After configuring in this place, it will be like this every time in the future.

  • Tomcat cannot be configured in Maven project

  • The web.xml version in Maven's default web project is too old

    We can see the path below

    Take a look at the version in webapp and make it consistent with tomcat

  • Use of Maven warehouse

    • Maven warehouse address: https://mvnrepository.com/

    • Search in the search bar; If you can't find it, check some others. If you can't find it, think about it. This is started with Tomcat. Do you go to Tomcat to find the relevant ones, then roughly lock them, and then search Maven. Generally, the most used ones are correct. Click here to download the jar package and see Maven's dependencies

    • Taking HttpServlet as an example, we found that it was popular. We found the servlet in Tomcat and then went to Maven warehouse

      <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
          <!-- This is a scope. We need to delete it-->

      There was something missing when I went in again. Let's download another JSP API. When we go in and see which one to download, we will see that one is updated in 2011, and we choose the one updated in 2018.


      Just go in again.

  • Let's create a page next

    package com.hxl;
    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;
    public class HelloServlet extends HttpServlet {
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //Type of response html
            //This place is to set the code, because it may become garbled after entering the browser
            //Gets the output stream of the response
            PrintWriter out = resp.getWriter();
            out.println("<title>Hello World!</title>");
            out.println("<h1>How do you do</h1>");
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
      <display-name>Archetype Created Web Application</display-name>
    <!--  web.xml Is to configure us web Core application of-->
    <!--  register Servet-->
    <!--  One Servlet Corresponding to one Mapping: mapping-->
    <!--    Request path-->

    header.html is a test. You can write some content at will.

    • In this way, after starting tomcat, enter localhost:8080 and enter index.jsp. Entering localhost:8080/header.html is the content entered into header.html; Then enter localhost:8080/hxl, which is the content in our HelloServlet.

Posted by einamiga on Fri, 08 Oct 2021 17:31:46 -0700