Maven - Dependency Management

Copyright unauthorized, prohibited reproduced


chapter


Mentioned earlier simple dependency management, this chapter will be described in detail.

Unless otherwise small projects, general project will usually rely on external jar package. The jar package management is very cumbersome, it will rely on other jar package jar package, if the manual management must have all of these jar into the project directory, and ensure that the versions of these jar package is correct. When the project becomes more and more, this work will become more complicated.

Using Maven to manage these dependent jar package will greatly simplify the work. You can specify the project depends on external libraries and versions POM file and maven download these jar package, and placed in the local library. If you need some other external jar package jar package, maven will download.

In the POM file <dependencies>element of the specified project dependencies.

Example:

<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.qikegu.demo</groupId>
    <artifactId>mybatis-demo</artifactId>
    <version>0.0.1</version>  
    <packaging>jar</packaging>  
    
    <name>mybatis demo </name>  
    <url>http://www.qikegu.com</url>  
    
    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.11.3</version>
    </dependency>


    <dependencies>  
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13-beta-2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>  
  
</project>  

The above configuration of the two dependencies. Each dependency by groupId, artifactId version and description.

When Maven POM execute this file, if the local library is not that two dependencies, maven central repository to download from them and placed in local libraries.

Sometimes there is no central repository dependencies needed, such as some third-party jar package, developers can manually download their own, and then into the local library. Jar package directory of the local library must match <dependency>the configuration in the directory format is:

groupId/artifactId/version

(.) GroupId replaced if a bit slash (/), such as directory packet is located above jsoup jar it is:

MAVEN_REPOSITORY_ROOT/org/jsoup/jsoup/1.11.3

Transitive dependencies

A dependency depend on other dependencies, maven will pass these dependencies, all required dependencies are included.

External dependencies

Refers to external dependencies do not exist in the maven central repository, the local repository, the remote repository jar package, such as a local hard drive jar package.

You may be configured by way of external dependencies.

<dependency>
  <groupId>com.qikegu.com</groupId>
  <artifactId>mydependency</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${basedir}\war\WEB-INF\lib\mydependency.jar</systemPath>
</dependency>

groupId artifactId and are set to the name dependencies, scope element set value is named system. systemPath element is set to point the position dependency comprising JAR file, $ {basedir} points to the directory where the POM, the rest of the path corresponding to the directory.

Snapshot dependence

Snapshots dependence refers to the latest version (snapshot version) is being developed using the dependency of each building always download the latest snapshot version.

To use a version of a snapshot version, the version number attached -SNAPSHOT.

<dependency>
    <groupId>com.qikegu.com</groupId>
    <artifactId>mydependency</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Preclude reliance

Because Maven will transitive dependencies, it may sometimes contain unwanted dependencies, for example, Java older version of a jar may be currently in use are not compatible. To solve this problem, Maven allows you to exclude specific dependencies.

Example:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sample.ProjectA</groupId>
      <artifactId>Project-A</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  <!-- 声明排除的依赖项 -->
          <groupId>sample.ProjectB</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>

maven when performing building will not download dependencies storage excluded.

Guess you like

Origin blog.csdn.net/weixin_43031412/article/details/92585354