1. Configuration file
The configuration file of maven mainly includes two files: and .
- Among them, in the maven installation directory, such as: D:\Program Files\apache-maven-3.6.3\conf\ is the global configuration file
- In the .m2 subdirectory of the user directory, such as: C:\Users\chenxc.m2\ configuration is only for the current user's configuration file
- The main configuration of the current project under the project root path is the configuration of the current project.
Local configuration takes precedence over global configuration. Configuration priority from high to low:
> user settings > global settings
2. Detailed configuration explanation
1、localRepository
This value is the path to the local repository of this build system.
The default value is${}/.m2/repository
。
This element is especially useful for the main build server and allows all logged-in users to build from a public local repository.
<localRepository>D:\repository</localRepository>
2、interactiveMode
Indicates whether you can interact with the user to get input, default is true.
<interactiveMode>true</interactiveMode>
3、offline
Indicates whether this build system should run in offline mode, defaults to false.
This element is useful for building servers that cannot connect to remote repositories due to network settings or security reasons.
<offline>false</offline>
4、pluginGroups
Plugin group, which contains a list of elementspluginGroup
, each element contains a groupId.
When a plugin is used and the groupId is not provided in the command line, the list is searched. This list containsand
。
<pluginGroups> <pluginGroup></pluginGroup> </pluginGroups>
As follows, there is no setting in the pom file<groupId>
, then it will<pluginGroups>
Search plugin for location specified in the list
<!-- Package skip test --> <plugin> <!-- <groupId></groupId> --> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin>
5、servers
Private server server configuration, configure the username and password of the private server.
The configured private server can be used to publish jar packages and pomdistributionManagement
The repository IDs configured in the tag correspond to each other.
<servers> <server> <id>server001</id> <username>my_login</username> <password>my_password</password> <privateKey>${}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> <configuration></configuration> </server> </servers>
-
id: The ID and
<distributionManagement>
middle<repository>
Elemental<id>
Match (note that it is not the id of the user login).
As shown in the pom file<distributionManagement>
middle<repository>
Elemental<id>
To be in the file<server>
of<id>
Only when the values are consistent can the match.
<distributionManagement> <repository> <id>server001</id> <url>http://121.***.***.51:1481/repository/maven/</url> </repository> </distributionManagement>
- username、password: These two elements appear in a pair, indicating the login name and password required to authenticate to the server.
-
privateKey、passphrase: Like the first two elements, a private key location is specified (default is
${}/.ssh/id_dsa
) and private key password (private key password may not be available). future<passphrase>
and<password>
Elements may be extracted externally, but currently they must be declared in the file as plain text. - filePermissions、DirectoryPermissions: These are the permissions to use when creating a repository file or directory at deployment time. Each legal value is a three-digit number corresponding to the unix file permissions, such as 664 or 775.
Notice: If you use a private key<privateKey>
Log in to the server, make sure that it is not filled in<password>
element. otherwise<privateKey>
Will be ignored.
Password encryption
A new feature has been added in 2.1.0+ - Server Password and Password Encryption. pleaseSee this pageDetails of
6、mirrors
<mirrors> <mirror> <id></id> <name>PlanetMirror Australia</name> <url>/pub/maven2</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
-
id , name: The unique identifier and user-friendly name of this image. id is used to distinguish mirror elements and when connected to the mirror
<servers>
Select the corresponding credentials in part. - url: The basic URL of the image. The build system uses this URL to connect to the repository, not the original repository URL.
-
mirrorOf: The id of the mirrored server. For example, to point to the image of the Mavencentral repository ( /maven2/), set this element to central. More advanced mappings are as follows:
-
*
Match all repository IDs. -
external:*
Match all repositories except using localhost or file-based repositories. This option is available when you want to exclude redirect repositories defined for integration tests. - Starting with Maven 3.8.0,
external:http:*
Matches all repositories that use HTTP except localhost. - Multiple repositories can be specified using commas as delimiters, such as
repo,repo1
= repo or repo1 -
!
Can be used in conjunction with one of the above wildcards to exclude repository IDs, such as!repo1
Everything except repo1
-
Notice:
A given repository can have up to one image. In other words, you cannot map a single repository to a set of all definitions the same<mirrorOf>
A mirror of the value. Maven does not aggregate the images, but selects the first match.
7、proxies
<proxies> <proxy> <id>myproxy</id> <active>true</active> <protocol>http</protocol> <host></host> <port>8080</port> <username>proxyuser</username> <password>somepassword</password> <nonProxyHosts>*.|</nonProxyHosts> </proxy> </proxies>
-
id: The unique identifier of the agent. This is used to distinguish
<proxy>
element. - active: true if this proxy is active. This is useful for declaring a set of proxy, but only one proxy is active at a time.
-
protocol、host、port:
protocol://host:port
The protocol, host, and port elements are respectively the proxy. - username、password: These elements appear in a pair of forms, representing the login name and password required to authenticate this proxy server.
- nonProxyHosts: This is the list of hosts that should not be proxyed. The delimiter for this list is specified by the proxy server; in the example, vertical line delimiter is used, and comma delimiter is also common.
8、profiles
In the file<profile>
It's in pom<profile>
part of<profile>
Included<activation>
、 <repositories>
、<pluginRepositories>
、<properties>
These four main elements.
Because they focus on the entire build system (this is what the file does), rather than the single project object model setting. If one is in<profile>
is activated, its value will overwrite any other definition with the same id in the pom<profile>
。
<profiles> <profile> <id>test</id> ... </profile> </profiles>
8.1、activation
<activation>
It is used to determine the<profile>
Whether it is activated when<profile>
When activated,<repositories>
、<pluginRepositories>
、<properties>
The configuration in these three elements takes effect.
<activation>
Therefore, it is only meaningful to use this element at least at the same time as one of the other three elements.
<profiles> <profile> <id>test</id> <activation> <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <file> <exists>${basedir}/</exists> <missing>${basedir}/</missing> </file> </activation> ... </profile> </profiles>
-
activeByDefault:
<profile>
The default activation flag -
jdk: When running the java program
jdk
When the version matches the specified version<profile>
Will be activated. For example: In the above example, when running the java programjdk
When the version is 1.5.0_06<profile>
Will be activated. The jdk version also supports range configuration. For more details on supporting scope matching, seemaven-enforcer-plugin 。 -
os: The os element can define some operating system specific properties shown above. When the condition is met
<profile>
Will be activated-
name: The operating system name matches the
<profile>
。 -
family: The family to which the operating system belongs is activated
<profile>
。 -
arch: The operating system architecture activates the
<profile>
。 -
version: Activate this on the operating system version
<profile>
。
-
name: The operating system name matches the
-
property: If Maven detects the corresponding name=value attribute (its value can be passed in pom
${name}
cited), then<profile>
Will be activated. If the value field is empty, then the existing attribute name field will be activatedprofile
, such as: if there ismavenVersion=2.0.3
,So<profile>
Will be activated. -
file: Activate by the presence or loss of a given file
<profile>
-
exists: Activate if the specified file exists
<profile>
。 -
missing: Activate if the specified file does not exist
<profile>
。
-
exists: Activate if the specified file exists
Notice:
The relationship between these multiple activation conditions is as follows: Before Maven 3.2.2, it was possible to activate the<profile>
, after Maven 3.2.2, all can be activated<profile>
。
8.2、properties
correspond<profile>
list of extended attributes. Can be used to store some values.
These values can be marked anywhere in the pom file${X}
To use, here X refers to the name of the property (in the following example${}
)。
<profiles> <profile> ... <properties> <>${}/our-project</> </properties> ... </profile> </profiles>
The value of an attribute comes in five different forms
-
: Prefixed with "env." to a variable will return a shell environment variable. For example:
Refers to the $path environment variable (%PATH% on Windows).
-
: Refers to the corresponding element value in the pom. For example:
<project><version>1.0</version></project>
pass${}
Get the value of version. -
: Refers to the value of the corresponding element in it. For example:
<settings><offline>false</offline></settings>
pass${}
get<offline>
value. - Java system properties: All attributes that can be obtained through () can be obtained in pom using this form, such as ${}.
-
x: exist
<properties/>
set in the element, or in an external file to${someVar}
Use in the form of
If we want to reference this in the fileAttributes, please note that you should use placeholder @ when referencing here
=@@
Only use in springboot projects@xxx@
(xxx is a custom attribute name)
8.3、repositories
Remote repository list, which is a set of remote repository lists used by Maven to populate the build system's local repository.
<profiles> <profile> ... <repositories> <repository> <id>codehausSnapshots</id> <name>Codehaus Snapshots</name> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> <url>/maven2</url> <layout>default</layout> </repository> </repositories> ... </profile> </profiles>
- id、name: Remote repository unique ID and remote repository name
-
releases、snapshots: How to handle the download of releases and snapshots in remote repositories
- enabled:trueorfalseIndicates whether the repository is enabled to download a certain type of component (release version, snapshot version).
- updatePolicy: This element specifies how often the update occurs. Maven compares the timestamps of local pom and remote pom. The options here are:always(Always),daily(default, daily),interval:X(Here X is a time interval in minutes), ornever(Never).
- checksumPolicy: What to do when the Maven verification component verification file fails -ignore, fail, or warning.
- layout: In the above description of the repository, it is mentioned that they all follow a common layout. This is basically correct. Maven 2's repository has a default layout; however, Maven has different layouts. Use this element to specify that Yesdefault(default) orlegacy(Legal).
8.4、pluginRepositories
Plugin repository, Maven plugins is a special dependency, defined separately from ordinary jar package dependency repository, and the structure is similar to repositories. For specific description, please refer to the above.
<profiles> <profile> ... <pluginRepositories> <pluginRepository> <id>myPluginRepo</id> <name>My Plugins repo</name> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> <url>https://maven-central-eu....com/maven2/</url> </pluginRepository> </pluginRepositories> ... </profile> </profiles>
9. activeProfiles
It contains a set of<activeProfile>
The value of each element is a<profile>
middle<id>
value in. Regardless of any environment settings,<activeProfile>
Any defined<profile>
All will be active.
If no matching configuration file is found, nothing will happen. and<activation>
Configuration comparison<activeProfiles>
The configuration is relatively simple and is also commonly used.
<activeProfiles> <activeProfile>env-test</activeProfile> </activeProfiles>
3. Configuration file
<parent> <!--The component identifier of the parent project --> <artifactId /> <!--The unique identifier of the parent project --> <groupId /> <!--Version of parent project --> <version /> <!-- Relative path to the parent project's file。 The default value is../。 MavenFirst, look for the parent project's where the current project is builtpom,Secondly, at this location on the file system(relativePathLocation),Then in the local warehouse,Finally, search for the parent project in the remote repositorypom。 Notice:If passed in parent project<modules>Submodule specified,And the submodule is in the parent project directory,This configuration is not required。If the child project is not in the parent project directory,This configuration should be specified。 --> <relativePath>../</relativePath> </parent> <!-- Model version --> <modelVersion>4.0.0</modelVersion> <!-- The only logo of a company or organization--> <groupId>-group</groupId> <!-- The only projectID-> <artifactId>project</artifactId> <!-- Version number --> <version>1.0</version> <!--Types of components generated by the project,For examplejar、war、ear、pom --> <packaging>jar</packaging> <!-- Properties Configuration --> <properties> <!-- Compilation-time encoding --> <>UTF-8</> <>2.3.</> </properties> <!-- Depend on configuration --> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter</artifactId> <version>${}</version> <scope>compile</scope> </dependency> </dependencies> <!-- Dependency Statement,Will not really introduce packages。Generally in the fatherpomMake a statement in,In thepomReally introduced --> <dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>hutool-core</artifactId> <version>${}</version> </dependency> </dependencies> </dependencyManagement> <!-- Compile and build related configurations --> <build> <!-- Plugin statement,Generally in the fatherpomStatement,In thepomReally introduced --> <pluginManagement> <plugins> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${}</version> </plugin> </plugins> </pluginManagement> <!-- Plugin introduction,In the fatherpomAfter introduction,All subspomIntroduced in China --> <plugins> <plugin> <groupId></groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.6.0.1398</version> </plugin> </plugins> </build> <!-- Remote repository configuration for current project --> <repositories> <repository> <id>aliyun-public</id> <url>/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <!-- Remote plug-in repository configuration for current project --> <pluginRepositories> <pluginRepository> <id>aliyun-public</id> <url>/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> <!--jarPackage release private server configuration--> <distributionManagement> <!--Release version--> <repository> <!-- thisIDand middleservermiddle配置的服务器进行对应 --> <id>maven-releases</id> <name>releases</name> <url>/repository/maven-releases/</url> <uniqueVersion>true</uniqueVersion> </repository> <!--Snapshot version--> <snapshotRepository> <id>maven-snapshots</id> <name>snapshots</name> <url>/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <!--Dynamic configuration build,By setting up the activeprofile,profilemiddle的配置会作用于当前的项目编译构建 --> <profiles> <profile> <id>dev</id> <properties> <>dev</> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <>prod</> </properties> </profile> </profiles>
4. Loading of remote warehouses
Maven repository depends on download order:
- Look for dependencies in the local repository configured in the file, if not found, go to step 2.
- Search in the global remote repository configured in the file, if not found, go to step 3.
- Look for the remote repository configured in the current project, and if it is not found, go to step 4.
- Look in central repository /maven2, if not found, throws a dependency cannot load exception.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.