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

news/2024/10/23 5:30:45/

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 阶段执行,自动编译源代码。


http://www.ppmy.cn/news/102731.html

相关文章

06. 数据结构之散列表

前言 散列表也叫作哈希表&#xff08;hash table&#xff09;&#xff0c;这种数据结构提供了键&#xff08;Key&#xff09;和值&#xff08;Value&#xff09;的映射关系。只要给出一个Key&#xff0c;就可以高效查找到它所匹配的Value&#xff0c;时间复杂度接近于O(1) 1.…

哪些因素会成为系统的瓶颈

CPU 如果存在大量的计算&#xff0c;他们会长时间不间断的占用 CPU 资源&#xff0c;导致其他资源无法争夺到 CPU 而响应缓慢&#xff0c;从而带来系统性能问题&#xff0c;例如频繁的 FullGC&#xff0c;以及多线程造成的上下文频繁的切换&#xff0c;都会导致 CPU 繁忙&…

从C#学习改善Java编码规范

最近在学习C#&#xff0c;感觉有些编码规范可以引入到Java项目中。 接口 C#中&#xff0c;Interface以大写字母I开头。例如&#xff0c;一个写数据的接口&#xff0c;命名为 IWriter。 Java中&#xff0c;则通常直接命名为Writer。 命名为IWriter的优势在于&#xff1a;一眼…

2023年下半年软考高级需要报班吗?

首先&#xff0c;对于软考高级考试报班与否的问题&#xff0c;需要根据自身的情况来做出决定。如果你有较强的自学能力&#xff0c;且具备丰富的实际工作经验和技术知识&#xff0c;那么不报班也完全可以自学备考。但如果你对软件工程的知识掌握程度较低&#xff0c;或者时间紧…

c语言全局变量和局部变量问题汇总

✅作者简介&#xff1a;嵌入式领域优质创作者&#xff0c;博客专家 ✨个人主页&#xff1a;咸鱼弟 &#x1f525;系列专栏&#xff1a;单片机设计专栏 &#x1f4c3;推荐一款求职面试、刷题神器&#x1f449;注册免费刷题 1、关键字static的作用是什么&#xff1f; 定义静态变…

服了呀,00后怎么这么卷....

现在的小年轻真的卷得过分了。前段时间我们公司来了个00年的&#xff0c;工作没两年&#xff0c;跳槽到我们公司起薪18K&#xff0c;都快接近我了。后来才知道人家是个卷王&#xff0c;从早干到晚就差搬张床到工位睡觉了。 最近和他聊了一次天&#xff0c;原来这位小老弟家里条…

掌握了它,软件测试拿下25K轻轻松松!

了解软件测试这行的人都清楚&#xff0c;功能测试的天花板可能也就15k左右&#xff0c;而自动化的起点就在15k左右&#xff0c;当然两个岗位需要掌握的技能肯定是不一样的。 如果刚入门学习完软件测试&#xff0c;那么基本薪资会在7-8k左右&#xff0c;这个薪资不太高主要是因…

Reptile-master编译安装

服务端和客户端都用一个包了。 设置好信息&#xff0c;就可以编译了。make config,然后是make&#xff0c;make install。 在客户端主机上也要make config 编译客户端的命令是make client 然后是进入output目录./client就可以控制服务端了。 主要是设置几个信息&#xff0c…