Zhang SAN recently did a project, but this project requires more than 30 JAR packages. He can only download from the Internet one by one, download a manual import.

Finally, it turned out that some jar versions were not compatible, and it didn’t work out for a day. It’s really “Tofu and curd – it’s getting confusing! .”

After more than a month of crazy overtime work, the project was finally completed, Zhang SAN deployed the test environment and then handed over to the tester Li Si.

Zhang SAN thinks can touch fish well finally, who knows Li Si works especially seriously, come out for a while a pile bug.

Zhang SAN can only immediately to change the bug, after the manual compilation, packaging project. Most of the time a day packed more than ten times, can zhang SAN tired out.

So how to avoid the above scenario? Maven makes its debut.

1. Introduction of Maven

Maven is an automated build tool from the Apache Software Foundation that focuses on project building and dependency management for the Java platform.

Simply put, Maven is a useful tool. We used to have to go online to find jar packages for our projects. With Maven, we simply configure the dependent coordinates and it will automatically download the JAR packages to a directory on our computer’s hard drive.

It helps us do a lot more than manage JAR packages:

  • 1. Automatically downloads required JAR packages and manages the versions of JAR packages and their dependencies.
  • 2. Help you compile the program
  • 3. Help you package your apps
  • 4. Help you deploy the program
  • 5. Help you manage the relationship between each module in the project

Noun explanation:

Dependencies: For example, if our project requires servlet.jar, the JAR package can be called a dependency, or the project depends on servlet.jar. When we import A.jar, we find that we also need to import B.jar, indicating that A.jar depends on B.jar.

Project construction: Project construction describes the process of compiling, running, packaging, and deploying code.

  • 1. Project cleanup: Clean up previously compiled code.
  • 2. Compilation: Compile the program source code into executable code for the virtual machine, that is, a class file. Maven can help you compile a bunch of code in bulk.
  • 3. Test: Maven can perform test code to verify that your functionality is correct.
  • 4. Packaging: Put all class files, configuration files and other resources into a compressed file. For Java programs, the compressed file is xx.jar, and for Web applications, the compressed file extension is xx.war.
  • 5. Installation: Install jar files or WAR files into the local repository.
  • 6. Deployment: The program will be deployed to the server, we can access through the website.

2. Maven installation and configuration

2.1 Download and Installation

Note: Maven is developed in Java, so you must install and configure the JDK first.

Maven’s official website:

https://archive.apache.org/dist/maven/maven-3/
Copy the code

Decompress the downloaded package to a non-Chinese directory without Spaces:

2.2 Configuring Environment Variables

1. Add variables

Variable name: MAVEN_HOME

Variable value: Installation path of Maven

2. Modify the path

Increment variable value: %MAVEN_HOME%\bin

3. Verify

Press the Window +R key, enter CMD, and press Enter. Then run the MVN -v command

4. Modify the configuration of the local repository

Why modify the local repository?

Maven’s default repository is in the C/ user /.m2 folder. As more jars are downloaded, the system disk becomes more full and the computer becomes more and more jammed. Therefore, it is recommended to build a repository outside of the C drive.

Open Maven’s core configuration file settings.xml

Add the following labels:

<localRepository> Your repository directory </localRepository>Copy the code

Such as:

<localRepository>D:\CodeSoftware\Maven\repository</localRepository>
Copy the code

5. Set an address for downloading ali Cloud images

Because foreign remote warehouse download JAR package speed is slow, domestic Ali cloud download speed is fast.

To the setting. XML file add:

 <mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>;  
    <mirrorOf>central</mirrorOf>          
  </mirror>
Copy the code

Complete the setting. The XML

<? xml version="1.0" encoding="UTF-8"? > <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"> 
      
       D:\CodeSoftware\maven\repository
       
       
        
        
         alimaven
         
        
         aliyun  maven
         
        
         http://maven.aliyun.com/nexus/content/groups/public/
         
        
         central
         
        
       Copy the code

3. Use Maven in IDEA

Configure Maven in 3.1 IDEA

File – > Settings:

3.2 Creating a JAVA Project

File -> New -> Project

3.3 Creating a WEB Project

File -> New -> Project

Here we need to use Maven’s own Web project template.

Because the project directory created using Maven’s built-in Web project template is incomplete, you need to create the Java and Resources directories under the main directory.

3.4 Using Maven

After configuring the Maven tool in IDEA, the Maven button will appear on the right side of the IDEA window:

Maven window ICONS are used for:

4. Core concepts of Maven

4.1 Directory Structure

Directory structure specified in Maven:

  • 1. SMS: the project name, which is also the root path of the project
  • 2. SRC: source code for the project
  • 3. The main program
  • 4. Java: Project source code
  • 5. Rsources: configuration file of the main program
  • 6. Test: Test code
  • 7. Rsources: configuration file to test the code
  • Pom.xml: Maven project configuration file

4.2 pom file

Pom: Project Object Model.

Maven abstractions the structure and content of a project into a model, declared and described in the POM.xml file. So pom.xml is at the heart of Maven.

The complete POM.xml file:


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

  <parent>
    <artifactId>spring-boot-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.2.5. RELEASE</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xxl</groupId>
  <artifactId>sms</artifactId>
  <version>1.0 the SNAPSHOT</version>

  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <modules>
    <module></module>
  </modules>

  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
    </plugins>
  </build>
</project>
Copy the code
  • 1. ModelVersion: specifies the Maven modelVersion. Default configuration.
  • 2. GroupId: the ID of a company or organization, usually the reverse of the company’s domain name, such as com.Alibaba. Or company domain + project name spelled backwards, e.g. Com.alibaba. Taotao.
  • 3. ArtifactId: project name.
  • 4. Version: Indicates the version number of the project. It is usually identified by a three-digit number, for example, 2.2.5. If the project is still under development, SNAPSHOT is usually added after the release. If the project is stable and has been released, a RELEASE is usually added after the RELEASE.
  • 5. Parent: POM configuration used to declare the parent project to inherit.
  • Packaging: Type of project packaging, jar by default. General Java programs are packaged as JARS and Web programs as wars.
  • 7. Properties: Used to define configuration in the project, such as encoding format, etc.
  • In the Maven project, we need jars called dependencies. We configure dependencies in the Dependencies TAB, and the configuration of dependencies is represented by coordinates. Coordinates are the combination of groupId, artifactId, and Version.
  • Modules: In Maven multi-module development, set the relationship between this module and other modules.
  • 10. Build: Build mainly contains configuration related to the build of the project, such as setting the JDK version to compile the plug-in.

4.3 depend on

In the Maven project, we need jar packages called dependencies. After we configure the dependencies TAB in the Dependencies TAB, Maven automatically searches the local repository for the dependencies and imports them into our project.

If not, Maven downloads the JAR package from the central repository to the local repository based on the dependent coordinates.

Dependencies need to be written in the Dependency tag, which contains groupId, artifactId, and Version. Such as:

<dependencies>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.27</version>
  </dependency>
</dependencies>
Copy the code

A dependency can be used as a JAR id. Maven will use this ID to find the jar in the repository directory. If you can’t find it, go to the central repository and download the jar to the repository folder on your computer and import it into the project.

4.4 Scope of Dependency

When configuring a dependency, there is also a scope for the dependency:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.27</version>
  <scope>compile</scope>
</dependency>
Copy the code

Dependencies: compile, test, and provided. The default value is compile.

For example, junit relies on a scope called Test and only participates in tests. The servlet depends on the provided scope and does not participate in packaging and deployment because the Tomcat server already contains the JAR package for the servlet.

4.5 coordinates

Maven manages any JAR package as an item in the repository, represented by coordinates of three vectors. Coordinates represent unique Maven projects in the repository.

Coordinates are the combination of groupId, artifactId, and Version. GroupId, artifactId, and Version determine the path of the project in the repository, and artifactId and Version determine the name of the JAR package.

4.6 warehouse

Maven automatically loads the jars into the project by configuring the coordinates of dependencies in the POP.xml file. Where does Maven get the JARS from? Maven’s repository.

We all know that a repository is used for storing things, but what is a Maven repository used for? The jar package.

Maven repositories are divided into local and remote repositories based on where they are stored.

Local repository: The jars are stored in a folder on your computer. Maven puts the jars in the.m2\repository directory on drive C by default.

Remote warehouse: remote warehouse is divided into central warehouse, central warehouse mirror, private server.

The central repository contains all common JAR packages, and all programmers can network to get jar packages from the central repository. The central repository is maven’s default remote repository and is the most authoritative.

Official website address of central Warehouse:

https://mvnrepository.com/
Copy the code

All the dependencies we need can be found here:

Mirror of the central warehouse: Each continent, Asia, Europe, etc., has several servers that share traffic for the central warehouse. Ease access to the central warehouse. Users in a continent first access the mirror server in their continent. The Maven repository image of Ali Cloud configured above belongs to the central repository image.

Private server: a server deployed on a LOCAL area network (LAN).

4.7 Maven Life cycle

Maven’s life cycle: The Maven build project process, cleaning, compiling, testing, reporting, packaging, installation, and deployment.

5. Maven common Settings

5.1 Defining global Variables

We can define global variables in Properties and then use ${variable name} to configure the dependent coordinates uniformly. Such as:

<properties>
<mysql.version>8.0.27</mysql.version>
</properties>

<dependencies>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.version}</version>
  </dependency>
</dependencies>
Copy the code

5.2 Specifying resource Locations

All Java files in the SRC directory are compiled in the comile and test-comiple phases, respectively, but other files in these directories are ignored. If you want to include other files in the SRC directory as part of the jar package, you need to specify the resource file location.

We need to specify the location of that resource in the Build tag:

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory><! -- Where is the directory -->
            <includes><! Properties,.xml files in the directory will be scanned -->
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>
Copy the code

6. Maven manages multi-module applications

Sometimes we divide a project into multiple modules. For example, the Common module mainly contains some tool classes and constant information, the Service module mainly deals with business, and the Web module mainly acts as a controller.

Maven is very handy for maintaining relationships between different modules.

6.1 Creating multi-module Projects using Maven

Create the Maven parent project SMS

File -> new -> Module -> Maven

Delete the SRC directory of the parent project and change the poM file packaging mode to POM

Create subproject SMS-Web

File -> new -> Module -> Maven

Create a subproject smS-service

File -> new -> Module -> Maven

Create a subproject SMS-DAO

File -> new -> Module -> Maven

Create the subproject SMS-Model

File -> new -> Module -> Maven

Create the subproject SMS-common

File -> new -> Module -> Maven

Pom file of parent project:

6.2 Importing Dependencies

1. Dependencies between modules:

We all know that dependencies are passed. For example, module B introduces module A, and module C introduces module B, which is the same as module C introduces module A.

In the project we know that service depends on DAO and controller depends on Service. Dao, Service, and Controller all rely on the Model entity class, and DAO and Service all need common. But it is not possible to add Model dependencies in all three of them, and common dependencies in both dao and Service.

Depending on the transitivity of dependencies, we can add model dependencies in the Common module, Common dependencies in the DAO, DAO dependencies in the Service, and Service dependencies in the Controller.

SMS – common pom. XML:

SMS – dao pom. XML:

SMS – service of pom. XML:

SMS – web pom. XML:

After being packaged and installed with Maven tools, modules can call each other.

2. Third-party dependencies:

We typically use the dependencyManagement tag in the parent project’s POM.xml to manage the versions of dependencies. If you use this dependency in a submodule, just add it to the Dependencies tag.

The dependencyManagement tag does not really reference a dependency, it just specifies the version of the dependency for us to manage.

If the SMS-Web module needs to use commons-io dependencies, we simply add the coordinates of the dependencies dependency in its PM.xml file.

Because the parent module uses the properties tag to specify the dependent version, there is no need to add the version number to the child module, which facilitates unified management.