1. maven tutorial

1. Benefits of using Maven

  1. Easy management of jar packages
  2. One-click build, from compile ----- test ----- run ---- package ------- deploy the entire process to Maven management

2. Maven installation

2.1 Download

Baidu search for maven,
Insert picture description here
enter the official website, select download to
Insert picture description here
find Binary zip archive apache-maven-3.6.2-bin.zip, click to download
Insert picture description here

2.2 Installation

Unzip to a path that does not contain spaces or Chinese, and see its structure.
Insert picture description here
Configure environment variables.
From Control Panel-> All Control Panel Items-> System-> Advanced System Settings-> Advanced-> Environment Variables.
Insert picture description here
First, your computer must install java And configure the java environment variables, which have been configured here.

Configure the MAVEN_HOME variable, the variable value is your maven installation path.
Insert picture description here
Add% MAVEN_HOME% / bin to the environment variable path and
Insert picture description here
run mvn -v to see if the installation is successful. If you see this, the installation is successful.
Insert picture description here

3. Maven warehouse and configuration

3.1 Maven's warehouse

Insert picture description here

  • Local warehouse: The directory where jar packages are stored locally.
  • Remote warehouse (private service): Privately built server. Generally used in the internal LAN of the enterprise.
  • Central warehouse: maven has a built-in remote warehouse address, which is the central warehouse, and the URL can be accessed from D: \ Tools \ apache-maven-3.6.2 \ lib \ maven-model-builder-3.6.2.jar \ org \ apache \ maven \ model \ pom-4.0.0.xml found https://repo.maven.apache.org/maven2
    Insert picture description here
    Insert picture description here

What order does Maven follow when searching for jars?

  • Find local first
  • If you cannot find it locally, you can search it from the private server, and download it to the local warehouse after finding it;
  • If you cannot find it on the private server, you can search it from the central warehouse, download it to the private server after finding it, and finally download it to the local warehouse;

In the end, Maven will download the jar to the local warehouse, and the maven project will then reference the jar package of the local warehouse.

3.2 Local warehouse configuration

Open conf / settings.xml in the installation path and add E: // 9_Repository // repository
Insert picture description here

3.3 Configuration of remote warehouse

Due to the reason of the wall in China, downloading the jar package is slow, you can configure the mirror, as follows

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

Insert picture description here

4. Standard directory structure of Maven project

Insert picture description here
Directory structure specification
Insert picture description here

5. Maven commands

  1. Clear
    clean is the cleanup command of the maven project. Executing clean will delete the target directory and all contents under it.
  2. compile
    compile is the compile command of the maven project. Its function is to compile the java source file under src / main / java into a class file and output it to the classes directory under the target.
  3. test
    test is the test command mvn test of the maven project, which will execute the unit test class under src / test / java. It will compile the java source code, and also compile the java source code in the test directory, and then run the test method in the test class.
  4. The package
    package is the packaging command of the maven project. For the java project, execute the package into a jar package, and for the web project into a war package, execute the mvn package under the project directory.
  5. Install
    install is the installation command of maven project, execute install to publish maven into jar package or war package and publish it to local warehouse.

When the following command is executed, the previous operation process will also be executed automatically.

  1. Three sets of life cycles

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

1. Clean Lifecycle: Do some clean-up work before the actual build.
2. Default Lifecycle: The core part of the build: compile, test, package, deploy, etc.
3. Site Lifecycle: Generate project reports, sites, and publishing sites.

Each stage has a corresponding command, and there are corresponding plug-ins to support the operation of the command.

Note: The instructions within the same instruction cycle, when the following commands are executed, the previous commands will be automatically executed.

6. Configure maven using eclipse

6.1 Configure maven plugin, high version comes with

Insert picture description here
Insert picture description here

6.2 Configure warehouse location in Eclipse

Insert picture description here
Insert picture description here

6.3 Building an index

Insert picture description here
Insert picture description here
Insert picture description here

6.4 Success can see the warehouse can be expanded

Insert picture description here

7. The scope attribute of the jar package

When adding the coordinates of the jar package, you can also specify the future scope of the jar package. The
dependent range includes

  • compile: Compile range. A depends on B during compilation. This range is the default dependent range. Compile-range dependencies are used for compilation, testing, and running. Because of runtime requirements, compile-range dependencies are packaged.

  • provided: The provided dependency is only used after the JDK or a container has provided the dependency. The provided dependency is required during compilation and testing, but not at runtime. For example, the servlet api is provided by the tomcat container.

  • runtime: The runtime dependency is needed when running and testing the system, but not when compiling, such as: jdbc driver package. Due to runtime requirements, runtime scope dependencies will be packaged

  • test: Test range dependencies are not required during compilation and operation, they are only available during test compilation and test operation, such as junit. Since it is not needed at runtime, the test scope dependency will not be packaged.

  • system: The system range dependency is similar to provided, but you must explicitly provide a path to the jar file in the local system. You need to specify the systemPath disk path. System dependency is not recommended.
    Insert picture description here

Published 97 original articles · praised 3 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_39530821/article/details/102882397