在 Maven BOM(物料清单)中定义依赖范围是一种好习惯吗? [英] Is it good practice to define scope of dependencies in Maven BOM (bill of materials)?

查看:79
本文介绍了在 Maven BOM(物料清单)中定义依赖范围是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 pom.xml 用作 BOM(物料清单).定义的依赖项之一是 *-test 工件,用于测试使用此 BOM 中的库的代码.

I have a pom.xml like this to be used as a BOM (Bill of Materials). One of the defined dependencies is a *-test artifact used for testing your code that uses the libraries from this BOM.

问题是:指定 *-test 工件仅用于 BOM 本身中的 test 范围是否合适/好的做法,还是应该保留它如果需要,让 BOM 的用户在其项目的 POM 中指定?

The question is: is it appropriate / good practice to specify that the *-test artifact is just for test scope in the BOM itself, or should this be left for the users of the BOM to specify in their project's POM, if needed?

<?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.example.mylib</groupId>
    <artifactId>mylib-bom</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>MyLib (Bill of Materials)</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example.mylib</groupId>
                <artifactId>mylib-cool-library</artifactId>
                <version>${project.version}</version>
            </dependency>    
            <dependency>
                <groupId>com.example.mylib</groupId>
                <artifactId>mylib-test</artifactId>
                <version>${project.version}</version>
                <scope>test</scope> <!-- === HERE === -->
            </dependency>    
        </dependencies>
    </dependencyManagement>    
</project>

我正在研究现有项目是如何做到这一点的,例如,Spring 框架 BOM 确实没有明确定义任何范围.但我仍然想知道是否有一些不成文的规定或类似的规定?

I was looking at how existing projects do this and, for example, the Spring Framework BOM does not, indeed, define any scope explicitly. But I am still wondering if there is some unwritten rule for this or something like that?

推荐答案

最佳实践是让用户决定范围,不要在 BOM(或父 pom)中设置它.

Best practice is to let the user decide scope, and not set it in BOM (or parent pom).

当您在 BOM 中设置范围(除 compile 之外的任何内容)时,您会更改该依赖项的默认范围,如从用户项目中看到的那样.maven 中默认的依赖范围是 compile,所以当你希望它被编译时,通常的做法是省略依赖范围.如果 BOM 强加了另一个范围,对于使用 BOM 的其他开发者(甚至您自己)来说,这可能是一个令人讨厌的惊喜.

When you set scope in BOM (to anything other than compile) you change the default scope for that dependency, as seen from user projects. The default dependency scope in maven is compile, so it's common practice to omit scope for a dependency when you want it to be compile. If the BOM imposes another scope, it can be a nasty surprise for other developers that use the BOM (or even yourself at a later point in time).

这篇关于在 Maven BOM(物料清单)中定义依赖范围是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆