Getting started with Maven for beginners (1)

1. What is Maven

Maven is a tool for managing Java projects, but not a tool for managing resource planning and scheduling. It deals with the various tasks involved in managing a specific project, such as compilation, testing, packaging, documentation, and distribution.

1.1 Composition of Maven

Maven includes the following parts.

  • A set of conventions for handling dependency management, directory structure, and build workflow. Standardization based on these conventions can greatly simplify the development process. For example, a common directory structure makes it easier for developers to keep up with unfamiliar projects.
  • An XML Schema for project configuration: Project Object Model, or POM for short. Every Maven project has a POM file.
  • A plugin architecture that delegates project tasks to external components. This simplifies the process of updating and extending Maven's capabilities.

1.2 Install and configure Maven

1.2.1 Download

The appropriate Maven tar.gz or zip file for your system can be downloaded from here .

1.2.2 Installation

Installation is very simple: unzip the download into any folder of your choice (let's call it <installation directory>).

Let's take apache-maven-3.8.6 as an example:

Step 1: Set the environment variable M2_HOME to point to <installation directory>apache-maven-3.8.6, this environment variable will tell Maven where to find its configuration file, conf\settings.xml;

Step 2: Add %M2_HOME%\bin (${M2_HOME}/bin on Linux) to your execution path. After that, execute mvn on the command line to run Maven.

There is no need to modify the default configuration when compiling and running the example project. When executing mvn for the first time, Maven will create a local repository for you and download a large number of JAR files required for basic operations from the Maven central repository. Finally, it downloads the dependencies needed to build the current project. Details of settings.xml can be found at .

Second, the basic concept of Maven

2.1 Standard directory structure

Maven defines a standard project directory structure. (Note: Not every element of Maven is required for every type of project, and many can be overridden in the POM file if necessary.)
insert image description here

2.2 POM Outline

2.2.1 pom outline display

<project>     //根元素

  <groupId/>
  <artifactId/> //唯一地定义一个Maven 项目的值 
  <version/>
  
  <packaging/>  //该项目所产生的构件的类型;默认值是 jar
  <properties/> //在 POM 中所引用的符号
  <dependencies/> //构建当前项目所需要的其他 Maven 项目
  <build/> //构建该项目所需要执行的任务的配置
  <profiles/> //为不同的用例自定义POM的命名的配置
</project>

2.3 Components

2.3.1 What is maven build

Any object that can be uniquely identified by Maven's coordinate system is a Maven artifact. In most cases, artifacts are files, such as JARs, produced by building a Maven project. However, a POM file that only contains definitions used by other POMs (which itself does not generate artifacts) is also a Maven artifact. A Maven artifact's type is specified by an element in its POM file. The most common values ​​are pom, jar, ear, war, and maven-plugin.

2.4 Use cases for POM files

The POM file can be used in the following ways.

  • Default - used to build an artifact.
  • Parent POMs—providing a single source of configuration information inherited by subprojects—declare this POM file as
    the value of their elements.
  • Aggregator - Used to build a set of projects declared as subprojects located in the folder of its current aggregator project, each containing its own POM file.

The value of the element of the POM file that is the parent POM or aggregator will be pom. Note that a POM file may provide two functions at the same time.

2.5 GAV coordinates

The POM defines five elements called coordinates that identify Maven artifacts. The acronym GAV refers to the 3 coordinates that must always be specified, as well as the initial letter.

The following coordinates are listed in the order in which they appear in the coordinate expression.

1. groupId: It is the global unique identifier of the project or project group. This is usually the fully qualified Java package name used in the Java source code. For example, io.netty, com.google.

2. artifactId: used to identify different components related to a certain one. For example, netty-all, netty-handler.

3. type: refers to the type of the main component related to the project (corresponding to the value in the POM file of the component). Its default value is jar. For example, pom, war, ear.

5. version: identifies the version of the component. For example, 1.1, 2.0-SNAPSHOT, , 4.1.9.Final.

6. classifier: >Used to distinguish components that belong to the same POM but are constructed in different ways. For example, javadoc, sources, jdk16, jdk17.

For example:

  • A complete coordinate expression has the format: artifactId:groupId:packaging:version:classifier
  • Contains the GAV coordinates of all components of Netty: io.netty:netty-all:4.1.9.Final
  • A POM file must declare the coordinates of the artifacts it manages. The above component has items with coordinates like this:
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.92.Final</version>
            <packaging>jar</packaging>
  • Its artifacts have names in the format: artifactId-version.packaging
  • In this case it will generate this artifact: netty-all-4.1.9.Final.jar

3. Dependence

A project's dependencies are the external components it needs to compile and execute. In most cases, a project's dependencies will also have dependencies of its own. We call these dependencies your project's transitive dependencies. A complex project may have a deep dependency tree;

Maven provides various tools to help understand and manage it. The declaration of Maven is in the element of POM:

<dependencies>
   <dependency>
     <groupId/>
     <artifactId/>
     <version/>
     <type/>
     <scope/>
    <systemPath/>
  </dependency>
...
</dependencies>

In declarations, GAV coordinates are always mandatory. . The type and scope elements are also required for dependencies whose values ​​are not the default values ​​jar and compile respectively.

for example:

    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.92.Final</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${
    
    junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${
    
    junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Elements can have the following values.

  • compile - Required for compilation and execution (default).
  • runtime—needed only for execution.
  • optional——other projects that do not reference the artifacts produced by this project are regarded as transitive dependencies.
  • provided - will not be included in the WEB_INF/lib directory of the WAR file generated by this POM.
  • test - only needed for compilation and test execution.
  • import - This is discussed later in the "Dependency Management" section.
  • The systemPath element is used to specify an absolute location in the file system.

The way Maven manages project dependencies, including a repository protocol for storing and retrieving those dependencies, has revolutionized the way JAR files are shared between projects, effectively eliminating the need for each A problem that often arises when everyone maintains a private lib directory.

Guess you like

Origin blog.csdn.net/qq_35241329/article/details/131262820