Maven 导入范围不导入测试依赖项
Maven import scope not importing test dependencies
我想在 POM 中定义大量 compile
和 test
依赖项,以便导入到其他 POM 中。对于 compile
依赖项,一切都按预期工作。但是 test
依赖项没有在我的项目的 POM 中解析。
她是一个极简主义的例子:
待导入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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<packaging>pom</packaging>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>
</project>
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-using-import</artifactId>
<packaging>pom</packaging>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<version>1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</project>
当我运行一个dependency:tree
时,它只显示commons-lang3
的compile
依赖,而不是JUnit test
依赖:
>mvn dependency:tree
. . .
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pom-using-import ---
[INFO] com.example.maven.import:pom-using-import:pom:1
[INFO] \- com.example.maven.import:pom-to-be-imported:pom:1:import
[INFO] \- org.apache.commons:commons-lang3:jar:3.8:runtime
[INFO] ------------------------------------------------------------------------
如何才能同时导入 test
依赖项? 我在 maven documentation 表示导入仅适用于 compile
依赖项。
import
范围仅适用于 dependencyManagement
部分。 (这个概念在maven中也称为BOM。)来自maven documentation:
- import
This scope is only supported on a dependency of type pom in the
<dependencyManagement> section.
你有几个选择来实现我认为你想要的:
- 通过将
import
作用域依赖项添加到 pom-using-import 中的 dependencyManagement
部分来正确使用导入机制。然后您可以删除 'pom-to-be-imported' 中的 <dependencies>
部分。每个 "importing pom" 必须明确列出 <dependencies>
部分中的所有依赖项,但您不必提供版本(或范围)。
<dependencyManagement>
<dependency>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<version>1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencyManagement>
- 如果所有 "importing poms" 都需要相同的依赖项,您可以通过将 'pom-to-be-imported' 声明为父项而不是使用 import:
来使用继承
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId> <!-- Rename it to e.g. 'parent' -->
<version>1</version>
<relativePath>../pom-to-be-imported/pom.xml</relativePath>
</parent>
<artifactId>pom-using-import</artifactId>
备选方案 2 可能最接近您的要求。
我想在 POM 中定义大量 compile
和 test
依赖项,以便导入到其他 POM 中。对于 compile
依赖项,一切都按预期工作。但是 test
依赖项没有在我的项目的 POM 中解析。
她是一个极简主义的例子:
待导入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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<packaging>pom</packaging>
<version>1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>
</project>
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-using-import</artifactId>
<packaging>pom</packaging>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<version>1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</project>
当我运行一个dependency:tree
时,它只显示commons-lang3
的compile
依赖,而不是JUnit test
依赖:
>mvn dependency:tree
. . .
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pom-using-import ---
[INFO] com.example.maven.import:pom-using-import:pom:1
[INFO] \- com.example.maven.import:pom-to-be-imported:pom:1:import
[INFO] \- org.apache.commons:commons-lang3:jar:3.8:runtime
[INFO] ------------------------------------------------------------------------
如何才能同时导入 test
依赖项? 我在 maven documentation 表示导入仅适用于 compile
依赖项。
import
范围仅适用于 dependencyManagement
部分。 (这个概念在maven中也称为BOM。)来自maven documentation:
- import
This scope is only supported on a dependency of type pom in the <dependencyManagement> section.
你有几个选择来实现我认为你想要的:
- 通过将
import
作用域依赖项添加到 pom-using-import 中的dependencyManagement
部分来正确使用导入机制。然后您可以删除 'pom-to-be-imported' 中的<dependencies>
部分。每个 "importing pom" 必须明确列出<dependencies>
部分中的所有依赖项,但您不必提供版本(或范围)。
<dependencyManagement>
<dependency>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId>
<version>1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencyManagement>
- 如果所有 "importing poms" 都需要相同的依赖项,您可以通过将 'pom-to-be-imported' 声明为父项而不是使用 import: 来使用继承
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.maven.import</groupId>
<artifactId>pom-to-be-imported</artifactId> <!-- Rename it to e.g. 'parent' -->
<version>1</version>
<relativePath>../pom-to-be-imported/pom.xml</relativePath>
</parent>
<artifactId>pom-using-import</artifactId>
备选方案 2 可能最接近您的要求。