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

这篇具有很好参考价值的文章主要介绍了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 阶段执行,自动编译源代码。文章来源地址https://www.toymoban.com/news/detail-471236.html

到了这里,关于Maven——SDK中的构建范围,构建插件,构建参数说明的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 构建SpringBoot工程时找不到插件“spring-boot-maven-plugin”

    目录 一、找不到插件“spring-boot-maven-plugin” 二、再次启动时,又发现报错         Unable to find a single main class from the following candidates [com.itheima.Application, com.example.springboot01.Springboot01Application]上面报错的意思是,有多个App类,它不知用哪个  三、把该插件打包后,在jar包的

    2024年02月07日
    浏览(44)
  • 【Docker】使用docker-maven-plugin插件构建发布推镜像到私有仓库

    本文描述了在Spring Boot项目中通过docker-maven-plugin插件把项目推送到私有docker仓库中,随后拉取仓库中的项目用docker run运行项目。作者自行构建,质量有保证。 1、要想使用 docker-maven-plugin ,需要在 pom.xml 中添加该插件; 注:注意下db:3306 2、我们构建镜像之前需要先将项目打包

    2024年02月15日
    浏览(36)
  • Jenkins 利用Build With Parameters 插件实现选择参数构建

    jenkins是CI/CD中部署时候一个重要工具,可以按照我们需求进行配置一些编译打包测试工作,有些是全自动化的,有些需要进行手动参数进行触发。今天给大家分享一下Jenkins使用Build With Parameters插件,实现外置参数进行触发job。 作者:良知犹存 转载授权以及围观:欢迎关注微

    2024年02月12日
    浏览(66)
  • Java中的Maven Shade插件是什么?

    Maven Shade插件是一个非常有用的Maven插件,它可以帮助你在构建项目时打包所有依赖项,并将其打包到一个单独的JAR文件中。这对于在构建过程中使用多个依赖项的项目非常有用,因为它可以让你避免在每个依赖项中都包含所有依赖项,从而减少构建过程中的开销。 在Java中,

    2024年02月13日
    浏览(29)
  • 使用cmake编译opencv过程中的可选参数说明

    opencv版本:4.5.5 通过源码编译opencv时,cmake可以带有很多可选参数,这里对这些可选参数进行说明。 配置选项可以通过几种不同的方式设置: 命令行:cmake-Doption=value… 初始缓存文件:cmake-C my_options.txt… 通过Cmake-GUI进行设定 在本参考中,我们将使用常规命令行。 最常用和有

    2024年02月16日
    浏览(41)
  • SpringBoot项目使用docker-maven-plugin插件构建docker镜像以及推送到docker hub或docker registry私服

    我们知道docker可将应用程序和基础设施层隔离,可更快地打包、测试以及部署应用程序。本文主要介绍SpringBoot项目如何构建docker镜像以及推送到私服或者docker hub服务器上。 本文介绍的方式是使用docker-maven-plugin的方式构建SpringBoot的docker镜像以及推送到私服或docker hub服务器上

    2024年02月16日
    浏览(35)
  • 3d max省时插件CG MAGIC功能中的材质参数可一键优化!

    渲染的最终结果就是为了让渲染效果更加真实的体现。 对于一些操作上,可能还是费些时间,VRay可以说是在给材质做加法的路上越走越远,透明度、凹凸、反射等等参数细节越做越多。 对于材质参数调节的重要性大家都心里有数的。 VRay材质系统的每一次加法和更加精细的

    2024年02月12日
    浏览(26)
  • Maven之mirrorof范围

    在配置阿里对官方中央仓库的镜像服务器时,我们使用到了 mirror 元素。 故名思意, mirror 用于配置网络仓库的『 镜像 』。当你准备去默认的中央仓库下载包和插件时,Maven 会『 拦截 』你的下载请求,转而去你所配置的『 镜像网址 』下载,从而也能实现从指定的国内的网

    2024年02月12日
    浏览(64)
  • maven的依赖范围scope使用

    标签的位置:dependencies/dependency/scope 标签的可选值:compile/test/provided/system/runtime/import #①compile 和 test 对比 main目录(空间) test目录(空间) 开发过程(时间) 部署到服务器(时间) compile 有效 有效 有效 有效 test 无效 有效 有效 无效 #②compile 和 provided 对比 main目录(空间

    2024年02月10日
    浏览(34)
  • Maven 基础之依赖管理、范围、传递、冲突

    坐标和 mvnrepository 网站 在 maven 中通过『 坐标 』概念来确定一个唯一确定的 jar 包。坐标的组成部分有: 元素 说明 groupId 定义当前 Maven 组织名称 artifactId 定义实际项目名称 version 定义当前项目的当前版本 [✔] 注意 任意两个不同包,它们的这三个属性必定至少有一项是不同

    2024年02月12日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包