❤ "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
      <mirror>
              <id>alimaven</id>
              <mirrorOf>central</mirrorOf>
              <name>aliyun maven</name>
              <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
      </mirror>
      
      <mirror>
              <id>nexus-aliyun</id>
              <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
              <name>Nexus aliyun</name>
              <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
      

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

File->Settings

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

start-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">
  <modelVersion>4.0.0</modelVersion>
<!--Previously configured GAV-->
  <groupId>com.hxl</groupId>
  <artifactId>javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
<!--Package: Packaging method of the project
  jar: java application
  war: javaWeb application-->
  <packaging>war</packaging>

  <name>javaweb-01-maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

<!--  to configure-->
  <properties>
<!--    Default build code for the project-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--    Code version, which can be modified to 1.8-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

<!--  Project dependency-->
  <dependencies>
<!--    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-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

<!--  Things for project construction-->
  <build>
    <finalName>javaweb-01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

  • 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-->  
    <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
    </build>
    

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 -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
          <!-- This is a scope. We need to delete it-->
          <scope>provided</scope>
      </dependency>
      

      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.

      <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
          </dependency>
      

      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 {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //Type of response html
            resp.setContentType("text/html");
            //This place is to set the code, because it may become garbled after entering the browser
            resp.setCharacterEncoding("utf-8");
            //Gets the output stream of the response
            PrintWriter out = resp.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Hello World!</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>How do you do</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    
        @Override
        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" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
    <!--  web.xml Is to configure us web Core application of-->
    <!--  register Servet-->
      <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.hxl.HelloServlet</servlet-class>
      </servlet>
    <!--  One Servlet Corresponding to one Mapping: mapping-->
      <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
    <!--    Request path-->
        <url-pattern>/hxl</url-pattern>
      </servlet-mapping>
    </web-app>
    

    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