IDEA creates Maven-Alibaba Cloud image, incomplete directory structure, 404 after Tomcat starts


The first time I used Maven in IDEA, I spent a lot of time in various configurations and environments, hereby record! !

The complete directory structure:
Insert picture description here

One, Maven installation and configuration environment variables

Official download link: https://archive.apache.org/dist/maven/maven-3/

1. It is recommended not to choose the latest download. When I used it for the first time, it was Maven 3.6.3+IDEA 2020.3. As a result, I couldn’t use it all the time. I kept stuck in the Alibaba Cloud mirror. Then I chose Maven 3.3.9+IDEA 2019.3 (generally not IDEA 2020 and Maven 3.6.3 should both work, if it fails, lower the version). When downloading Maven, select bin-zip and unzip it.
Insert picture description here

2. After decompression, find a place to create a new folder (as a local warehouse, place various jar packages), and then go to the maven directory -conf-setting.xml for configuration.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!-- 下面这行代码需要自己添加,表明自己本地仓库的位置(实际上不加也可以,IDEA也能改)-->
  <localRepository>F:\Environment\repository</localRepository>

The following is an Alibaba Cloud image. It should be placed in the mirrors tag of setting.xml . Others such as Huawei Cloud are also available. However, the latest requirement is to use https. However, I tried many methods and failed, so I chose to use http , And then go to IDEA to ignore the security protocol.

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

3. Configure system environment variables. Here, the difference between M2_HOME and MAVEN_HOME is the maven version. Previously, Maven 1 used MAVEN_HOME, and later maven 2 and 3 were M2_HOME. During the insurance period, both are added.
Insert picture description here

Then you need to add the bin directory of maven to the PATH in order to use the maven command in cmd.
Insert picture description here

Either way, just choose one and add it. The second one is the relative path is more flexible. If the second one fails in the later verification, the first absolute path is used.

4. Verify, win+R, enter cmd, and then enter the command mvn -v. If the output is successful, the maven configuration is successful.
Insert picture description here

Two, IDEA creates a Maven project

1. IDEA is mainly used here to create JavaWeb. Generally, multiple modules are used, so do not use templates at the beginning. Always go next first.

(If you choose maven during the process, you can first use IDEA's built-in, or choose your own, and the default settings will be set after entering)
Insert picture description here

2. Make the default settings for the new project
Insert picture description here

Choose your own Maven and warehouse and configuration files
Insert picture description here

Select your own JDK and check the automatic import package, and then fill in the following commands in the VM options for importer (ignore the security protocol, otherwise the previous Alibaba Cloud image cannot be used).

-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true

Insert picture description here

**Note:** It is possible that the mirror still cannot be used after entering this command. Pay attention to what the error is. It is possible to use it by lowering the version. It is recommended to change to the version I use, Maven 3.3.9+IDEA 2019.3, This is guaranteed to be available, if you have any questions, you can private me.

3. Because the web template is not used, the directory structure is incomplete. At the same time, we need to create a sub-template, first delete the src directory, and then create a new template.
Insert picture description here
Insert picture description here

Generally speaking, if you choose a web template, there will be a webapp directory, but even if I use the template, there is nothing, so all the directory structure needs to be created by myself.
Insert picture description here

**Note:** After the template is created, the following error may occur, so just ignore it.
Insert picture description here

4. Create directories src, test, webapp and other directories by yourself.

  1. Right-click the sub-template and select new-directory (if you don't need the sub-template, right-click the project directory), and create several necessary directories.

Insert picture description here

  1. Then Ctrl+Shift+Alt+S, open the project structure, select the submodule to add Web. Create directories and files under these two paths first. Then click Create Aritifact in the lower right corner.

Insert picture description here

  1. The target is created for the submodule, and the subsequent generated packages are all in this file

Insert picture description here

  1. Modify the output directory of the module (this compilation output is inherited from the parent project and can be changed by yourself)

Insert picture description here

5. Modify the output directory of the project (out is used as the output of the entire project, and target is used as the output of the module, so you need to create the out directory first, and then delete the target of the project)

Insert picture description here

Insert picture description here

Note: The output location of artifacts needs to be changed, and it needs to be changed to the target.

Insert picture description here

5. For testing, first fill in the contents of the two maven pom.xml and web.xml, and then create an index.jsp and TestServlet.java.

1613031303796

pom.xml (untitled, for the entire project)

<?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>org.example</groupId>
    <artifactId>untitled</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>untitled1</module>
    </modules>
    <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>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

TestServlet.java

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

public class TestServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("Servlet");
    }

    @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>测试</h1>
</body>
</html>

Three, configure Tomcat

1. Add local Tomcat
Insert picture description here

2. Set local Tomcat and other parameters and add artifacts

Insert picture description here

3. Deployment, the red circle below is what we need to add when we enter the url in the browser.

Insert picture description here

4. Start the project and enter the URL of the servlet
Insert picture description here

Insert picture description here

Fourth, the problems of creating a good Maven project

1. When creating a Maven project, the following problems may occur: The plug-in is not downloaded because the mirroring is wrong, etc.

Insert picture description here

Report 8 errors:

Cannot resolve plugin org.apache.maven.plugins:maven-clean-plugin:2.5
Cannot resolve plugin org.apache.maven.plugins:maven-resources-plugin:2.6
Cannot resolve plugin org.apache.maven.plugins:maven-jar-plugin:2.4
Cannot resolve plugin org.apache.maven.plugins:maven-compiler-plugin:3.1
Cannot resolve plugin org.apache.maven.plugins:maven-surefire-plugin:2.12.4
Cannot resolve plugin org.apache.maven.plugins:maven-install-plugin:2.4
Cannot resolve plugin org.apache.maven.plugins:maven-deploy-plugin:2.7
Cannot resolve plugin org.apache.maven.plugins:maven-site-plugin:3.3

Solution: Open cmd, enter mvn help:system, check whether it can be downloaded successfully, until SUCCESS BUILD appears
Insert picture description here

If it succeeds, go back to the project to reload and refresh, and try all three refreshes.
Insert picture description here

If it fails, take a look at the last error and copy it to the browser translation. If there is an error such as unable to connect to Alibaba Cloud and unable to find verification, it is caused by https. First, see if the command in the place of maven-import is correct. Input, if the input is still wrong, it may be that the mirror is invalid, or it may be a problem with the version of maven and idea.

Guess you like

Origin blog.csdn.net/qq_39763246/article/details/113791089