Introduction to Maven

 

 

1. What is Maven

The project construction tool abstracts the construction life cycle, and no longer needs to define the construction process. Like a simple maven install command, it corresponds to compiling, testing, packaging and other actions

The dependency management tool can accurately locate each component (artifact) through a coordinate system (groupId, artifactId, version), that is, you can locate any jar file according to a coordinate

Maven provides a free central library for the whole world, and most of the popular open source class libraries can be found in this central library.

 

Contrast: Ant

Ant (Another Neat Tool), meaning "another neat tool"

Ant uses XML to define build scripts, there is a build.xml file, and the build process is defined in build.xml (specifying compilation, packaging, etc.)

Ant requires developers to explicitly specify each target and the tasks that need to be performed to complete the target, and write it again for each project

Ant has no dependency management and needs to manage dependencies manually, which is very fatal, especially in projects involving multiple people.

 

2. maven installation (Windows environment)

1. Download address: http://maven.apache.org/download.cgi

You can download apache-maven-3.5.3-bin.zip and unzip it. The location after I unzip it is: G:\study\tools\apache-maven-3.5.3

2. Environment variable configuration

Create a new variable named M2_HOME with a value of G:\study\tools\apache-maven-3.5.3

 

Then add in the path: %M2_HOME%\bin;

After confirmation, go to the command window to confirm whether the installation is successful: mvn -v

 

 Three. maven installation directory analysis

 

1. bin

Contains scripts run by mvn

 

2. conf

Contains a very important file: settings.xml

settings.xml is the global configuration file for the current maven environment

Under normal circumstances, we will copy the settings.xml to the ~/m2/ directory (~/ represents the user directory), and then modify the settings.xml in this directory to control the maven configuration in the user scope

 For example, my directory is: C:\Users\conglin\.m2

3. ~/.m2/ directory, this directory is not the installation directory, after we install maven, any mvn command that involves downloading jar (or other dependent components) will generate /.m2 in the user directory by default

Where /.m2\repository/ is the dependency component downloaded from the remote library to the local when our component project is stored

 

 Four. maven project core file - pom.xml

 pom (Project Object Model), which defines the basic information of the project, is used to describe how the project is built and declare project dependencies.

The following is the pom.xml when I create a Spring boot project demo, where <!-- --> is the remark I add to describe the meaning of the tag

<? xml version="1.0" encoding="UTF-8" ?> 
<!-- xml tag specifies the xml document version and encoding --> 
<!-- project is the root element of pom.xml --> 
< 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 specifies the version of the current POM model, for maven2 and maven3, Value can only be 4.0.0 --> 
    < modelVersion > 4.0.0 </ modelVersion >

    <!-- groupId, artifactId, version These three elements define the basic coordinates of the current project
        in,
        groupId defines which group the project belongs to. This group is often associated with the organization or company where the project is located.
        artifactId defines the unique id of the current project group in the group
        version indicates the version of the current project, and SNAPSHOT indicates that the current project is still under development, which is an unstable snapshot version
    
    --> 
    < groupId > com.example </ groupId > 
    < artifactId > fuck </ artifactId > 
    < version > 0.0.1-SNAPSHOT </ version > 
    <!-- packaging specifies the packaging type, and defaults to jar if not specified -- > 
    < packaging > jar </ packaging >

    <!-- is a more readable name for the project, not required --> 
    < name > fuck </ name > 
    < description > Demo project for Spring Boot </ description >

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

In fact, after we figured out the meaning of maven's existence, we can completely understand the unfamiliar tags in pom.xml through real-time online query data.

As long as it is clear that various labels are ultimately reflected in several purposes: packaging instructions, dependency instructions, project instructions, etc.

 

5. Warehouse - repository

Maven can store the components shared by all maven projects in a unified location, and this unified location is the repository.

For maven, there are only two types of repositories: local repositories and remote repositories. When maven looks for components based on coordinates, it first queries the local warehouse, and then queries the remote warehouse when the local warehouse is not available, and an error will be reported when both are not available.

1. Local warehouse

In a Windows environment, the local repository usually defaults to ~\.m\repository\

The current user's directory is often in the C drive. When the C drive hard disk capacity is not enough, we can set it in the localRepository tag of setting.xml

Such as

<localRepository>D:\maven\repo\</localRepository>

 

2. Remote warehouse

Remote warehouses are often subdivided into three categories: central warehouses, private servers, and other public remote warehouses

Among them, almost all companies will have private servers!

The details of the private server can be found in the [Nexus maven private server] which will be published later.

 

六. mvn clean install

When we package the project, the most commonly used command is

mvn clean install

However, what happened behind mvn clean install?

When executing the mvn clean install command, it is equivalent to executing the following commands in sequence in the background:

mvn clean compile
mvn clean test
mvn clean package
mvn clean install 
//Note: The above commands are in order from top to bottom. When the command below is executed, the action of the above command is automatically invoked; clean specifies the /target directory of the cleanup project, and clean is not a necessary parameter

That is, when the mvn clean install command is executed, the compilation, testing, and packaging are successively performed, and finally the packaged project is installed into the local warehouse for use by other maven projects.

So when we are asked about maven's packaging command, it can be said to be mvn install, but strictly speaking it should be mvn package

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325316412&siteId=291194637