Maven's installation, configuration, command and detailed teaching

Posted by webtuto on Mon, 10 Jan 2022 05:40:01 +0100

Why should we learn maven?

Before maven was born, we had a lot of inconvenience in making the project.

1: jar packages are hard to find

2: jar package dependency

3: Jars are inconvenient to manage (manage jar s according to different functions)

4: Project compilation

Introduction to Maven

Maven is an open source project of the Apache Software Foundation and an excellent project construction tool,
It is used to help developers manage the jars in the project, the dependencies between jars, and complete the compilation, testing, packaging and publishing of the project.

Concepts in Maven

Pom(Project Object Model)
There is a POM in the root directory of projects managed by Maven XML file. pom. The XML file indicates how Maven works.

In POM Configure the project basic information and project construction information in the XML file. For example, project coordinates, project dependent jar s, plug-ins, compilation options, etc.
Once in POM If the dependent jar is configured in the XML file, Maven will automatically download the corresponding component from the component warehouse.

Project coordinates

maven defines a unique flag for each jar. This is called the project coordinate in maven. Through this coordinate, you can find any version of jar package you need.

The combination of groupId, artifactId, packaging and version is called the coordinates of the project. They form the unique identification of the project,

Maven uses coordinates to accurately position components. groupId, artifactId and version are required, and the values of these three items must be unique,

packaging is optional (jar by default).

Warehouse

The central warehouse is shared globally. First download the jar from the central warehouse to the local warehouse, and then reference the jar of the local warehouse in the project

Maven development environment construction

Premise: install jdk

Maven needs to download various plug-ins through the network to complete the task, so it should ensure the smoothness of the network.

1 download maven server

Link: https://pan.baidu.com/s/1EpcuXM_Xk4BfLiGp604hEg
Extraction code: xryy

2. Install and configure maven

Click to download the binary apache-maven-3.5.2-bin Zip, and then configure the environment variable in the system environment and the bin directory in the path. After the configuration is successful, enter the command mvn -version

The view version is shown in the figure below

Configure local warehouse address

Next, according to the situation, create a folder in the local disk where the jar files downloaded through maven are stored, which is generally called "repository";

Finally, how to store the downloaded jar file in our designated warehouse? You need to find the settings in the conf folder in the file extracted by maven's server Modify the XML file

As shown in the figure below

Replace part of Figure 5

Replace part of Figure 5 and configure Alibaba cloud maven image warehouse to download faster

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

Next, configure it in our development tool

Create maven project



Ensure smooth network during downloading

Project catalog description

Pom.xml configuration

maven warehouse official website( http://mvnrepository.com/ )As shown in the figure below: you can download the required development jar package on this official website and continue to click the link to open it, as shown in the figure below

For example, download the servlet jar package
In POM XML file to configure jar package information, and use maven to automatically download jar packages
Manage dependencies between jar s

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
 <!--File upload and download component-->
    <dependencies>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
    <span class="token comment">&lt;!--Configure project dependencies jar Package address--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.google.code.gson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
  <span class="token comment">&lt;!-- Build name--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>gson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
       <span class="token comment">&lt;!-- Version number--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.8.5<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>javax.servlet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>javax.servlet-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.1.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>provided<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span><span class="token comment">&lt;!--this jar Packages are only used when editing local tests,Will not be packaged into the project--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

Functional explanation of each sub element

< groupid >, < artifactid >, and < version >: the base coordinates of the dependency
< type >: the packaging type of the dependency, which is jar by default;
< scope >: the scope of the dependency. The default is compile. The values and meanings are as follows:
Compile: compile time and run-time dependencies. If your project is finally packaged as a war package, the dependent package will be copied to the lib directory in the war package.
Provided: this dependency is used when compiling the project main code and test code, and the runtime is provided by jre or web container. If your project is finally packaged as a war package, the jar package will not be packaged.

Maven command

Maven common commands

1. compile

2. clean delete target

3. package to local

4. install the project to the local warehouse

Add jar to maven repository

Some jar packages may be used in the development process, but these jar packages do not exist in maven warehouse. We need to add them to our local warehouse so that maven can be used to manage this jar package

command

MVN install: install file dgroupid = group name (com.taobao) -DartifactId = build name (druid) -Dversion = Version (1.0) -Dpackaging = package type (jar) - dfile = jar package full path

Execute the following command on the command line and prompt build success to install as

Topics: Java JavaEE Maven jar intellij-idea