Maven Foundation

Maven introduction

What is Maven

Maven's correct pronunciation is[ ˈ mev ə n] , not "horse plague" and other plagues. Maven is a colloquial word in the United States, representing experts and experts.

A more formal definition of Maven is as follows: Maven is a project management tool, which includes a project object model (POM), a set of standards, a project lifecycle and a dependency management system, And the logic used to run the goal of the plug-in defined in the life cycle phase.

Two classic functions of maven

Maven's dependency management

A core feature of Maven is dependency management. When it comes to multi module projects (including hundreds of modules or subprojects), managing dependencies becomes a difficult task. Maven shows its high control over dealing with this situation. In traditional web projects, we must copy the jar packages that the project depends on into the project, resulting in a great change in the project. Then, how does Maven project make the project few? The analysis is as follows:

Through analysis, it is found that the jar package is not directly imported into the Maven project, but through the POM Add the coordinates of the required jar package to the XML file, so as to avoid the direct introduction of jars. When you need to use the jar package, just look up POM XML file, and then through POM The coordinates in the XML file are transferred to a maven warehouse specially used for storing jar packages. These jar packages can be found according to the coordinates, and then these jar packages can be run.

So here comes the question...

First: what does the "warehouse for storing jar packages" look like?

Second: read POM Will it be slow to find the jar package in the warehouse? This makes this approach infeasible!

First question: what is the length of the warehouse where jar packages are stored? Later, we will analyze the classification of the warehouse and take you to see what the local warehouse looks like.

Second question: through POM The XML file configures the coordinates of the jar package to be imported, then reads the coordinates and loads the jar package into the warehouse, so that we can directly use the jar package. In order to solve the problem of slow speed in this process, Maven also has the concept of index, which can greatly improve the speed of loading the jar package by establishing an index, This makes us think that the speed of jar package is basically the same as that of reading it in the local project file. This process is like when we look up a dictionary, in order to find the content quickly, the directory in front of the book is like an index. With this directory, we can easily find the content. Similarly, with an index in Maven warehouse, we can think that we can quickly find the jar package.

One click construction of project

Our projects often go through a series of processes such as compilation, testing, running, packaging, installation and deployment.

What is build?

It means that the whole process of compiling, testing, running, packaging, installing and deploying the project is managed by Maven. This process is called build.

One click construction refers to the whole construction process. The whole work can be easily completed with one command of Maven.

Maven's standardized construction process is as follows:

Through the command of Tomcat: run, we find that the current project compilation, testing and running have become very simple.

Use of maven

Maven configuration

JDK needs to be installed and configured before configuration

Environment variable configuration

1. Add maven_ Home corresponds to Maven's decompression directory

2. Edit the path environment variable plus% Maven_ HOME%\bin

After configuration, enter MVN - V in the CMD window to view and display Maven version information, indicating that the configuration is successful.

Maven warehouse

Maven needs to download some jar packages from the warehouse, as shown in the following figure, local project a Project B and others will be from the remote warehouse through Maven software (it can be understood as a warehouse on the Internet) download the jar package and store it in the local warehouse. The local warehouse is the local folder. When this jar package is needed for the second time, it will no longer be downloaded from the remote warehouse. Because the local warehouse already exists, the local warehouse can be understood as a cache. With the local warehouse, you don't have to download it from the remote warehouse every time.

The following figure describes the types of warehouses in Maven:

Configuration of Maven local warehouse

At mave_ HOME/conf/settings. Configure the local warehouse location in the XML file (under the Maven installation directory):

Open settings XML file, configured as follows:

Put the path of your local warehouse in the middle, and then put some common jar packages under the local warehouse directory.

Global setting and user setting

Maven warehouse address, private server and other configuration information need to be in setting The configuration in the XML file is divided into global configuration and user configuration. In the Maven installation directory, there is conf / setting XML file, this setting The XML file is used for all Maven project projects as the global configuration of Maven.

If personality configuration is required, it needs to be set in user configuration, and the setting of user configuration The default location of the XML file is in the: \ ({user. Dir} /. M2 / settings. XML directory, \) {user. Dir} refers to the user directory in windows.

Maven will find the user configuration first. If found, the user configuration file will prevail. Otherwise, the global configuration file will be used.

Understanding of Maven project

As a maven project, its SRC directory and POM XML is required.

After entering the SRC directory, we find that its directory structure is as follows:

Src / main / Java -- the of the project Java file

Src / main / resources -- store project resource files, such as spring and Hibernate configuration files

Src / test / Java -- store all unit tests Java files, such as JUnit test classes

Src / test / resources -- test resource file

Target -- the output location of the project. The compiled class file will be output to this directory

pom. XML -- the core configuration file of Maven project

Note: if it is an ordinary java project, there is no webapp directory.

Maven common commands

We can compile, test, run, package, install and deploy our maven HelloWorld project through a series of Maven commands in CMD.

Maven instruction life cycle

Maven divides the project construction process into three sets of mutually independent life cycles. Please note that "three sets" and "mutually independent" are mentioned here. The three sets of life cycles are:

Clean lifecycle does some cleanup before a real build.

Default lifecycle is the core part of the build, including compilation, testing, packaging, deployment, and so on.

Site lifecycle generates project reports, sites, and publishes sites.

Maven's conceptual model

Maven includes a project object model, a set of standards, a project lifecycle, a dependency management system, and logic used to run plug-in goals defined in the life cycle phase.

Every Maven project has a POM XML file, through POM XML file defines project coordinates, project dependencies, project information, plug-in targets, etc.

The jar packages that the project depends on are managed uniformly through Maven's dependency management. For example, the project relies on junit4 9, through the POM Junit4. XML 9, i.e. use junit4 9, as shown below, is junit4 9 dependency definition:

<!-- 依赖关系 -->
    <dependencies>
        <!-- 此项目运行使用 junit,所以此项目依赖 junit -->
        <dependency>
            <!-- junit 的项目名称 -->
            <groupId>junit</groupId>
            <!-- junit 的模块名称 -->
            <artifactId>junit</artifactId>
            <!-- junit 版本 -->
            <version>4.9</version>
            <!-- 依赖范围:单元测试时使用 junit -->
            <scope>test</scope>
        </dependency>

Maven is used to complete the construction of the project. The project construction includes the processes of cleaning, compiling, testing and deployment. Maven standardizes these processes into a life cycle, as shown below, which are the stages of the life cycle:

Maven can implement the above life cycle processes by executing some simple commands, such as MVN compile, MVN clean and cleanup.

Maven defines a set of standards for the whole project management process, such as standard directory structure, standard life cycle stage, dependency management, standard coordinate definition, etc.

Maven manages the project life cycle process based on plug-ins.

Idea development Maven project

Maven configuration of idea

Select the local Maven installation directory and specify the settings configuration file in the conf folder under the Maven installation directory.

Solution to the invalidity of modifying Maven default configuration by idea

The idea version I use is 2019.3. After modifying the default configuration of Maven through file - > other setting - > setting for new projects, the default configuration of Maven for new projects will not take effect.

terms of settlement:

Open C: \ users \ user IntelliJIDEA2019. 3. Project under \ config \ options default. XML, add a component, save, open idea, file - > other setting - > setting for new projects, and you can see that it has been modified.

 <component name="MavenImportPreferences">
    <option name="generalSettings">
      <MavenGeneralSettings>
        <option name="localRepository" value="D:\develop\maven_repository" />
        <option name="mavenHome" value="D:\develop\apache-maven-3.6.3" />
        <option name="userSettingsFile" value="D:\develop\apache-maven-3.6.3\conf\settings.xml" />
      </MavenGeneralSettings>
    </option>
  </component>

When creating Maven project, idea gives priority to finding plug-ins locally

Add in settings - > Maven - > runner - > VM options

-DarchetypeCatalog=internal

Create a maven web project in idea

Select the Maven web project template provided by idea

Click next to fill in the project information

Click Next and nothing will be changed here.

Click finish directly to start creating the project. Wait patiently until the following interface appears.

Manually add the Src / main / Java directory, as shown in the figure below. Right click the main folder - > New - > directory and name it Java.

Right click the new Java folder - > make directory as - > sources root

New found no servlet solution

A servlet is created under Src / main / Java, and an error will be reported if found.

To solve the problem, the servlet API XXX The jar package is put in. As a maven project, you should add the coordinates of the servlet to import its jar

At born Add coordinates to XML file

Directly open the project's born XML file, and then add coordinates

When adding the coordinates of the jar package, you can also specify the scope of the jar package in the future. Each Maven project needs to define the coordinates of the project. The coordinates are Maven's identity definition of the jar package. For example, the coordinates of the entry program are defined as follows:

<!--项目名称,定义为组织名+项目名,类似包名-->
<groupId>com.ping</groupId>
<!-- 模块名称 -->
<artifactId>web_demo</artifactId>
<!-- 当前项目版本号,snapshot 为快照版本即非正式版本,release为正式发布版本 -->
<version>0.0.1-SNAPSHOT</version>

<packaging > :打包类型
                   jar:执行 package 会打成 jar 包
                  war:执行 package 会打成 war 包
pom :用于 maven 工程的继承,通常父工程设置为 pom

Source method of coordinates

To add a dependency, you need to specify the coordinates of the dependent jar package, but in many cases, we don't know the coordinates of the jar package. You can query it in the following way:

Query coordinates from the Internet

http://search.maven.org/

http://mvnrepository.com/

Dependency range

A depends on B and needs to be in the POM of A Add the coordinates of B in the XML file. When adding coordinates, you need to specify the dependency range, which includes:

Test each SCOP in Maven web project. Test summary:

The order of dependency range from strong to weak is: compile > provided > runtime > test

You need to set the compiled version to 1.8. Here, you need to use Maven's plug-in to set it in POM Add in XML

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

Add tomcat7 plug-in

  <!--添加tomcat7插件-->
  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <port>8080</port>
      <!--<path>/</path>-->
    </configuration>
  </plugin>

At the beginning of creating Maven project, it was found that Tomcat could not be started. After checking the information on the Internet, it was found that tomcat7 plug-in needs to be added. Once added, you can start. The runtime should be started with the tomcat7: run command.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>