Mavenx learn to find the right way, get started quickly!

Keywords: Java Maven Apache xml

1,Maven

Why should I learn this technology?

  1. In Java Web development, we need to use a large number of jar packages. We need to import them manually;

  2. How to make a thing automatically help me import and configure this jar package.

    So Maven was born!

1.1 Maven project architecture management tool

At present, it is convenient for us to import jar package!

Maven's core idea: Convention is greater than configuration

  • There are restrictions. Don't violate them.

Maven will stipulate how you should write our Java code. You must follow this specification;

1.2 download and install Maven

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

After downloading, unzip it;

1.3 configure environment variables

In our system environment variables

The configuration is as follows:

  • bin directory under M2 home Maven directory
  • Directory of Maven? Home maven
  • Configure% Maven? Home% \ bin in the path of the system

Test: enter mvn -version in the command line window

Test whether Maven is installed successfully, and ensure that the configuration is completed!

1.4 Alibaba cloud image

  • Mirrors: mirrors
    • Function: accelerate our download
  • Alibaba cloud image is recommended in China
<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>

1.5 local warehouse

In local warehouse, remote warehouse;

Build a local repository

<localRepository>E:\JavaWeb\apache-maven-3.6.3\maven-repo/localRepository>

1.6 using Maven in IDEA

  1. Start IDEA

  2. Create a maven web project

  3. Wait for project initialization to complete

  1. Observe what's in the maven warehouse?

  2. Maven settings in IDEA

    Note: after the IDEA project is created successfully, take a look at Maven's configuration

  1. Here, Maven's configuration and use in IDEA is OK!

1.7 create a common Maven project

Uncheck create from archetype, next step

Auto import

Automatically generate clean maven

1.8 marking folder function

1.9 configure Tomcat in IDEA

If not, click More

Resolve warning issues

Necessary configuration: why do we have this problem: when we visit a website, we need to specify a folder name;

Run once: connection succeeded

1.10 pom file

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>
<!--This is the configuration just now GAV-->
  <groupId>com.wan</groupId>
  <artifactId>JavaWeb-Maven01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--How to package a project
  jar: java application
  war:JavaWeb application-->
  <packaging>war</packaging>
<!--You can delete the following information-->
  <name>JavaWeb-Maven01 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
<!--To configure-->
  <properties>
<!--    Default construction code for project-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
<!--Project dependency-->
  <dependencies>
<!--    Specific projects jar Dependent profile-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>JavaWeb-Maven01</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>

<?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>

  <!--This is what we just configured GAV-->
  <groupId>com.kuang</groupId>
  <artifactId>javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--Package: How to package a project
  jar: java application
  war: JavaWeb application
  -->
  <packaging>war</packaging>


  <!--To configure-->
  <properties>
    <!--Default build code for the project-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--Coding version-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!--Project dependency-->
  <dependencies>
    <!--Specific jar Package profile-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </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>

<?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.wan</groupId>
    <artifactId>JavaWeb-Maven02</artifactId>
    <version>1.0-SNAPSHOT</version>
<!--Add project dependency-->
    <dependencies>
        <!--    Specific projects jar Dependent profile-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>

Because maven's convention is greater than configuration, we can encounter the problem that the configuration file we wrote cannot be exported or effective later

maven resource export problem

Solution:

<!--stay build Medium configuration resources,To prevent the failure of our 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>

1.12 IDEA operation

Generate tree

1.13 solve problems encountered

  1. Maven 3.6.3

    Solution: downgrade to 3.6.2

    If the problem hasn't been solved, continue to downgrade

  2. Tomcat flash back

    Find the catalina.bat configuration file under the bin directory of tomcat and open it with Notepad to see if it can connect to the java environment, or add a pause statement to find the problem

    E:\JavaWeb\apache-tomcat-9.0.33\bin

  3. Configure Maven repeatedly in IDEA

    Do not enter the project, click configure in the lower right corner of IDEA to enter the configuration

    Configure in global default configuration in IDEA

  4. Tomcat cannot be configured in Maven project

  5. The problem of web.xml version in maven default web project

  6. Replace with webapp version 4.0 and tomcat

    <?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"
             metadata-complete="true">
    
    
    
    </web-app>
    
  7. Use of Maven repository (search for related jar packages)

    Address: https://mvnrepository.com/

    Search jar package by:

    import java.io.;
    import javax.servlet.;
    import javax.servlet.http.*;

Source Code for HelloWorld Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

Posted by PupChow on Tue, 21 Apr 2020 04:49:17 -0700