Maven quick start Maven beginner must see Maven basic steps to use Maven in IDEA

Maven

1. Reasons for using Maven

  1. If the project is very large, it is not suitable to use package to divide modules. Maven can split a project into multiple projects
  2. Using Maven, you can save the jar package in the warehouse and take it directly from the warehouse when you use it, without copying and pasting the jar package
  3. All well-known jar packages have been placed in the Maven warehouse, no need to search online
  4. Use Maven to automatically import the jar package that a jar package depends on

Two, Maven introduction

Maven is an open source project management tool of Apache. Whether it is a JavaSE or JavaEE project created in the future, it will be created as a Maven project; each project in Maven is equivalent to an object, one of an object (project) and an object (project) There is a relationship between them; the relationship includes: dependency, inheritance, aggregation, and the use of Maven projects can more easily achieve the effects of importing jar packages and splitting projects

Three, Maven download and installation

  1. Maven download address: http://maven.apache.org/

Insert picture description here

  1. Maven installation

(1) Decompress the downloaded zip to get

Insert picture description here

(2) Modify in settings.xml in the conf directory

① Add in the settings tab

<localRepository>E:\maven_localRepository</localRepository>

The content in the label indicates the location on the disk of the local warehouse storing the jar package (you can create a folder to specify it yourself)

If not specified, the default path of the local repository: C:\Users[Currently logged in user name].m2\repository

② Add to the mirrors tag in the settings tag

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

Indicates that when downloading jar packages from remote warehouses on the Internet, Ali's mirroring is used, and the download speed is very slow using the official address

③ Add to the profiles tab in the settings tab

<profile>
	<!-- 告诉maven我们用jdk1.8 -->
	<id>jdk-1.8</id>
	<!-- 开启JDK的使用 -->
	<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	</activation>
	<properties>
		<!-- 配置编译器信息 -->
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	</properties>
</profile>

When there are multiple JDKs in the operating environment, JDK needs to be specified, which is specified as JDK8 here

  1. Maven environment variable configuration

Insert picture description here

Insert picture description here

After configuration, enter mvn -v in the console to check whether the installation is successful (the jdk version and maven version appear to indicate the installation is successful)

Insert picture description here

Three, the introduction of Maven warehouse

1. Remote warehouse

(1) Central warehouse

Address: https://mvnrepository.com/

The jar package is downloaded from the central warehouse by default when downloading online, as shown in the figure:

Insert picture description here

(2) Central warehouse mirroring

Share the traffic of the central warehouse, the previously configured Ali image is the central warehouse image

(3) Private server warehouse

The internal Maven warehouse built on the intranet for the company’s internal use

2. Local warehouse

The jar package downloaded from the remote warehouse is stored in the local warehouse, and the path has been configured before

3. Warehouse priority

Insert picture description here

Fourth, the introduction of Maven coordinates

Insert picture description here

Five, Maven project type

  1. POM engineering

The POM project is a logical project, used in the parent project or aggregation project, used for version control of the jar package

  1. JAR project

Will be packaged into a jar and used as a jar package, that is, a common local project —> Java Project

  1. WAR project

Will be packaged into war and published on the server project

Six, use Maven in IDEA

  1. Configure Maven in IDEA

Insert picture description here

  1. Create Maven project in IDEA

Insert picture description here

  1. Introduction to the directory structure of the Maven project

src
contains all the source code and resource files of the project, as well as other files related to the project

The
java source code is stored in the src/main/java directory

src/main/resources
stores the main resource files, such as xml configuration files and properties files

src/test/java
stores the classes used for testing. For example, the tests of JUNIT are generally placed under this directory

src/test/resources
can manually create a folder to store resource files for the test environment

pom.xml
is the most important configuration file of Maven, which can configure the relationship between projects and projects, import jar packages, etc.

The target
is at the same level as src, the folder where the compiled content is placed (currently not compiled, so there is no such structure)

production methods:

Insert picture description here

Note: A warning may appear when you click install

Insert picture description here

Solution:
add in pom.xml:

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

Seven, the relationship between the Maven project

1. Dependency

A project development or operation requires B project support, which means A project depends on B project

It is generally understood that the dependency is to import the jar package, and the B project can be the jar package after the install of its own project, or the jar package in the warehouse

Method to inject dependencies: Configure in pom.xml:

Insert picture description here

Process: First find in the local warehouse, if not found, go to the remote warehouse online to find, download to the local warehouse and import, the result:

Insert picture description here

Benefits of dependence:

(1) Programmers do not need to manually add the jar package, just configure it directly in pom.xml, delete the dependent dependency configuration, and the project automatically deletes the corresponding jar package

(2) It can solve the problem of jar package conflict. If the same jar package of different versions is imported, two jar packages will not appear in the project, and one of them will be automatically retained

① The characteristics and principles of dependence

(1) Features

Maven project 1 depends on jar1, Maven project 2 depends on Maven project 1, then jar1 will also be imported into Maven project 2, and jar1 dependent on project 1 can be used directly

The method of project 1 dependent on project 2 is the same as the method of relying on jar package, just configure the coordinates after project install

(2) Principle

i. The shortest path first principle

For example, suppose the dependency between A, B, and C is A->B->C->D(2.0) and A->E->(D1.0), then D(1.0) will be used because The path from A through E to D is shorter
ii. First declare the principle of priority

On the premise that the length of the dependency path is equal, the order of the dependency declaration in pom.xml determines who will be parsed and used, and the dependency with the highest order is used first

② Eliminate dependence

A depends on B, B depends on C, C depends on mybatis.jar, excludes mybatis.jar in C from A

<!-- 项目A的pom.xml中编辑 -->
<dependencies>
    <!-- 依赖项目C -->
    <dependency>
        <groupId>com.qizegao</groupId>
        <artifactId>MavenDemo</artifactId>
        <version>1.0-SNAPSHOT</version>

        <!-- <exclusions>中可以使用多个<exclusion> -->
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <!-- 不写版本号 -->
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

③ Scope of dependence

The scope of dependency determines when the dependent jar package is valid and invalid

Dependent scope usage:

Insert picture description here

依赖范围分类:

compile
This is the default scope, if not specified, the dependency scope will be used, which means that the dependency takes effect at both compile and runtime

The provided
dependency is required when compiling and testing the project, but it does not take effect when running the project

runtime
does not need to take effect at compile time, but only takes effect at runtime

The system
dependency scope is similar to provided. When using the system scope dependency, the path of the dependent file must be explicitly specified through the systemPath tag.

test is
only needed when compiling the test code and running the test, the normal operation of the project does not require such dependencies

import
import must be declared in the tag in the pom.xml configuration file to specify the version number of the jar package

2. Inheritance

Declare the parent project as a POM project, and the child project will import the jar package that the parent project depends on by default
(use the dependencyManagement tag to manage all jar packages and control the version of the jar package)

<groupId>com.qizegao</groupId>
<artifactId>ParentMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 打包类型,默认是jar -->
<packaging>pom</packaging>

<!-- 管理所有jar包,使用dependencyManagement标签表示不会引入这些jar包,仅管理 -->
<!-- 使用properties标签和${}控制版本 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <!-- 使用properties标签中的版本号 -->
            <version>${name}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<properties>
    <!-- 此标签可任意起名 -->
    <name>3.5.6</name>
</properties>

Declare a subproject to inherit from the above project

<!-- parent标签指定父工程 -->
<parent>
    <groupId>com.qizegao</groupId>
    <artifactId>ParentMaven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 以下标签声明父工程的pom.xml文件所在的位置 -->
    <relativePath>../ParentMaven/pom.xml</relativePath>
</parent>

<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <!-- 无需声明版本号,继承父工程的版本号,父工程可以控制子工程的版本号 -->
        <!-- 子工程可以使用version标签指定版本号而不使用父工程的版本号 -->
    </dependency>
</dependencies>

Note: If the parent project uses the import dependency range, the subproject must use the version number specified by the parent project

3. Aggregation

When the developed project has multiple modules (each module is an independent project), you need to use the aggregate project

The total project of the aggregation project (the parent project at the top level) must be a POM project, and the rest of the modules are included by it. Use tags in the parent project to indicate the included submodules:

Eight, Maven's commonly used plugins

1. Resource Copy Plugin

When Maven is packaging, by default, only the configuration files in src/main/resources are copied to the project and packaged. The configuration files in this directory will not be added to the project when packaging. Use this plugin to package the specified files

Configure the resources to be packaged in pom.xml:

<build>
    <resources>
        <resource>
            <!-- 要打包的资源位置 -->
            <directory>src/main/java</directory>
            <includes>
                <!-- 要打包的资源 -->
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <!-- 要打包的资源位置 -->
            <directory>src/main/resources</directory>
            <includes>
                <!-- 要打包的资源 -->
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

2. Tomcat plugin

No need to rely on external Tomcat server, Maven provides tomcat plugin

(1) Create a web project

Insert picture description here
Insert picture description here

Structure after creation (missing directories such as java, resources, etc. can be created manually):

Insert picture description here

 注意:创建项目时如果出现警告:
		   No archetype found in remote catalog. Defaulting to internal catalo

Solution:
Insert picture description here

(2) Configure the Tomcat plugin in the pom.xml file

<build>
  <plugins>
    <!-- 配置Tomcat插件 -->
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <!-- 配置Tomcat监听端口 -->
        <port>8080</port>
        <!-- 配置项目的访问路径(Application Context) -->
        <path>/</path>
      </configuration>
    </plugin>
  </plugins>

Insert picture description here

Operation result:
Enter in the address bar: http://localhost:8080/MyProject
successfully accessed the index.jsp page
(modify the encoding format on the jsp page to UTF-8 to prevent messy codes)

This indicates that the server started successfully
Insert picture description here

Nine, Maven's common commands

Maven commands can be directly clicked on the right side of the above icon to execute

  • install
    Local installation, including compilation, packaging, and installation to a local warehouse

Compile-javac command
Package-jar command, package the java code into a jar file
Install to the local warehouse-save the packaged jar file to the local warehouse

  • clean
    clears the compiled information, deletes the target directory in the project, and deletes the packaged jar file

  • compile
    only compiles, equivalent to javac command

  • package
    , including two functions of compilation and packaging

The difference between install and package:

The package command completed the project compilation, unit testing, and packaging functions, but did not deploy the executable jar package (war package or other form of package) to the local maven warehouse and remote maven private server warehouse

The install command completes the project compilation, unit testing, and packaging functions. At the same time, the executable jar package (war package or other form of package) is deployed to the local maven warehouse, but not to the remote maven private server warehouse.

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/110009147