Project build tool: Maven (on)

Maven

Introduction

maven is a tool for project construction and dependency management based on the java platform.

Rely on warehouse address

https://mvnrepository.com/tags/maven

Four characteristics of maven

Dependency management system

Maven introduces the dependency management system jar package management to modify the configuration file when jar is upgraded.
The three attributes of dependency : groupld, artifactld, and version These three attributes form Coordination (coordinates) to uniquely identify a dependency.
Any project built on Maven itself must also define these three attributes. The generated can be a jar package or a war package. A typical dependency reference is as follows:

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

groupld: indicates that the Meven project belongs to the actual project-company name

artifactId: This element defines a Maven module in the actual project-project name

version: This element defines the version of the Maven project

The coordinate attribute
maven coordinates introduces order for various components, and any component must define its own coordinates.

Features of Maven :

  1. Consistent project structure: Solve the problem of inconsistent file directories caused by different IDEs (for example, Idea's maven project can also be imported into eclipce, because the project structure is unified).
  2. Consistent construction model and plug-in mechanism: the introduction of plug-ins (such as: Tomcat is needed, then the Tomcat plug-in can be imported)

Maven's philosophy : convention is greater than configuration!

Maven installation and configuration

Download maven

The address will not be posted, just go to the official website to download it.

Configure environment variables
Insert picture description here

After decompression, configure the root directory of Maven to the system environment variable MAVEN_HOME, and configure the bin directory to the path environment variable.

Environment variables in path:
Insert picture description here

Note: The directory stored after decompression in maven should not contain Chinese characters and spaces

Check the Maven environment: enter mvn -v in the dos window to
Insert picture description here
modify the configuration file to modify the mirror

  1. Create a maven-repo folder in the maven directory to use as a local maven warehouse to store the jar package downloaded by maven
F:\apache-maven-3.6.1\maven-repo
  1. Open the configuration file under the maven path: maven directory/conf/settings.xml
  2. Add local warehouse location configuration: as follows
路径是刚刚创建的那个本仓库路径
<localRepository>F:\apache-maven-3.6.1\maven-repo</localRepository>
  1. In the mirrors tab, replace the mirror with Ali's mirror to speed up the dependency download, because maven uses a foreign central warehouse by default, which is very slow:
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

Pom.xml tag explanation :

modelversion: specifies the version of the current pom model. For Maven2 and Maven3, it can only be 4.0.0

groupId: As the name implies, it defines which group the project belongs to. This group is often associated with the organization and company where the project is located, such as com.xxxx

artifactId: defines the unique Id of the current Maven project in the group

version: xxx-Milestone
For example: 1.0.0-SNAPSHOT The
first x: major version has major changes.
Second x: minor version fixes bugs and adds features.
Third x: updates

Milestone version:
SNAPSHOT (development version)
alpha (internal test)
beta (public test)
Release | Rc (release version)
GA (normal version)

Common commands

  • mvn -version: Display version information
  • mvn clean: clean up temporary files generated by the project, generally the target directory under the module
  • mvn compile: compile the source code, generally compile the sec/main/java directory under the module
  • mvn package: project packaging tool, which will generate jar or war files in the target directory under the module
  • mvn test: test command, or execute the junit test case under src/test/java/
  • mvn install: Copy the packaged jar/war file to your local warehouse for use by other modules
  • mvn site: The website that generates project-related information
  • mvn eclipse:eclipse: convert the project into an Eclipse project
  • mvn dependency:tree: print out the entire dependency tree of the project

IDEA integrated Maven environment

Set Maven version

Insert picture description here
Creation of Maven's Web project

  1. Create maven webapp project
    Insert picture description here
  2. Project gav:
    Insert picture description here
  3. The initial project structure is shown in the figure
    Insert picture description here
  4. Modify the pom.xml configuration file
  • Modify the jdk version : the default is 1.7, modified to 1.8 as follows:
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  • Junit can be deleted if it is not needed
  • Delete the pluginManagement tag and all content in the tag
  1. The
    first step of Maven integration Tomcat :
    <plugins>
   			集成Tomcat9插件
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <!--访问端口-->
              <port>8080</port>
              <!--对外访问路径-->
              <path>/mavenDemo</path>
              <uriEncoding>UTF-8</uriEncoding>
              <server>tomcat7</server>
            </configuration>
          </plugin>
          
    </plugins>

Step 2:
1.
Insert picture description here
2.
Insert picture description here

Maven warehouse concept

Maven warehouses are divided into two categories: local warehouses and remote warehouses .

When Maven searches for components based on coordinates, it will first check the local warehouse and use it directly. If the local warehouse does not exist, it will search and download from the remote warehouse to the local warehouse. If there is neither the local warehouse nor the remote warehouse, Maven will report an error. .

There are three types of remote warehouses: central warehouse, private server, and other public warehouses.

In the default configuration, Maven uses the central warehouse to download jar packages.

Private server is another special kind of remote warehouse, in order to save bandwidth and time, set up a private warehouse server in the local area network, and use this warehouse to proxy all external remote warehouses. Internal projects can also be uploaded to the private server for other projects to use

If you do not configure the local warehouse folder yourself, the default local warehouse path is: ${User}/.m2/repository/. It is recommended to modify the configuration to the local warehouse folder created by yourself. The configuration method has been mentioned before.

Guess you like

Origin blog.csdn.net/qq_40492885/article/details/115348181