Maven optional dependencies and excluded dependencies are simple to use

Optional dependencies

  • Optional dependencies refer to hiding the currently dependent resources from the outside.
    Insert image description here

In pom.xml ofmaven_04_dao, when introducing maven_03_pojo, addoptional

<dependency>
    <groupId>com.rqz</groupId>
    <artifactId>maven_03_pojo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递-->
    <optional>true</optional>
</dependency>

At this time, BookServiceImpl has reported an error, indicating that because maven_04_dao sets maven_03_pojo as an optional dependency, maven_02_ssm cannot reference the content in maven_03_pojo, causing the Book class to not be found.

exclude dependencies

  • Excluding dependencies means actively disconnecting dependent resources. Excluded resources do not need to specify versions.

For excluding dependencies, it refers to the fact that there are already dependencies. That is to say, maven_03_pojo has been used in the maven_02_ssm project through dependency transfer. What we need to do at this time is to exclude it, so next we need to modify the pom.xml of maven_02_ssm.

<dependency>
    <groupId>com.rqz</groupId>
    <artifactId>maven_04_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--排除依赖是隐藏当前资源对应的依赖关系-->
    <exclusions>
        <exclusion>
            <groupId>com.rqz</groupId>
            <artifactId>maven_03_pojo</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Of courseexclusionsThe tagss indicate that we can exclude multiple dependent jar packages in sequence. For example, maven_04_dao has dependencies on junit and mybatis. We You can also exclude them altogether.

<dependency>
    <groupId>com.rqz</groupId>
    <artifactId>maven_04_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--排除依赖是隐藏当前资源对应的依赖关系-->
    <exclusions>
        <exclusion>
            <groupId>com.rqz</groupId>
            <artifactId>maven_03_pojo</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Guess you like

Origin blog.csdn.net/rqz__/article/details/132079262