gpt4 book ai didi

java - 为什么从 BOM 引用版本的 Maven 子项不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:34:10 53 4
gpt4 key购买 nike

我创建了这个示例 POM 文件,这样我就不需要在每个子项目中复制依赖版本。我的要求是不需要每个项目都提供第三方版本。每个项目都应从 bom 文件继承库版本。我根据 documentation of Maven dependencies 创建了示例.我的maven版本是Apache Maven 3.2.5

我收到以下错误

[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project org.test.testapp:test-app:1.0-SNAPSHOT (/Users/skoya/workspace/test-app/pom.xml) has 2 errors
[ERROR] 'dependencies.dependency.version' for junit:junit:jar is missing. @ line 22, column 17
[ERROR] 'dependencies.dependency.version' for commons-cli:commons-cli:jar is missing. @ line 26, column 17

根bom pom文件

<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.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test-dependencies-bom</name>
<url>http://myorg.org</url>

<properties>
<commons-cli.version>1.3</commons-cli.version>
<junit.version>3.8.1</junit.version>
</properties>

<modules>
<module>test-dependencies</module>
</modules>
</project>

test-dependencies/pom.xml

<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.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>test-parent-pom</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<prerequisites>
<maven>3.0.0</maven>
</prerequisites>


<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

</dependencies>
</project>

引用 bom 文件的 Maven 项目文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test.testapp</groupId>
<artifactId>test-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
</project>

最佳答案

有两个问题:

  1. 为了导入依赖项,导入的 pom 项目 (test-dependencies/pom.xml) 应该在 <dependencyManagement> 中定义要导入的依赖项部分,而不仅仅是 <dependencies>正如在您的原始样本中所做的那样。
  2. 导入该 pom 的项目需要导入声明依赖项的项目 (test-dependencies/pom.xml),而不是父 pom.xml。

我通过进行以下更改解决了这个问题。

test-dependencies/pom.xml

<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.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test-parent-pom</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<prerequisites>
<maven>3.0.0</maven>
</prerequisites>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

引用 bom 文件的 Maven 项目文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test.testapp</groupId>
<artifactId>test-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
</project>

在那之后,我能够构建并验证导入 pom 的项目是否引入了预期的依赖项:

mvn dependency:tree
...
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-app ---
[INFO] org.test.testapp:test-app:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:compile
[INFO] \- commons-cli:commons-cli:jar:1.3:compile

请注意,它仍然成功地从父 pom.xml 中提取了属性中定义的版本号。这是构建项目的一种完全有效的方式。它只需要我描述的 2 项调整。

关于java - 为什么从 BOM 引用版本的 Maven 子项不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41816411/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com