[英]Importing pom in dependency management
從文檔中閱讀,
- 不要嘗試導入在當前 pom 的子模塊中定義的 pom。 嘗試這樣做將導致構建失敗,因為它將無法找到 pom。
- 永遠不要將導入 pom 的 pom 聲明為目標 pom 的父級(或祖父級等)。 沒有辦法解決循環問題,會拋出異常。
- 當提及其 poms 具有可傳遞依賴項的工件時,項目需要將這些工件的版本指定為托管依賴項。 不這樣做將導致構建失敗,因為工件可能沒有指定版本。 (在任何情況下,這都應被視為最佳實踐,因為它可以防止工件的版本從一個構建更改為下一個構建)。
我有以下疑問:
a) 第 3 點是什么意思?
b) 在第一點,為什么 maven 無法找到子模塊 pom ? 子模塊不是在父模塊之前構建的嗎?
在第 3 點中,我需要更清楚地...When referring to artifacts whose poms have transitive dependencies the project will need to specify versions of those artifacts as managed dependencies...
因此,假設我們有project A
,我們將在project B
<dependencyManagement>
部分中導入該project B
。 現在,創建項目 A 的人必須在其<dependencyManagement>
部分中提及項目 A 的所有傳遞依賴項(非直接)的版本? 怎么會有人知道項目 A 的所有傳遞依賴項的這些版本?
我對第 1 點產生了另一個疑問。我基本上創建了一個帶有超級模塊和子模塊的骨架項目,基本上沒有 Java 代碼。 因此,我將分享他們的poms。 我們將看到構建成功,而根據第 1 點,情況不應該如此。
項目結構如下:
super模塊的pom如下:
<?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>com.home</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>my-project</name>
<url>http://www.example.com</url>
<modules>
<module>./sub-module1/pom.xml</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.home</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
子模塊的 pom 如下:
<?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>com.home</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
當我執行mvn clean compile
,它構建得很好。
另外,我看到即使子模塊 pom 包裝類型保留為 jar,范圍導入也不會引發錯誤。 它仍然可以很好地編譯。
對於未來的讀者,我將總結答案。 第 1 點和第 2 點都成立,但只有在 POM1(父模塊或超級模塊)導入 POM2(子模塊或子模塊)然后 POM2 需要 POM1 來解決依賴關系時才會出錯。
在第 1 點,這不會被找到。 在第 2 點,它會因繼承而被發現,但會創建一個循環。
下面我舉個例子讓大家驗證一下。
超級模塊POM
<?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>com.home</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>my-project</name>
<url>http://www.example.com</url>
<modules>
<module>./sub-module1/pom.xml</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.home</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
子模塊 POM
<?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>com.home</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</dependency>
</dependencies>
</project>
b) 在第一點,為什么 maven 無法找到子模塊 pom ? 子模塊不是在父模塊之前構建的嗎?
子模塊未安裝/部署在 GAV 參考(您的 Maven 緩存或遠程 Nexus 存儲庫)中
這意味着任何引用子模塊 pom 的項目都不會在子模塊 GAV 中找到該 pom,因為構建、安裝和部署的是主項目 GAV。
正如khmarbaise在評論中補充的那樣:
你永遠不應該在已經定義為子模塊的項目中執行
<scope>import</scope>
,這意味着列出了一個<module>..</module>
條目。 因為導入scope
的解析將在其余部分之前完成,這將導致構建失敗。
和:
a) 第 3 點是什么意思?
這意味着<dependencies>
部分應該聲明一些<dependency>
只包含<group>
和<artifact>
,而不是<version>
。
版本將從導入的<dependencyManagement>
。
另請參閱“在 Maven 中跟蹤托管依賴項版本”。
如此處所示,托管依賴項用於鎖定父 pom 上的版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.