Maven——SDK中的构建范围,构建插件,构建参数说明

Maven 依赖使用注意

封装 Maven SDK 的 Dependency 时,需要注意以下几点:

版本控制:确保所依赖的 SDK 版本与当前应用程序的其他依赖项兼容,并在 pom.xml 文件中指定正确的版本号。

稳定性:使用经过稳定测试和验证的SDK,并避免使用不稳定或已弃用的版本。

可靠性:确保所依赖的 SDK 来源可靠,并且可以随时获取到。

兼容性:考虑到不同操作系统、JVM 和其他环境的差异,确保所依赖的 SDK 在目标平台上可用并兼容。

文档说明:提供明确的文档说明,包括如何使用该 SDK,以及在遇到常见问题或错误时如何解决。

发布方式:选择合适的发布方式,例如将封装后的 SDK 发布到 Maven 仓库,方便其他项目进行依赖管理。

自动化测试:编写自动化测试来确保所依赖的 SDK 行为符合预期,并且当 SDK 更新时能够迅速发现并修复问题。

Maven 构建依赖范围

在 Maven 中,dependency scope 的设置决定了依赖库在不同构建阶段的使用范围和生命周期。下面是各个 dependency scope 的具体作用:

  1. compile(默认依赖范围):表示该依赖项需要被包含在项目的构建路径中,同时也需要在运行时被加载。

  2. provided:表示该依赖项需要在运行时被加载,但是在编译和打包阶段不需要被包含在构建路径中,因为这些依赖项通常由容器或运行时环境提供,例如 Java Servlet API。

  3. runtime:表示该依赖项需要在运行时被加载,但在编译阶段不需要包含在构建路径中。

  4. test:表示该依赖项只在测试时需要被加载,在编译和打包阶段不需要被包含在构建路径中。

  5. system:表示该依赖项在本地系统中已经存在,需要手动指定其路径来加载依赖项。

  6. import:表示该依赖项的作用是将一个 Maven 项目的依赖项传递给另一个 Maven 项目。

当开发SDK时,需要特别注意依赖的作用域,以确保它们不会影响父依赖中的依赖关系。
通常建议将指定的依赖库的 scope 设为 “provided”,以确保其不会影响父依赖库中的传递依赖关系图。

冲突的主要原因

Maven 项目依赖关系冲突的主要原因是来自不同依赖库的同一依赖的版本差异。例如,如果项目中同时引入了 A 和 B 两个依赖库,且这两个库都依赖了 C 库,A 依赖 C 版本为 1.0.0,而 B 依赖 C 版本为 2.0.0,则在 Maven 自动解决依赖关系时,可能会选择其中一个版本作为最终解决方案,导致另一个依赖库无法正常工作,或者产生不稳定的行为。这种问题通常称为「依赖冲突」(Dependency Conflict)。

为了解决依赖冲突问题,Maven 提供了一套依赖管理机制,包括依赖范围(Scope)、依赖排除(Exclusion)、依赖传递(Dependency Mediation)等。其中,依赖范围用于限定依赖库在不同阶段(如编译、测试、运行)的使用范围;依赖排除用于排除依赖库中的某些依赖项;依赖传递用于解决依赖冲突,它通过在依赖库之间建立依赖树,递归查找并选择合适的版本,以满足所有依赖的需求。

Maven构建插件

			<!--

            - <dependencyConvergence>
                标签表示检查一个项目中的依赖是否收敛,即是否存在相同的依赖但版本号不同的情况,如果存在就会给出警告或错误提示。
                这个标签主要用于确保项目的依赖版本一致,避免可能出现的冲突或错误。
            - <requireReleaseDeps>
                标签表示强制要求项目的依赖必须是已经发布的稳定版本,不能使用任何快照版本或开发中版本。
                这个标签主要用于确保项目的依赖是稳定和可靠的,避免可能出现的不稳定或错误。
            - <requireUpperBoundDeps>
                标签表示强制要求项目的依赖必须有一个明确的上限版本号,不能使用类似于 1.0.+ 这样的通配符版本号。
                这个标签主要用于确保项目的依赖是可控的,避免可能出现的版本不清晰或冲突的情况。

            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce-maven</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <version>3.6.3</version>
                                </requireMavenVersion>
                                <requireJavaVersion>
                                    <version>1.8</version>
                                </requireJavaVersion>
                                <dependencyConvergence />
                                <requireReleaseDeps />
                                <requireUpperBoundDeps />
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

插件主要解决问题

The Maven Enforcer Plugin is used to enforce certain rules and constraints on Maven projects to ensure their correctness and consistency. It provides a set of pre-defined rules that can be configured and executed during the build process to check various aspects of the project, such as the version of Java, the presence of certain files, and the validity of dependencies.

Some of the common problems that the Maven Enforcer Plugin can solve include:

  • Dependency conflicts: Enforce a specific version of a dependency to avoid conflicts with other dependencies.
  • Unsupported Java version: Enforce a specific version of Java to ensure that the project is compatible with the targeted platform.
  • Missing files or resources: Enforce the presence of required files or resources to ensure that the project can be built and run correctly.
  • Inconsistent configuration: Enforce consistent configuration across different modules or components of the project to avoid compatibility issues.

Overall, the Maven Enforcer Plugin helps to improve the quality and stability of Maven projects by enforcing best practices and standards.

Example

SDK应用工程

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gee.maven</groupId>
    <artifactId>mavenEnforcerExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavenEnforcerExample</name>
    <description>mavenEnforcerExample</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven-sdk</artifactId>
            <version>1.0-0</version>
        </dependency>

    </dependencies>


    <repositories>
        <repository>
            <id>aliyunmaven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--

            - <dependencyConvergence>
                标签表示检查一个项目中的依赖是否收敛,即是否存在相同的依赖但版本号不同的情况,如果存在就会给出警告或错误提示。
                这个标签主要用于确保项目的依赖版本一致,避免可能出现的冲突或错误。
            - <requireReleaseDeps>
                标签表示强制要求项目的依赖必须是已经发布的稳定版本,不能使用任何快照版本或开发中版本。
                这个标签主要用于确保项目的依赖是稳定和可靠的,避免可能出现的不稳定或错误。
            - <requireUpperBoundDeps>
                标签表示强制要求项目的依赖必须有一个明确的上限版本号,不能使用类似于 1.0.+ 这样的通配符版本号。
                这个标签主要用于确保项目的依赖是可控的,避免可能出现的版本不清晰或冲突的情况。

            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce-maven</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <version>3.6.3</version>
                                </requireMavenVersion>
                                <requireJavaVersion>
                                    <version>1.8</version>
                                </requireJavaVersion>
                                <dependencyConvergence />
                                <requireReleaseDeps />
                                <requireUpperBoundDeps />
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>





        </plugins>



    </build>

</project>

SDK工程


<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>maven-sdk</artifactId>
    <version>1.0-0</version>
    <name>${project.artifactId}</name>
    <description>Gee would like to happy with you</description>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <licenses>
        <license>
            <name>Apache 2</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
            <comments>A business-friendly OSS license</comments>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>gee</id>
            <name>gee</name>
            <email>green.gee.lu(at)gmail.com</email>
            <roles>
                <role>Tech Leader</role>
                <role>Developer</role>
                <role>CI/SCM Engineer</role>
            </roles>
            <timezone>+8</timezone>
            <url>https://github.com/ciertou</url>
        </developer>
    </developers>

    <dependencies>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

</project>

maven 中的生命周期

Maven 中的生命周期是构建过程中的一系列阶段,包括清理、编译、测试、打包、部署等。Maven 的生命周期分为三个阶段:clean、default 和 site。

clean 阶段包含了与项目清理相关的步骤,例如删除 target 目录等。

default 阶段包含了项目构建的主要步骤,包括编译、测试、打包、安装等。

site 阶段包含了生成项目站点的相关步骤,例如生成项目文档、测试报告等。

Maven 的生命周期是自动化的,用户无需手动执行各个阶段。Maven 使用插件来完成各个阶段的任务,用户只需要在 pom.xml 文件中配置需要的插件和参数即可。

例如,要在项目中编译 Java 代码,可以在 pom.xml 文件中配置 Maven Compiler Plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

该插件会在 default 阶段的 compile 阶段执行,自动编译源代码。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值