Maven 中的 classifier 属性用过没?

最近训练营有小伙伴问到松哥一个关于 Maven 依赖的问题,涉及到 classifier 属性,随机问了几个小伙伴,都说工作中没用到过,因此简单整篇文章和小伙伴们分享下。

Maven 大家日常开发应该都有使用,Maven 中有一个比较好玩的 classifier 属性不知道小伙伴们有没有用过?

简单聊一聊这个话题。

一 Classifier 属性的作用

Classifier 属性在 Maven 中的主要作用是用于区分同一 artifact 的不同版本或变种。在 Maven 的坐标系统中,一个 artifact 通常由 groupId、artifactId 和 version 三个基本元素确定。

然而,在某些情况下,我们可能需要为同一个 artifact 创建多个不同的版本或变种,比如源代码包、文档包或特定平台的二进制包等。这时,Classifier属性就派上了用场。

通过为 artifact 添加 Classifier 属性,我们可以为同一个 artifact 创建多个不同的附件(attachment)。这些附件可以是源代码、测试代码、文档、特定平台的二进制文件等。每个附件都有一个唯一的 Classifier 值,用于区分它们。这样,我们就可以在 Maven 仓库中存储和管理这些不同的 artifact 变种,并在构建过程中根据需要引用它们。

例如下面这张图:

如果我想引用第一个 shiro-web-2.0.0-jakarta.jar 该怎么写呢?下面这种写法显然不对,这种写法引入的是 shiro-web-2.0.0.jar

xml 复制代码
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>2.0.0</version>
</dependency>

二 在依赖中引用Classifier

当我们需要在其他 Maven 项目中引用具有 Classifier 的 artifact 时,我们需要在依赖声明中指定 classifier 属性。例如:

xml 复制代码
<dependencies>  
    <dependency>  
        <groupId>com.example</groupId>  
        <artifactId>my-artifact</artifactId>  
        <version>1.0.0</version>  
        <classifier>sources</classifier>  
        <type>jar</type>  
        <scope>compile</scope>  
    </dependency>  
</dependencies>

在上面的示例中,我们引用了一个 Classifier 值为 sources 的 artifact 变种。Maven 将根据 groupId、artifactId、version 和 classifier 的值在仓库中查找相应的 artifact,并将其添加到项目的依赖中。

如果是上面的 Shiro 依赖呢?写法如下:

xml 复制代码
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>2.0.0</version>
    <classifier>jakarta</classifier>
</dependency>

三 自定义 Classifier 属性

上面是我们引用别人的项目,配置 Classifier,如果要在自己的 Maven 项目中定义 Classifier,那么我们需要在 pom.xml 文件中进行相应的配置。具体来说,我们需要在 build 标签下添加 maven-jar-plugin 插件,并为其配置 classifier 属性。例如,如果我们想要创建一个包含源代码的 artifact 变种,可以这样做:

xml 复制代码
<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>3.2.0</version>  
            <executions>  
                <execution>  
                    <goals>  
                        <goal>jar</goal>  
                    </goals>  
                </execution>  
                <execution>  
                    <id>attach-sources</id>  
                    <goals>  
                        <goal>jar</goal>  
                    </goals>  
                    <configuration>  
                        <classifier>sources</classifier>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build>

在上面的示例中,我们为 maven-jar-plugin 插件添加了一个名为 attach-sources 的执行,并为其配置了 classifier 属性为 sources。这表示我们将创建一个包含源代码的 artifact 变种,其 Classifier 值为 sources。

四 总结

Maven 的 Classifier 属性为我们在构建和管理 Java 项目时提供了极大的灵活性。通过为 artifact 添加 Classifier 属性,我们可以创建多个不同的版本或变种,以满足不同的项目需求或构建环境。同时,我们还可以在依赖声明中引用具有 Classifier 的 artifact,从而轻松地将其集成到其他项目中。

相关推荐
liO_Oil1 分钟前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装
奈斯。zs20 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody205020 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术22 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
【D'accumulation】35 分钟前
典型的MVC设计模式:使用JSP和JavaBean相结合的方式来动态生成网页内容典型的MVC设计模式
java·设计模式·mvc
试行1 小时前
Android实现自定义下拉列表绑定数据
android·java
Narutolxy1 小时前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
茜茜西西CeCe1 小时前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发
救救孩子把1 小时前
Java基础之IO流
java·开发语言
小菜yh1 小时前
关于Redis
java·数据库·spring boot·redis·spring·缓存