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
- Download the Maven installation package from the official website
- Decompress the Maven installation package to a directory on your disk
- 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” **
groupId
: Company or organization name in reverse order + project nameartifactId
: Module name (project name)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
- Local repository: The default address of the local warehouse is
Current user directory /.m2/repository
You 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>
- Remote warehouse
- Private server: Built in a LAN environment, for all MAVEN projects within the LAN service
- Central repository: Presumably on the Internet, for all Maven projects around the world
- 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
-
mvn clean
Cleanup command. Delete the generated SRC /target directory
Plugins used:
- Maven-clean-plugin, version 2.5
-
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:
- Maven-resources-plugin, version 2.6: For copying resource files
- Maven-compiler-plugin, version 3.1: for compiling code
-
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
-
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:
- Maven-surefire-plugin, version 2.6
-
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:
- Maven-jar-plugin, version 2.4
-
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:
- 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
-
The name of the Test class is usually Test+ the name of the class to be tested
-
The package name of the test class must be the same as the package name of the class to be tested
-
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
- Test methods must be public methods
- The test method returns no value, that is, void
- The test method has no parameters
- The name of the test method is custom, but the general recommendation is
Test + Test method name
-
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
-
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:
- Compile: default value. Participate in all phases of project construction
- Test: indicates a test. Use only during the test phase, such as execution
mvn test
The command will use junit - 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
- Maven processing
src/main/resources
Directory, all files in the directory are copied totarget/classes
directory - Maven processing
src/main/java
Directory: Only those in the directory are processed.java
Files, put thesejava
Files are compiled into bytecode files and copied totarget/classes
Directory, 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