This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Maven

Introduction to the

Maven is an open source project in the Apaache organization. It mainly serves project management, dependency management and information management based on the Java platform.

Why you need to learnMaven

  • inJavawebDevelopment, need to use a large number ofjarPackage, we need to manually import
  • We need an auto to import and configure this for usjarpackage

Maven was born!

Mavenfeatures

  • Dependency Management System

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.5.6</version>
            </dependency>
    Copy the code
    • Bag type:jarPackage orwarpackage
    • groupId: Indicates the actual project – company name
    • artifactId: Used to represent one of the actual projectsMavenModule – Project name
    • version: Indicates the version of the project
  • Multi-module construction

    We can decompose a project into modules.

  • Consistent directory structure

    The core idea of Maven is convention over configuration, and a project directory structure is established as the standard Java project structure.

  • Consistent build models and plug-in mechanisms

MavenProject architecture management tool

Rely onJDK

JDK version 1.8 or later is recommended

MavenThe installation

Download address: maven.apache.org/download.cg…

After the download is completed, decompress the directory without Spaces and Chinese.

Directory Structure description

  • bin: This directory containsmvnScripts to run that are used to configurejavaOrder, stand byclasspathAnd the relatedJavaSystem properties, and executeJavaCommand.
  • bootThe file under this directory is a classloader, relative to the defaultjavaClass loaders, which provide richer syntax for easy configuration,mavenWith this framework, load your own class libraries
  • confThis directory contains a very important filesettings.xml.MavenCore configuration file
  • lib: This directory contains allMavenRequired at runtimeJavaClass libraries, ` ` MavenIt is developed in modules itself, and there are some included hereThird party dependencies used by Maven

Configuring environment Variables

  • M2_HOME:mavenIn the directorybindirectory
  • MAVEN_HOME:mavendirectory
  • Path: in the systempathIn the configuration%MAVEN_HOME%\bin

validation

Verification found problems:

  • Restart the computer
  • If multiple versions are installed, check to see if any environment variables have been set

Ali Cloud Mirror

Generally, access to the Internet is slow, so we use the domestic mirror warehouse.

  • Mirror:mirrors
  • Domestic suggestions to use ali Cloud mirror

Open the Maven configuration file (typically in the Maven installation directory conf/settings.xml for Windows machines) and add a mirror child node to the
TAB:

<mirror>
  <id>aliyunmaven</id>
  <mirrorOf>*</mirrorOf>
  <name>Ali Cloud public warehouse</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>
Copy the code

Configuring a Local Warehouse

Warehouses are generally divided into two types: local and remote.

Establish local warehouse:

Open the Maven configuration file (usually in the Maven installation directory conf/settings.xml for Windows machines) and modify it in
. ${user.home}/.m2/repository; ${MAVEN_HOME}/repo;

  <localRepository>${MAVEN_HOME}/repo</localRepository>
Copy the code

Create the repo folder in the Maven installation directory

inIDEAThe use ofMaven

IDEA has a default Maven configuration and we need to replace Maven with our own

IDEA will help us download the dependency automatically after changing the warehouse.

MavenProject directory structure

directory instructions
${basedir} storepom.xmlAnd all the subdirectories
${basedir}/src/main/java The projectjavaThe source code
${basedir}/src/main/resources Resources for the project, such as configuration files
${basedir}/src/test/java The test class for the project
${basedir}/src/test/resources Resources used for testing