May 26, 2021 Maven
One of Maven's core features is its reliance on management. O nce we start working with multi-module engineering, which consists of hundreds of sub-modules or sub-engineerings, the dependencies between modules become very complex and management becomes difficult. In this case, Maven provides a highly controlled approach.
This situation is often seen when one library A depends on another library B. Another project C wants to use library A, then the project also needs to use library B.
Maven avoids the need to search for all the library resources it needs. By reading the dependencies in the .xml (pom) and the project, Maven can identify the dependencies between the projects.
We just need to define the direct dependencies in the pom file for each project. Maven will automatically take over the follow-up work.
By passing dependencies, the graphics of all included libraries can grow rapidly. W hen duplicate inventory is in, what may happen will continue to rise. Maven provides features to control the extent of the dependencies that can be passed on.
Function | Description of the function |
---|---|
Depends on adjustment | Decide which dependent version will be used when multiple manually created versions appear at the same time. If the depth of the two dependency versions is the same in the dependency tree, the first declared dependency will be used. |
Rely on management | A version that is directly specified and manually created is used. For example, when a project C contains project B in its own management module, i.e. B depends on A, then A can specify the version used when B is referenced. |
The dependency range | Contains dependencies at each stage of the build process. |
Dependency exclusion | Any passable dependencies can be excluded through the "exclusion" element. For example, A depends on B, B depends on C, so A can mark C as "excluded." |
Dependency is optional | Any passable dependencies can be marked as optional by using the "optional" element. F or example: A depends on B, and B depends on C. Therefore, B can mark C as optional so that A can no longer use C. |
Pass-through dependency discovery can be limited by using the following dependency scopes:
Range | Describe |
---|---|
The compilation phase | This range indicates that the dependency is valid only under the class path of the project. The default value. |
Supply phase | This range indicates that the dependency is provided by the JDK or network server at runtime. |
The run stage | This scope indicates that dependencies are not required during the compilation phase, but are necessary during the execution phase. |
The test phase | This scope indicates that the dependency is only in the test compilation and execution phases. |
System phase | This range indicates that you need to provide a system path. |
The import phase | The scope is used only if the dependency is a dependency defined in a pom. At the same time, a partially defined dependency in the POM file of the current project can replace a particular POM. |
Usually, under a common project, there is a series of works. I n this case, we can create a publicly dependent pom file that contains all the public dependencies that we call the pom parent of the other child project pom. The next example can help you better understand the concept.
Here are the details of the dependency chart above:
The POM files for App-UI-WAR are as follows:
<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.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-UI-WAR</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Core-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
The POM file for App-Core-lib is as follows:
<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.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Root</artifactId>
<groupId>com.companyname.groupname</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Core-lib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
The POM file for App-Data-lib is as follows:
<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.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Root</artifactId>
<groupId>com.companyname.groupname</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
Root's POM file is as follows:
<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.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>Root</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.companyname.groupname1</groupId>
<artifactId>Lib1</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname2</groupId>
<artifactId>Lib2</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname3</groupId>
<artifactId>Lib3</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Now, when we build the App-UI-WAR project, Maven will find all the dependencies by traversing the dependency diagram and build the application.
From the example above, we can learn the following key concepts: