• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Maven learning notes

King Wang

1 月 3, 2022

brief introduction

Maven Is a powerful project building tool , It can help users to establish an effective automatic construction system , yes Apache A top-level project of ( Official website address :http://maven.apache.org/).maven The core document of is pom.xml, Among them pom That is to say Project Object Modal, In the process of building the project , The main need to configure pom.xml The interdependence between project modules and the use of related commands can be , It saves the tedious construction task before .

Maven Model

Maven Model diagram

As shown in the figure above ,maven Through the core document pom.xml Project object model , Manage the dependencies of the project ,maven The order of search dependencies is as follows :

  1. The first 1 Step – Search for dependencies in the local repository , If not found , Then move to step 2, If you find , Further processing is performed .
  2. step 2- If the search dependency in the central repository is not found , And mention remote repositories / The repository , Then move to step 4, If you find , Download it to the local repository for future reference .
  3. step 3- If you don’t mention remote repositories ,Maven Just stop processing and throw an error ( Cannot find dependency ).
  4. step 4- Search for dependencies in a remote repository or repository , If you find , Download it to the local repository for future reference , otherwise Maven Will stop processing as expected and throw an error ( Cannot find dependency ).

Last maven Clean up the project through plug-ins 、 compile 、 Test generation report 、 pack , Of course, the corresponding operation needs the corresponding command to execute .

Maven Basic concepts

  • Project: Anything you want to build ,Maven They can be thought of as engineering . These projects are defined as engineering object models , That is to say Project Object Modal. One project can rely on other projects , Of course, it can also have multiple sub projects .
  • POMpom.xml yes Maven Core document of , She was directing Maven How to work metadata file .POM The files are located in the root directory of each project .
  • GroupIdGroupId Is the unique identifier of a project in the global context , Generally, it is the name of the project .GroupId It is helpful to use a complete package name to distinguish a project from other projects with similar names .
  • Artifact: artifacts , It is the document that the project will produce or need to use , It can be .jar file 、 Source file 、 Binary 、.war file , It could even be .pom file . Every Artifact All by GroupId and ArtifactId The combined identifier uniquely identifies . Need to be used Artifact All in the warehouse , otherwise Maven They can’t be found .
  • Dependency: In order to be able to build or run , One java Projects usually rely on other packages . stay Maven in , These dependent packages are called Dependency.Dependency It’s usually for other projects Artifact.
  • Plug-in: so to speak Maven It’s a collection of plug-ins , Every function of her is done by plug-ins . Plug in provides goal. And according to POM To complete the work . The main Maven Plug ins are made by Java written .
  • Repository: Warehouse , Place Artifact The place of , There’s a central warehouse 、 Public Warehouse 、 Private warehouse and local warehouse . In order to improve the Artifact Download speed , Generally, we will use domestic private warehouses or open public warehouses , The default is to pull from the central warehouse .

By the way, a few Maven Warehouse address :

https://search.maven.org/

http://mvnrepository.com/

http://maven.aliyun.com/mvn/view

Maven install

  1. Download from official website Maven package , The address is :http://maven.apache.org/download.cgi
  2. Download it zip The compressed package is decompressed to the specified directory ( Don’t put it in C disc )
  3. Set up “JAVA_HOME” environment variable , Appoint JDK The installation directory .(java Programmers usually install jdk It will be configured at all times )
  4. Set up “M2_HOME” environment variable , Here is the directory you just unzipped .
  5. To configure “Path” environment variable , Take the decompressed Maven In the directory bin Catalog (%M2_HOME%\bin) Add to the current environment variable , Both system variables and user variables work , The difference between the two lies in the user’s permissions , Put it in the system variable and all users can use it , The user variable can only be used by the current user , commonly Windows It’s all personal computers , There won’t be multi-user situations , Either way . After adding it in cmd Enter the keyword in the mvn You can use mvn Relevant command .
  6. To prevent memory overflow ( This is not the case with personal computers ), Need to set up MAVEN_OPTS=-Xms 512m -XMX
    1024m
    , This is equivalent to giving… By hand Maven Add memory space .

By the way Maven Directory structure

  • bin:Maven Run script for .
  • boot:Maven Your own class loader .
  • conf: Global profile ( The local warehouse address is configured here ).
  • lib:Maven Class libraries required by runtime .

Maven To configure

settings.xml

maven The default local warehouse is in a .m2 Under the directory of ,Windows By default, the user is in C:\Users\ Yours PC user name \.m2, If you don’t want to use the default address , You need to maven In the catalog conf Found in the directory settings.xml( Save the global configuration information shared by all local projects ), Then modify the following code snippet :

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository> The local warehouse storage path you want to set up </localRepository>

Of course settings.xml There are many configuration items in the file , Keep your notes here , I believe it will be used in future construction projects .

localRepository: Local warehouse location , Default in .m2/repository/, It can be changed artificially

offline: Off line switch , Whether each build is downloaded from a remote repository , Default false

servers: Corresponding POM Of documents distributionManagement Elements define id, And the user name of the login server 、 password

mirrors: Define the warehouse image , Point the warehouse address to the custom warehouse address (id: New mirror image ID,name: Image name ,url: Mirror address ,mirrorOf: Take that address as a mirror image , The default is central)

proxies: Set up HTTP agent

parent Give the location of the parent project , If the parent exists . If not specifically pointed out , Value is the parent item for the current project . The location is a as a group ID, artifact ID and version The combination of elements .

modelVersion Describe this POM Which version of the project descriptor does the file follow .

groupId Universal unique identifier for a project . It’s common to use a perfectly correct package name to distinguish it from similar names for other projects ( such as :org.apache.maven)

artifactId In the given groupID Of group Inside is artifact The specified identifier is unique artifact It stands for being made or by a project Application components . about Maven Project artifact Examples include : some JARs, The original code and the release of binary and WARs.

packaging This project produces artifact type , for instance jar war pom Plugins Be able to create their own packages , Including the type of package , So this list can’t contain all the possible types

name The full name of the current project

version The current project produces artifact The current version of

description A detailed description of the current project , When it comes to describing the project, it is Maven used , For example web Site . This element can be specified as CDATA type , Allow in the descriptor HTML Of tags, Blank text is not encouraged . If you need to modify the generated web Index page of the site , You can use your own index instead of automatically generated text .

url The home page of the current project URL

prerequisites Describes the prerequisites for the compilation environment of the current project

issueManagement Current project release management information .

ciManagement Continuous integration information for the current project .

inceptionYear The year the current project started , use 4 Digital description . When it comes to introduction, it is used to provide copyright information

mailingLists Contains information including mailing lists

developers Information describing the developer of the current project

contributors Describe information about people who have contributed to the current project , Not specifically developers

licenses This element describes all the license files for the current project . Each license file is described by a license element , Then describe the extra elements . Usually only the license documents that apply to this project are listed as well as the ones that apply to The dependence is not licenses. If more than one licenses It’s all listed , So let’s say that the user chooses what they want , Instead of accepting all license documents .

scm Specifies the version control tool in the current project , such as CVS, Subversion, wait .

organization This element describes the various attributes of the organization to which the project belongs . These attributes are applied to document creation ( Copyright notices and links ).

build Information necessary to create a project .

profiles List of files compiled by local project , When activated, it will change build The process of

modules modular ( Sometimes called subprojects ) As part of the current project . Each listed submodule points to the relative path of the directory file containing the module

repositories Discover dependent and extended remote repository

pluginRepositories Find out plugins List of remote repositories for , Mainly for compiling and reporting

dependencies This element describes all the dependencies associated with the current project . These dependencies are used to create a compile time path . They are automatically downloaded from the repository based on the definition of the current project . For more information , see the dependency mechanism

reports Deprecated. It is forbidden to apply . In the current version, it is Maven What has been overlooked is .

reporting This element includes the reported plugins The specified , Used as a Maven Generate auto generated reports for your site . These reports will run when the user executes mvn site. All reports will be included in the browser navigation bar .

dependencyManagement The default dependency information will be inherited from this element . These dependencies are not immediately resolved in this part . When one comes from this POM The element of describes a dependency based on the matched groupId and artifactId, The version and other values of this section are used for dependencies that have not been specified yet .

distributionManagement Information assigned to a project allows for remote web Server and repository site and artifacts To configure .

pom.xml

pom.xml File is the most frequently used configuration file , The following configuration items are allowed :

Depend on the configuration

groupId: A unique symbol of a project or organization , And the path generated during configuration is also generated from this , Such as org.codehaus.mojo The relative path generated is :/org/codehaus/mojo

artifactId: General name of the project

version: Version of the project

packaging: The packaging mechanism , Such as pom, jar, maven-plugin, ejb, war, ear, rar, par

classifier: classification

pom Relationship

POM The main relationship is ** rely on **、** Inherit **、** synthesis **

Dependency relationship

Look at the code below

<dependencies>
<dependency>
<groupId>beifeng.jms<groupId>
<artifactId>jms<artifactId>
<version>1.0<version>
<type>jar<type>
<scope>test<scope>
<optional>true<optional>
<dependency>
...
<dependencies>
mvn install:install-file
-DgroupId=javax.jms This and your own pom.xml Just like the one in the middle

-DartifactId=jms This and your own pom.xml Just like the one in the middle

-Dversion=1.0 This and your own pom.xml Just like the one in the middle

-Dfile=D:\jms-1.0.jar ( What you want to add jar package , For example, I put it in D disc )

-Dpackaging=jar

type: The corresponding dependency package form , Such as jar,war
scope: Used to limit the range of dependencies , It includes the following variables :
compile : Default range , Used to compile
provided: Similar to compiling , But support your expectations jdk Or the container provides , Be similar to classpath
runtime: When executed , Need to use
test: be used for test Use
system: We need to provide the corresponding elements from the outside . adopt systemPath To obtain
systemPath: Only used in the range of system. Provide the appropriate path
optional: The label is optional , When the project itself is dependent . Used for continuous dependency

You can put your own jar Package imported into the maven Medium repository in

Exclusivity
The outside tells maven You only include designated projects , Does not include related dependencies . This factor is mainly used to solve version conflicts

<dependencies>
<dependency>
<groupId>org.apache.maven<groupId>
<artifactId>maven-embedder<artifactId>
<version>2.0<version>
<exclusions>
<exclusion>
<groupId>org.apache.maven<groupId>
<artifactId>maven-core<artifactId>
<exclusion>
<exclusions>
<dependency>
<dependencies>

Represents a project maven-embedder Project required maven-core, But we don’t want to quote maven-core

Inheritance relationships

First define the parent project as follows :

<project>
<modelVersion>4.0.0<modelVersion>
<groupId>org.codehaus.mojo<groupId>
<artifactId>my-parent<artifactId>
<version>2.0version>
<packaging>pom<packaging>
<project>

packaging type , need pom be used for parent And synthesize multiple projects . We need to add the corresponding value to the parent pom, Used for subproject inheritance .

If you want a parent project , The configuration is as follows :

<project>
<modelVersion>4.0.0<modelVersion>
<parent>
<groupId>org.codehaus.mojo<groupId>
<artifactId>my-parent<artifactId>
<version>2.0<version>
<relativePath>../my-parent<relativePath>
<parent>
<artifactId>my-project<artifactId>
<project>

relativePath No need , Because by artifactId You can find the sub item , But used to indicate parent The catalog of , For quick queries .

synthesis ( Or multiple modules )

A project has multiple modules , Also called multiple modules , Or composite projects .

<project>
<modelVersion>4.0.0<modelVersion>
<groupId>org.codehaus.mojo<groupId>
<artifactId>my-parent<artifactId>
<version>2.0<version>
<modules>
<module>my-project1<module>
<module>my-project2<module>
<modules>
<project>

Here from my-project1 and my-project2 Two project model synthesis

build To configure

It is mainly used for compiling settings , There are two main elements ,build and report, It is mainly divided into two parts , Collection of basic and extended elements

The basic elements
<build>
<defaultGoal>install<defaultGoal>
<directory>${basedir}/targetdirectory>
<finalName>${artifactId}-${version}finalName>
<filters>
<filter>filters/filter1.properties<filter>
<filters>
...
<build>

defaultGoal: Define default goals or phases . Such as install
directory: Compiled output directory
finalName: Generate the final file style
filter: Define filtering , Used to replace the corresponding property file , Use maven Properties defined . Set all placehold Value

resources (resource)

When you need resources in the project , It needs to be configured accordingly , Such as spring The configuration file ,log4j.properties

<project>
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus<targetPath>
<filtering>falsefiltering>
<directory>${basedir}/src/main/plexus<directory>
<includes>
<include>configuration.xml<include>
<includes>
<excludes>
<exclude>**/*.properties<exclude>
<excludes>
<resource>
<resources>
<testResources>
...
<testResources>
...
<build>
<project>

resources: resource A list of , Used to include all resources
targetPath: Specify the target path , Used to place resources , be used for build
filtering: Whether to replace the attribute in the resource placehold
directory: Where the resources are
includes: style , Including those resources
excludes: Excluded resources
testResources: Test resource list

plug-in unit

stay build when , Implemented plug-ins , The more useful part

<project>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins<groupId>
<artifactId>maven-jar-plugin<artifactId>
<version>2.0<version>
<extensions>false<extensions>
<inherited>true<inherited>
<configuration>
<classifier>test<classifier>
<configuration>
<dependencies>...<dependencies>
<executions>...<executions>
<plugin>
<plugins>
<build>
<project>

extensions: true or false, Whether to load plug-in extensions . Default false
inherited: true or false, Whether this plug-in configuration will be applied to poms, Those projects that inherit from this
configuration: Specify plug-in configuration
dependencies: Plug ins need packages to depend on
executions: Used for configuration execution The goal is , A plug-in can have multiple goals .
The following code :

<plugin>
<artifactId>maven-antrun-plugin<artifactId>
<executions>
<execution>
<id>echodirid>
<goals>
<goal>run<goal>
<phase>verify<phase>
<inherited>false<inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}<echo>
<tasks>
<configuration>
<execution>
<executions>
<plugin>

id: Regulations execution Unique logo for
goals: It means a goal
phase: Represents the stage , At what stage will the goal be implemented
inherited: Like the elements above , Set up false maven Inheritance to child plug-ins will be rejected
configuration: Represents the configuration property of this execution

Organization Configure organization information

<organization>
<name>wingstudio</name>
<url>http://www.wingstudio.org</url>
organization>

Developers Configure Developer Information

<developers>
<developer>
<id>beifengtz<id>
<name>beifeng<name>
<email>[email protected]<email>
<url>http://blog.beifengtz.com<url>
<organization>wingstudio</organization>
<Url>http://www.wingstudio.org<Url>
<roles>
<role>architect<role>
<role>developer<role>
<roles>
<timezone>-6timezone>
<properties>
<picUrl>http://blog.beifengtz.com<Url>
<properties>
<developer>
<developers>

reference : 《 Master Spring4.x》、 Blog (https://blog.csdn.net/u012562943/article/details/51683978)

Welcome to visit my blog ~blog.beifengtz.com

发表回复