Important concepts and best practices of maven

Posted on

What is Maven

Maven is a cross platform project management tool, which mainly serves for project construction, dependency management and project information management based on Java platform.

What is construction

In addition to writing source code, we spend a considerable part of our time every day compiling, running unit tests, generating documents, packaging and deploymentstructurebuild)。

Maven is an excellent build tool

Maven can automate the build process, from cleaning, compiling, testing, to generating reports, to packaging and deployment. Maven maximally eliminates the duplication of construction, abstracts the construction life cycle, and provides implemented plug-ins for most construction tasks. We no longer need to define processes, or even implement some tasks in these processes.
The simplest example is testing. We don’t need to tell Maven to test, let alone tell Maven how to run tests. We just need to write test cases according to Maven’s convention. When we run the build, these tests will run automatically.

Maven is more than just a build tool

Maven is not only a build tool, but also a dependency management tool box project information management tool. It provides a central repository to help us download components automatically.

Maven dependency

Root elementprojectNextdependenciesCan contain one or moredependencyElement to declare one or more project dependencies. Each dependency can contain the following elements:

  • groupId, artifactId, version: basic coordinates of the dependency
  • type: dependency type. In most cases, you don’t have to declare it. The default is jar
  • scope: scope of dependency
  • optional: is the tag dependency optional
  • exclusions: used to exclude transitive dependencies

Scope of dependence

First of all, make sure that Maven uses different Classpaths at different times. Maven uses compile classpath when compiling the main code of the project, test classpath when compiling and executing tests, and runtime classpath when actually running Maven project.
Scope of dependenceIt is used to control the relationships that depend on the three Classpaths (compile classpath, test classpath, and runtime classpath). Maven has the following dependency ranges:

  • compile: compile dependency range. If it is not specified, it is used by default
  • test: Test dependency scope. Maven dependency using this dependency scope is only valid for test classpath
  • provided: dependency scope has been provided. Maven dependency using this dependency scope is valid for compile and test classpath, but not at run time
  • runtime: runtime dependency scope. Maven dependency using this dependency scope is valid for test and runtime classpath, but invalid when compiling main code
  • system: system dependency scope, which is consistent with three kinds of Classpaths and provided dependency scope

The relationship between the dependency range and the three Classpaths is as follows:

Scope of dependency Valid for compile classpath Valid for test classpath Valid for runtime classpath example
compile Y Y Y spring-core
test Y JUnit
provided Y Y servlet-api
runtime Y Y Implementation of JDBC Driver
system Y Y Class library file of local Maven warehouse position

Transitive dependency

Suppose a depends on B and B depends on C, then a is the first direct dependence on B, B is the second direct dependence on C, and a is the second direct dependence on CTransitive dependency. The first direct dependency and the second direct dependency determine the scope of transitive dependency. As shown in the following table, the leftmost column represents the first direct dependency scope, the top row represents the second direct dependency scope, and the cross cell in the middle represents the transitive dependency scope.

compile test provided runtime
compile compile / / runtime
test test / / test
provided provided / provided provided
runtime runtime / / runtime

Reliance on arbitration

Maven relies on Arbitration:

  • First principleThe shortest path first principle
  • Second principleThe first is the priority principle of declaration. On the premise that the length of dependency paths is equal, the top dependency declaration in POM takes precedence

For example, project a has such a dependency:
1. A, B, C, X (1.0), a, D, X (2.0), X (1.0) path length is 3, X (2.0) path length is 2. According to the first principle, X (2.0) will be parsed and used;
2. The length of dependency paths of a → B → y (1.0), a → C → y (2.0), y (1.0) and Y (2.0) are the same. If the dependency declaration of B is before C, then y (1.0) will be parsed and used.

Rely on related commands

  • mvn dependency:listList the final dependencies
  • mvn dependency:treeHit the dependency tree
  • mvn dependency:analyzeDependency analysis gives the used but undeclared dependencies and the unused but declared dependencies in the project

POM optimization

Dependency management

  • In a multi module project, a main POM is defined and used in the main POMdependencyManagementDefine dependencies, versions, and dependency exclusions
  • Submodules inherit dependencies from the main POM. Generally, do not exclude dependencies from submodules or specify dependency versions

Best practices

Categorical dependence

If multiple dependent versions are the same, you can use thepropertiesElement defines the Maven attribute, and the dependent version value is represented by this attribute reference.



reference material

  1. Maven in action
  2. Maven authoritative guide

Leave a Reply

Your email address will not be published.