Maven 依賴使用注意
封裝 Maven SDK 的 Dependency 時,需要注意以下幾點:
版本控制:確保所依賴的 SDK 版本與當前應用程序的其他依賴項兼容,并在 pom.xml 文件中指定正確的版本號。
穩(wěn)定性:使用經(jīng)過穩(wěn)定測試和驗證的SDK,并避免使用不穩(wěn)定或已棄用的版本。
可靠性:確保所依賴的 SDK 來源可靠,并且可以隨時獲取到。
兼容性:考慮到不同操作系統(tǒng)、JVM 和其他環(huán)境的差異,確保所依賴的 SDK 在目標平臺上可用并兼容。
文檔說明:提供明確的文檔說明,包括如何使用該 SDK,以及在遇到常見問題或錯誤時如何解決。
發(fā)布方式:選擇合適的發(fā)布方式,例如將封裝后的 SDK 發(fā)布到 Maven 倉庫,方便其他項目進行依賴管理。
自動化測試:編寫自動化測試來確保所依賴的 SDK 行為符合預期,并且當 SDK 更新時能夠迅速發(fā)現(xiàn)并修復問題。
Maven 構建依賴范圍
在 Maven 中,dependency scope 的設置決定了依賴庫在不同構建階段的使用范圍和生命周期。下面是各個 dependency scope 的具體作用:
-
compile(默認依賴范圍):表示該依賴項需要被包含在項目的構建路徑中,同時也需要在運行時被加載。
-
provided:表示該依賴項需要在運行時被加載,但是在編譯和打包階段不需要被包含在構建路徑中,因為這些依賴項通常由容器或運行時環(huán)境提供,例如 Java Servlet API。
-
runtime:表示該依賴項需要在運行時被加載,但在編譯階段不需要包含在構建路徑中。
-
test:表示該依賴項只在測試時需要被加載,在編譯和打包階段不需要被包含在構建路徑中。
-
system:表示該依賴項在本地系統(tǒng)中已經(jīng)存在,需要手動指定其路徑來加載依賴項。
-
import:表示該依賴項的作用是將一個 Maven 項目的依賴項傳遞給另一個 Maven 項目。
當開發(fā)SDK時,需要特別注意依賴的作用域,以確保它們不會影響父依賴中的依賴關系。
通常建議將指定的依賴庫的 scope 設為 “provided”,以確保其不會影響父依賴庫中的傳遞依賴關系圖。
沖突的主要原因
Maven 項目依賴關系沖突的主要原因是來自不同依賴庫的同一依賴的版本差異。例如,如果項目中同時引入了 A 和 B 兩個依賴庫,且這兩個庫都依賴了 C 庫,A 依賴 C 版本為 1.0.0,而 B 依賴 C 版本為 2.0.0,則在 Maven 自動解決依賴關系時,可能會選擇其中一個版本作為最終解決方案,導致另一個依賴庫無法正常工作,或者產(chǎn)生不穩(wěn)定的行為。這種問題通常稱為「依賴沖突」(Dependency Conflict)。
為了解決依賴沖突問題,Maven 提供了一套依賴管理機制,包括依賴范圍(Scope)、依賴排除(Exclusion)、依賴傳遞(Dependency Mediation)等。其中,依賴范圍用于限定依賴庫在不同階段(如編譯、測試、運行)的使用范圍;依賴排除用于排除依賴庫中的某些依賴項;依賴傳遞用于解決依賴沖突,它通過在依賴庫之間建立依賴樹,遞歸查找并選擇合適的版本,以滿足所有依賴的需求。
Maven構建插件
<!--
- <dependencyConvergence>
標簽表示檢查一個項目中的依賴是否收斂,即是否存在相同的依賴但版本號不同的情況,如果存在就會給出警告或錯誤提示。
這個標簽主要用于確保項目的依賴版本一致,避免可能出現(xiàn)的沖突或錯誤。
- <requireReleaseDeps>
標簽表示強制要求項目的依賴必須是已經(jīng)發(fā)布的穩(wěn)定版本,不能使用任何快照版本或開發(fā)中版本。
這個標簽主要用于確保項目的依賴是穩(wěn)定和可靠的,避免可能出現(xiàn)的不穩(wěn)定或錯誤。
- <requireUpperBoundDeps>
標簽表示強制要求項目的依賴必須有一個明確的上限版本號,不能使用類似于 1.0.+ 這樣的通配符版本號。
這個標簽主要用于確保項目的依賴是可控的,避免可能出現(xiàn)的版本不清晰或沖突的情況。
-->
<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>
標簽表示檢查一個項目中的依賴是否收斂,即是否存在相同的依賴但版本號不同的情況,如果存在就會給出警告或錯誤提示。
這個標簽主要用于確保項目的依賴版本一致,避免可能出現(xiàn)的沖突或錯誤。
- <requireReleaseDeps>
標簽表示強制要求項目的依賴必須是已經(jīng)發(fā)布的穩(wěn)定版本,不能使用任何快照版本或開發(fā)中版本。
這個標簽主要用于確保項目的依賴是穩(wěn)定和可靠的,避免可能出現(xiàn)的不穩(wěn)定或錯誤。
- <requireUpperBoundDeps>
標簽表示強制要求項目的依賴必須有一個明確的上限版本號,不能使用類似于 1.0.+ 這樣的通配符版本號。
這個標簽主要用于確保項目的依賴是可控的,避免可能出現(xiàn)的版本不清晰或沖突的情況。
-->
<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 的生命周期是自動化的,用戶無需手動執(zhí)行各個階段。Maven 使用插件來完成各個階段的任務,用戶只需要在 pom.xml 文件中配置需要的插件和參數(shù)即可。
例如,要在項目中編譯 Java 代碼,可以在 pom.xml 文件中配置 Maven Compiler Plugin:文章來源:http://www.zghlxwxcb.cn/news/detail-471236.html
<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 階段執(zhí)行,自動編譯源代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-471236.html
到了這里,關于Maven——SDK中的構建范圍,構建插件,構建參數(shù)說明的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!