Maven is a project management tool that contains a project object model (POM: Project Object Model, a set of standards, a Project Lifecycle, a Dependency Management System, And the logic used to define the plug-in goal in the lifecycle phase.

Maven can help us manage jar packages. Maven can also compile our Java files into class files. Maven can execute our unit test files in the specified directory and tell us if there is an exception

When Maven manages our JARS, it will find the corresponding JARS according to the coordinates of the JARS. These JARS are in the repository. If there are many projects, so many projects can use the same jar package, and this jar package is placed in the repository, which is Maven dependency management.

To use Maven, first download and install Maven

The Maven project does not have a JAR package. The Maven project will find the jar package in the repository according to the coordinates of the jar package. The Maven project will find the JAR package in the Settings. ${user. Home} / m2 / repository, which is the default is in the system tray to find local repository, just download good Maven local warehouse is no jar package, if it is connected to the case, Mavaen will go to the central warehouse to download, here put almost all of the open source jar package, If there is no network that is cool, so we will create a remote warehouse in the company, namely private servers, as long as their computers in a LAN and the remote warehouse, if the remote repository without the jars we need, it will download the corresponding jar from a central repository package, also can be obtained by means of local upload jars. We found that our local repository was in disk C, and it was a hidden folder, which was very inconvenient, so we usually created a local repository by ourselves. For example, I created a repository folder under Maven in disk D. We just need to configure settings. XML in the Config directory of the Maven directory as shown below

The following is to change the remote warehouse to Ali warehouse, so that the local download JAR package is faster, modify the configuration file as follows

Maven’s directory structure includes the core code section, the configuration file section, the test code section, and the test configuration file.

SRC /main/ Java/directory core code part

SRC /main/resources section of the configuration file

The SRC /test/ Java directory tests the code part

SRC /test/resources tests the configuration file

SRC /main/webapps Page resources, HTML, CSS, JS, etc

Common commands in Maven

MVN clean Deletes the target directory

SRC /main SRC /main SRC /main SRC /main

MVN test compile, here not only execute compile function, also compile the code under SRC /test

MVN package, which performs all the functions of test and forms a WAR package

MVN install not only performs all the functionality of the package, but also installs the package into a local repository

MVN deploy publishes, not only performs all the features of Install, but also publishes the project

In the Maven command above, clean is the clean life cycle, and the remaining five are the default life cycle, which is a one-click build.

In the POM. XML file of our project development, the first is the information of the project itself, the JAR package on which the project runs, and the information of the project running environment, which is our project object Model (POM).

Maven jar package coordinates, which are configured in pop.xml

<dependency>// Coordinates of the dependent JAR package<groupId>mysql</groupId>// Company or organization domain name + project name<artifactId>mysql-connector-java</artifactId>/ / module name<version>5.1.48</version>// Jar package version number</dependency>
Copy the code

Here is my pop.xml file configured with Mybatis, mysql, logging, and unit tests


      
<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.zhiying</groupId>
    <artifactId>mymaven</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>
Copy the code