What is Maven

Maven is generally a build tool, but it is more than just a build tool, so it is not a replacement for Ant, another Java project build tool

Download and install Maven

  1. Download the Maven installation package from the official website
  2. Decompress the Maven installation package to a directory on your disk
  3. Configure maven environment variables

Maven Core Concepts

The convention directory structure

In the root directory of a Maven project there is the following agreed directory structure

├─ ├─ test.txt // ├─ test.txt // ├─ test.txt // ├─ test.txt // ├─ test.txt // ├─ test.txt ├─ Java // ├─ resources // ├─ Java // ├─ resources //Copy the code

Pom. The XML file

Project Object Model (POM) : Project Object Model. The pom.xml file is the one we touch most during development

Three vectors locate a Maven project

Gav is groupId+artifactId+version three labels for short, also known as coordinates, says that according to the three labels can know the basic information of the project, namely ** “which company does this project belong to”, “what is the name of this project”, “what is the current version of this project” **

  1. groupId: Company or organization name in reverse order + project name
  2. artifactId: Module name (project name)
  3. version: Version number (major version number · Minor version number · Minor version number)
<groupId>com.lkj</groupId>
<artifactId>mvn-project</artifactId>
<version>1.0</version>
Copy the code

File path mapped to disk: MAVEN_REPO/com/ LKJ /mvn-project/1.0/mvn-project-1.0.jar

warehouse

  1. Local repository: The default address of the local warehouse isCurrent user directory /.m2/repositoryYou can customize the local repository address by modifying maven’s configuration file. Maven configuration file address:The maven directory you installed /conf/settings.xml. Modified contents are as follows:<localRepository> the address of the localRepository that you want to set </localRepository>
  2. Remote warehouse
    1. Private server: Built in a LAN environment, for all MAVEN projects within the LAN service
    2. Central repository: Presumably on the Internet, for all Maven projects around the world
    3. Central warehouse mirror: it is built to share the flow of central warehouse and improve the access speed of users. The most familiar one is the mirror of Ali Cloud

Rely on

Dependency search: first search the local repository, the local repository does not have the corresponding dependency from the remote repository to the local repository

Maven’s life cycle

The life cycle

Maven refers to the stages that Maven goes through during the build process, including the clean, Compile, Test, Report, Package, Install, deploy, and so on

The plug-in

Maven projects are built using Maven commands. Maven commands are executed using plugins, each of which is a JAR package. For example, MVN clean is executed using maven-clean-plugin

The command

Different maven functions, such as compile, can be performed using MVN compile

  1. mvn clean

    Cleanup command. Delete the generated SRC /target directory

    Plugins used:

    1. Maven-clean-plugin, version 2.5
  2. mvn compile

    Compile the command. Perform the compilation of the source code (Java files in the SRC /main/ Java directory) and generate the compiled class files to the SRC /target/classes directory. In addition, the resource files used (SRC /main/resources) are copied to the SRC /target/classes directory, copying the resource files first and then the class files

    Plugins used:

    1. Maven-resources-plugin, version 2.6: For copying resource files
    2. Maven-compiler-plugin, version 3.1: for compiling code
  3. mvn test-compile

    Compile the test command. In addition to the MVN compile function, you compile the test code (Java test files in the SRC /test/ Java directory) and generate the compiled class files to the SRC /target/test-classes directory. In addition, the resource files used (SRC /test/resources) are copied to SRC /target/test-classes, and then the class files

  4. mvn test

    Test the command. Run the MVN test-compile command to generate the class file and then test the corresponding class file

    Plugins used:

    1. Maven-surefire-plugin, version 2.6
  5. mvn package

    Package command. Before packaging, the code is compiled, the resources are copied, and the class file is tested. Finally, after these operations are successful, the resources, class files, and configuration files in the project are put into a compressed file. The default compressed file type is JAR, and the Web application is war (of course, You can also do this via the pom.xml file). The packaged file contains all generated classes and configuration files in the SRC /main directory, regardless of test, because the packaged JAR (WAR) file has passed the test and is no longer needed.

    Plugins used:

    1. Maven-jar-plugin, version 2.4
  6. mvn install

    Install command. Install the generated packaged files (such as JAR files) into your local Maven repository. Of course, during the execution of this command, the MVN package command is automatically executed first.

    Plugins used:

    1. Maven-install-plugin, version 2.4

The following are the screenshots of the preceding commands on the local terminal

Custom configuration plug-in

Configure the plug-in in the POM.xml file as follows

<! -- Configuration related to project build -->
<build>
  <! -- Plug-in configuration -->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <source>1.8</source> <! -- Specify the JDK version of the compiled code -->
        <target>1.8</target> <! -- Specify the JDK version to run the Java program -->
      </configuration>
    </plugin>
  </plugins>
</build>
Copy the code

Unit testing

In Java, a unit is a method, and a method is the smallest unit of testing

Take junit test framework as an example to learn about maven’s life cycle, plug-ins, commands and other related knowledge

Unit test usage tips:

Create the test class Java file in the SRC /test/ Java directory

  1. The name of the Test class is usually Test+ the name of the class to be tested

  2. The package name of the test class must be the same as the package name of the class to be tested

  3. Define the methods in the test class and write the test code in the methods, but be aware of the requirements when defining the test methods

    1. Test methods must be public methods
    2. The test method returns no value, that is, void
    3. The test method has no parameters
    4. The name of the test method is custom, but the general recommendation isTest + Test method name
  4. The methods in the test class can be executed separately, and the test class itself can be executed separately. When the test class itself is executed independently, all test methods of the test class are executed

  5. Add the @test annotation to the Test method to indicate that it is a Test method

// src/main/java/com/lkj
package com.lkj;

public class HelloMaven {
    public int sum(int a, int b) {
        returna + b; }}Copy the code
// src/test/java/com/lkj
package com.lkj;

import org.junit.Assert;
import org.junit.Test;

public class TestHelloMaven {
    @Test
    public void testSum(a){
        HelloMaven helloMaven = new HelloMaven();
        int sum = helloMaven.sum(2.3);
        // Expected value, actual value
        Assert.assertEquals(5, sum); }}Copy the code

IDEA uses Maven

IDEA comes with Maven, but we usually need to use maven installed by us, so we need to configure Maven in IDEA

Dependency scope management

We often add project-dependent JARS to the Maven configuration file pom.xml, but some jars are not needed during certain phases of the project’s build, such as the unit test jar junit, which is only useful during the test phase and not during other phases, such as the “deploy” phase. Therefore, we can set up the scope tag to allow for different JAR packages to be used at different stages of project construction.

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
		<! Use the jar package junit only in the test phase -->		    
    <scope>test</scope>
  </dependency>
</dependencies>
Copy the code

Value of scope:

  1. Compile: default value. Participate in all phases of project construction
  2. Test: indicates a test. Use only during the test phase, such as executionmvn testThe command will use junit
  3. Provided: the provider. When a project is deployed to the server, it does not need to provide this dependent JAR package, but the server itself. The servlet and JSP dependencies in a Web project built with Tomcat are obvious. Because the Tomcat server already provides these dependencies, the scope of the two dependencies in the POM.xml file will be provided

Commonly used Settings

When maven builds a project, it will have some default configurations. Then it will read the configuration file pom.xml and merge the configuration and default configurations in the pom.xml file to generate the final configuration file. Finally, it will build the project based on the final configuration.

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<! The JDK version used by Maven when compiling Java source code  
  <maven.compiler.source>1.7</maven.compiler.source>
  <! -- Maven JDK version used when running Java bytecode -->  
  <maven.compiler.target>1.7</maven.compiler.target>
</properties>
Copy the code

Configure custom variables

If a project built using Maven is large and depends on many JARS, some of which have the same version number, it can be cumbersome to change the version number of these jars manually, one by one. In this case, we can replace the version number with a custom tag in the Properties tag, with the tag name as the variable name and the tag body as the variable value

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <! The spring. Version tag represents the spring version. The version number is 5.3.10.
  <spring.version>5.3.10</spring.version>
  <! The junit. Version tag indicates the junit version. The version number is 4.11.
  <junit.version>4.11</junit.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <! -- Use a custom spring version -->
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <! -- Use custom junit version -->
    <version>${junit.version}</version>
    <scope>test</scope>
  </dependency>
</dependencies>
Copy the code

Configuring resource plug-ins

By default, Maven handles files as follows during the build process

  1. Maven processingsrc/main/resourcesDirectory, all files in the directory are copied totarget/classesdirectory
  2. Maven processingsrc/main/javaDirectory: Only those in the directory are processed.javaFiles, put thesejavaFiles are compiled into bytecode files and copied totarget/classesDirectory, no other files are processed

However, in some cases we need to handle non-.java files in the SRC /main/ Java directory, so you can do this in the Maven configuration file pom.xml

<build>
  <! SRC /main/ Java to target/class
  <resources>
    <resource>
      <! -- Directory to work with -->
      <directory>src/main/java</directory>
      <includes>
        <! The.properties and.xml files in the directory are scanned.
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
      <! -- Filtering set to false disables filters, and *.properties is enabled -->
      <filtering>false</filtering>
    </resource>
  </resources>
</build>
Copy the code

The resources

  • Maven Tutorial