Study notes - Maven

Posted by phpfanphp on Tue, 21 Dec 2021 14:35:04 +0100

What is Maven?

Now we need to use many third-party class libraries to build a project. For example, writing a Web project using Spring requires a large number of jar packages. The number of jar packages in a project often makes us stunned, and the relationship between jar packages is complex. One jar package often references other jar packages. The lack of any jar package will lead to project compilation failure.

In the past, when developing projects, programmers often needed to spend more energy on quoting Jar packages to build the project environment, and this work is particularly difficult. Less Jar packages and more Jar packages often report some confusing exceptions.

Maven is a tool to help programmers build projects. We just need to tell Maven which Jar packages we need. It will help us Download all jars and greatly improve development efficiency.
Abstract the project into an object model (pom) pom.xml file

What is Maven warehouse?

Maven warehouse is used to store all Jar packages managed by Maven. It is divided into local warehouse and central warehouse.

Local warehouse: Maven's local Jar package warehouse.
Central warehouse: remote warehouse officially provided by Maven.
When the project is compiled, Maven first looks for the Jar package required by the project from the local warehouse. If the local warehouse does not, Maven downloads the required Jar package from Maven's central warehouse.

What are "coordinates"?

In maven, the coordinates are the unique identification of the Jar package. Maven uses the coordinates to find the Jar package required by the project in the warehouse.

In the following code, groupId and artifactId constitute the coordinates of a Jar package.

<dependency>
   <groupId>cn.missbe.web.search</groupId>
   <artifactId>resource-search</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
</dependency>
  • groupId: the project name of the required Jar package
  • artifactId: module name of the required Jar package
  • Version: the version number of the required Jar package POM XML explanation

inherit

What is inheritance?

When aggregating multiple projects, if the same jars need to be introduced into these aggregated projects, these jars can be written into the parent pom, and each sub project can inherit the pom.

How to implement inheritance?

Parent pom configuration: put the coordinates of the Jar package to be inherited into the label.

<dependencyManagement>
    <dependencies>
          <dependency>
            <groupId>cn.missbe.web.search</groupId>
            <artifactId>resource-search</artifactId>
            <packaging>pom</packaging>
            <version>1.0-SNAPSHOT</version>
          </dependency> 
    </dependencies>
</dependencyManagement>

Sub pom configuration:

<parent>
      <groupId>father pom Of the project groupId</groupId>
      <artifactId>father pom Of the project artifactId</artifactId>
      <version>father pom Version number of the project</version>
</parent>
 <parent>
      <artifactId>resource-search</artifactId>
      <groupId>cn.missbe.web.search</groupId>
      <version>1.0-SNAPSHOT</version>
</parent>

to configure

The configuration process is omitted. It depends on slow download and needs to download the image

image

\apache-maven-3.6.1\conf, you can find the settings file and open it

Add code

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

Creating projects with maven

1. Air construction project

2.modules select maven

  • pom.xml

    pom.xml is the core of Maven. What Jar package your project needs is in POM XML configuration. When compiling the project, Maven reads the file and downloads the corresponding Jar package from the repository.

<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">  
  <!--Coordinates of the parent project. If the value of an element is not specified in the project,
Then the corresponding value in the parent project is the default value of the project. 
Coordinates include group ID,artifact ID and version. -->  
  <parent> 
    <!--Component identifier of the inherited parent project-->  
    <artifactId/>  
    <!--Globally unique identifier of the inherited parent project-->  
    <groupId/>  
    <!--The version of the inherited parent project-->  
    <version/> 
  </parent>  
  <!--Declare which project descriptor follows POM Model version. The version of the model itself rarely changes, however,
But it is still essential to be Maven When new features or other model changes are introduced,
Ensure stability.-->  
  <modelVersion>4.0.0</modelVersion>  
  <!--The globally unique identifier of the project, usually using a fully qualified package name to distinguish the project from other projects.
And the path generated during construction is also generated from this, such as com.mycompany.app The generated relative path is:
/com/mycompany/app-->  
  <groupId>cn.missbe.web</groupId>  
  <!-- Identifier of the component, which and group ID Together, uniquely identify a component. let me put it another way,
You can't have two different projects with the same artifact ID and groupID;At some point 
Specific group ID Next, artifact ID It must also be unique. A component is something produced or used by a project,
Maven The components generated for the project include: JARs,Source code, binary release and WARs Wait.-->  
  <artifactId>search-resources</artifactId>  
  <!--The type of component produced by the project, such as jar,war,ear,pom. Plug ins can be created
 They have their own component types, so not all component types listed above-->  
  <packaging>war</packaging>  
  <!--The current version of the project in the format:Major version.Minor version.Incremental version-Qualified version number-->  
  <version>1.0-SNAPSHOT</version>  
  <!--Name of the project, Maven Generated documents-->  
  <name>search-resources</name>  
  <!--Project home page URL, Maven Generated documents-->  
  <url>http://www.missbe.cn</url>  
  <!-- Detailed description of the project, Maven The generated document is used. When this element can be used HTML Format description
(For example, CDATA The text in is ignored by the parser and can be included HTML Label), 
Plain text descriptions are discouraged. If you need to modify the generated web Index page of the site,
You should modify your own index page file instead of adjusting the documents here.-->  
  <description>A maven project to study maven.</description>  
  <!--Describes the prerequisites in the project build environment.-->  
  <prerequisites> 
    <!--Required to build the project or use the plug-in Maven Minimum version of-->  
    <maven/> 
  </prerequisites>  
  <!--Information needed to build the project-->  
  <build> 
    <!--This element sets the project source code directory. When building the project,
The build system will compile the source code in the directory. The path is relative to pom.xml The relative path of the.-->  
    <sourceDirectory/>  
    <!--This element sets the project script source directory, which is different from the source directory:
In most cases, the contents of this directory will be copied to the output directory(Because scripts are interpreted, not compiled). -->  
    <scriptSourceDirectory/>  
    <!--This element sets the source code directory used by the project unit test. When testing the project,
The build system will compile the source code in the directory. The path is relative to pom.xml The relative path of the.-->  
    <testSourceDirectory/>  
    <!--Compiled application class Directory where files are stored.-->  
    <outputDirectory/>  
    <!--Compiled tests class Directory where files are stored.-->  
    <testOutputDirectory/>  
    <!--Use a series of build extensions from the project-->  
    <extensions> 
      <!--Describes the build extension used.-->  
      <extension> 
        <!--Build extended groupId-->  
        <groupId/>  
        <!--Build extended artifactId-->  
        <artifactId/>  
        <!--Build extended version-->  
        <version/> 
      </extension> 
    </extensions>  
    <!--This element describes the list of all resource paths related to the project, such as the property file related to the project,
These resources are included in the final package file.-->  
    <resources> 
      <!--This element describes all resource paths related to the project or test-->  
      <resource> 
        <!-- Describes the target path of the resource. The path is relative target/classes Directory (e.g ${project.build.outputDirectory}). For example, if you want resources in a specific package(org.apache.maven.messages),You must set the element to org/apache/maven /messages. 
However, if you just want to put resources into the source directory structure, you don't need this configuration.-->  
        <targetPath/>  
        <!--Whether to use parameter value instead of parameter name. Parameter values are taken from properties Element or attribute configured in the file,
File in filters Element.-->  
        <filtering/>  
        <!--Describes the directory where resources are stored. The path is relative POM route-->  
        <directory/>  
        <!--Contains a list of modes, such as**/*.xml.-->  
        <includes/>  
        <!--List of excluded modes, for example**/*.xml-->  
        <excludes/> 
      </resource> 
    </resources>  
    <!--This element describes all the resource paths related to the unit test, such as the property files related to the unit test.-->  
    <testResources> 
      <!--This element describes all resource paths related to the test. See build/resources/resource Description of the element-->  
      <testResource> 
        <targetPath/>
        <filtering/>
        <directory/>
        <includes/>
        <excludes/> 
      </testResource> 
    </testResources>  
    <!--The directory where all files generated by the build are stored-->  
    <directory/>  
    <!--The file name of the generated component. The default value is ${artifactId}-${version}. -->  
    <finalName/>  
    <!--When filtering List of filter property files used when the switch is on-->  
    <filters/>  
    <!--Default plug-in information that can be referenced by subprojects. The plug-in configuration item will not be resolved or bound to the lifecycle until it is referenced.
Any local configuration of a given plug-in will override the configuration here-->  
    <pluginManagement> 
      <!--List of plug-ins used.-->  
      <plugins> 
        <!--plugin The element contains the information needed to describe the plug-in.-->  
        <plugin> 
          <!--The plug-in is in the warehouse group ID-->  
          <groupId/>  
          <!--The plug-in is in the warehouse artifact ID-->  
          <artifactId/>  
          <!--The version (or version range) of the plug-in being used-->  
          <version/>  
          <!--Download from this plugin Maven Extensions (such as packaging and type processors), for performance reasons,
This element is set to only when it really needs to be downloaded enabled. -->  
          <extensions/>  
          <!--The configuration of a set of goals is performed during the build lifecycle. Each target may have a different configuration.-->  
          <executions> 
            <!--execution Element contains the information required for plug-in execution-->  
            <execution> 
              <!--The identifier of the execution target, which is used to identify the target in the construction process or match the execution target to be merged in the inheritance process-->  
              <id/>  
              <!--The construction lifecycle phase of the target is bound. If omitted, the target will be bound to the default phase configured in the source data-->  
              <phase/>  
              <!--Configured execution target-->  
              <goals/>  
              <!--Is the configuration propagated to the child POM-->  
              <inherited/>  
              <!--As DOM Object configuration-->  
              <configuration/> 
            </execution> 
          </executions>  
          <!--Additional dependencies required for the project to introduce plug-ins-->  
          <dependencies> 
            <!--See dependencies/dependency element-->  
            <dependency>......</dependency> 
          </dependencies>  
          <!--Is any configuration propagated to subprojects-->  
          <inherited/>  
          <!--As DOM Object configuration-->  
          <configuration/> 
        </plugin> 
      </plugins> 
    </pluginManagement>  
    <!--List of plug-ins used-->  
    <plugins> 
      <!--See build/pluginManagement/plugins/plugin element-->  
      <plugin> 
        <groupId/>
        <artifactId/>
        <version/>
        <extensions/>  
        <executions> 
          <execution> 
            <id/>
            <phase/>
            <goals/>
            <inherited/>
            <configuration/> 
          </execution> 
        </executions>  
        <dependencies> 
          <!--See dependencies/dependency element-->  
          <dependency>......</dependency> 
        </dependencies>  
        <goals/>
        <inherited/>
        <configuration/> 
      </plugin> 
    </plugins> 
  </build>  
  <!--Modules (sometimes called subprojects) are built as part of the project.
Each module element listed is a relative path to the directory of the module-->  
  <modules/>  
  <!--Discover the list of dependent and extended remote warehouses.-->  
  <repositories> 
    <!--Contains information that needs to be connected to the remote warehouse-->  
    <repository> 
      <!--How to handle the download of release version in remote warehouse-->  
      <releases> 
        <!--true perhaps false Indicates whether the warehouse is enabled for downloading certain types of components (release version, snapshot version). -->  
        <enabled/>  
        <!--This element specifies how often updates occur. Maven Will compare local POM And remote POM The timestamp of the. The options here are: always(All the time), daily(Default, daily), interval: X(here X Is the time interval in minutes), or never(Never).-->  
        <updatePolicy/>  
        <!--When Maven What to do when verifying the component verification file fails: ignore(Ignore), fail(Failed), or warn(Warning).-->  
        <checksumPolicy/> 
      </releases>  
      <!-- How to handle the download of snapshot version in remote warehouse. Yes releases and snapshots These two sets of configurations,
POM You can adopt different strategies for each type of component in each separate warehouse.
For example, someone may decide to turn on support for snapshot version download only for development purposes.
See repositories/repository/releases element -->  
      <snapshots> 
        <enabled/>
        <updatePolicy/>
        <checksumPolicy/> 
      </snapshots>  
      <!--Unique identifier of the remote warehouse. Can be used to match in settings.xml Remote warehouse configured in file-->  
      <id>banseon-repository-proxy</id>  
      <!--Remote warehouse name-->  
      <name>banseon-repository-proxy</name>  
      <!--Remote warehouse URL,Press protocol://hostname/path form -- >  
      <url>http://192.168.1.169:9999/repository/</url>  
      <!-- Warehouse layout type used to locate and sort components-Can be default(Default) or legacy(Legacy). Maven 2 Provides a default layout for its warehouse; However, Maven 1.x There is a different layout. We can use this element to specify that the layout is default(Default) or legacy(Legacy).-->  
      <layout>default</layout> 
    </repository> 
  </repositories>  
  <!--Discover a list of remote repositories for plug-ins that are used to build and report-->  
  <pluginRepositories> 
    <!--Contains information about the need to connect to the remote plug-in repository.See repositories/repository element-->  
    <pluginRepository>......</pluginRepository> 
  </pluginRepositories>  
  <!--This element describes all dependencies related to the project. These dependencies constitute each link in the project construction process.
They are automatically downloaded from the repository defined by the project. For more information, see project dependency mechanism.-->  
  <dependencies> 
    <dependency> 
      <!--Dependent group ID-->  
      <groupId>org.apache.maven</groupId>  
      <!--Dependent artifact ID-->  
      <artifactId>maven-artifact</artifactId>  
      <!--Dependent version number. stay Maven 2 in, It can also be configured as a range of version numbers.-->  
      <version>3.8.1</version>  
      <!-- Dependency type. The default type is jar. It usually represents the extension of the dependent file, but there are exceptions
. A type can be mapped to another extension or classifier. The type often corresponds to the packaging method used,
 Although there are exceptions. Examples of some types: jar,war,ejb-client and test-jar. 
If set extensions by true,Can be in plugin Define new types in. So the previous examples of types are incomplete.-->  
      <type>jar</type>  
      <!-- Dependent classifier. Classifiers can distinguish between belonging to the same POM,However, components with different construction methods.
The classifier name is appended to the version number of the file name. For example, if you want to build two separate components into JAR,
One use Java 1.4 Compiler, another uses Java 6 Compiler, you can use a classifier to generate two separate JAR Components.-->  
      <classifier/>  
      <!--Dependency range. In the process of project publishing, it helps to decide which components are included. For details, please refer to the dependency mechanism.    

   - compile : Default range for compilation      
     It's like compiling, but it supports your expectations jdk Or a container, similar to classpath      
        - runtime: Required for execution      
          to test Use when task      
             - system: Corresponding elements need to be provided externally. adopt systemPath To get      
               : Only for use in the range system. Provide the corresponding path      
                  - optional:   When the project itself is dependent, mark whether the dependency is passed. Used for continuous dependencies-->  
                    /scope>  
                          <!--Only system Scope of use. Note that this element is not encouraged,
                    And the element may be overwritten in the new version. This element specifies the path on the file system for the dependency.
                    An absolute path is required instead of a relative path. It is recommended to use attributes to match absolute paths, such as ${java.home}. -->  
                          <systemPath/>  
                          <!--When calculating the transfer dependency, list the excluded dependent component set from the dependent component list.
                    Namely tell maven You only depend on the specified project, not the project. This element is mainly used to resolve version conflicts-->  
                          <exclusions> 
                            <exclusion> 
                              <artifactId>spring-core</artifactId>  
                              <groupId>org.springframework</groupId> 
                            </exclusion> 
                          </exclusions>  
                          <!--Optional dependencies if you are in the project B Middle handle C If the dependency declaration is optional, you need to B Project (e.g. project) A)Explicit reference pairs in C Dependence on. Optional dependencies block the transitivity of dependencies.-->  
                          <optional>true</optional> 
                        </dependency> 
                      </dependencies>  
                      <!-- Default dependency information inherited from all subprojects of the project. This part of the dependency information will not be resolved immediately,
                    Instead, when a subproject declares a dependency (which must be described) group ID and artifact ID Information),
                    If group ID and artifact ID Some other information is not described,
                    Then pass group ID and artifact ID Match the dependencies here and use the dependency information here.-->  
                      <dependencyManagement> 
                        <dependencies> 
                          <!--See dependencies/dependency element-->  
                          <dependency>......</dependency> 
                        </dependencies> 
                      </dependencyManagement>  
                      <!--Project distribution information during implementation mvn deploy After indicates the location to publish.
                    With this information, you can deploy the website to a remote server or components to a remote warehouse.-->  
                      <distributionManagement> 
                        <!--The information required to deploy the components generated by the project to the remote warehouse-->  
                        <repository> 
                          <!--Is a unique version number (by timestamp and build serial number) assigned to the snapshot?
                    Or use the same version number every time? See repositories/repository element-->  
                          <uniqueVersion/>  
                          <id>banseon-maven2</id>  
                          <name>banseon maven2</name>  
                          <url>file://${basedir}/target/deploy</url>  
                          <layout/> 
                        </repository>  
                        <!--Where is the snapshot of the component deployed? If the element is not configured, it is deployed to by default repository The warehouse configured by the element,
                    See distributionManagement/repository element-->  
                        <snapshotRepository> 
                          <uniqueVersion/>  
                          <id>banseon-maven2</id>  
                          <name>Banseon-maven2 Snapshot Repository</name>  
                          <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url>  
                          <layout/> 
                        </snapshotRepository>  
                        <!--Information required to deploy the project's Web site-->  
                        <site> 
                          <!--Unique identifier of the deployment location that matches the site and settings.xml Configuration in file-->  
                          <id>banseon-site</id>  
                          <!--Name of the deployment location-->  
                          <name>business api website</name>  
                          <!--Deployment location URL,Press protocol://hostname/path form -- >  
                          <url>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web</url> 
                        </site>  
                        <!--Of the project download page URL. If there is no such element, the user should refer to the home page.
                    The reason for using this element is to help locate components that are not in the warehouse (due to license Restrictions).-->  
                        <downloadUrl/>  
                        <!-- The status of the component in the remote warehouse is given. This element must not be set in a local project,
                    Because this is automatically updated by the tool. Valid values are: none(Default),
                    converted(Warehouse keeper from Maven 1 POM Switch over), partner(Directly from partner Maven 2 Warehouse synchronization), deployed(from Maven 2 Instance deployment), verified(Correct and final when verified).-->  
                        <status/> 
                      </distributionManagement>  
                      <!--Replace the name with a value, Properties Can be in the whole POM It can also be used as a trigger condition (see settings.xml In the configuration file activation Description of the element). Format is<name>value</name>. -->  
                      <properties/> 
                    </project>

website

View the URL of the jar package- https://mvnrepository.com

Topics: Java Maven jar