依赖分类器使用
classifier 区分同一构件的不同变体,如源码包、文档包、平台特定包。
classifier 概念
定义
classifier 是坐标的附加元素,区分同一版本的不同构建产物。
完整坐标格式
XML
groupId:artifactId:version:type:classifier
com.example:my-lib:1.0.0:jar:sources
常见 classifier
| classifier | 说明 |
|---|---|
| sources | 源码包 |
| javadoc | API 文档包 |
| tests | 测试类包 |
| linux-x86_64 | Linux 平台包 |
| windows-x86_64 | Windows 平台包 |
引用源码包
基本语法
XML
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
<classifier>sources</classifier>
</dependency>
效果
下载 spring-core-5.3.20-sources.jar。
引用 javadoc
XML
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
<classifier>javadoc</classifier>
</dependency>
下载 spring-core-5.3.20-javadoc.jar。
平台特定包
不同平台依赖
XML
<dependency>
<groupId>com.example</groupId>
<artifactId>native-lib</artifactId>
<version>1.0.0</version>
<classifier>linux-x86_64</classifier>
</dependency>
多平台配置
XML
<profiles>
<profile>
<id>linux</id>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>native-lib</artifactId>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
</profile>
<profile>
<id>windows</id>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>native-lib</artifactId>
<classifier>windows-x86_64</classifier>
</dependency>
</dependencies>
</profile>
</profiles>
测试包引用
XML
<dependency>
<groupId>com.example</groupId>
<artifactId>my-lib</artifactId>
<version>1.0.0</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
引用测试类用于共享测试工具。
classifier vs type
区别
| 元素 | 说明 |
|---|---|
| type | 构件类型(jar、war、pom) |
| classifier | 同类型的不同变体 |
示例
XML
spring-core-5.3.20.jar → type=jar(默认)
spring-core-5.3.20-sources.jar → classifier=sources
spring-core-5.3.20-javadoc.jar → classifier=javadoc
打包带 classifier
maven-source-plugin
Bash
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal> <!-- 生成 sources.jar -->
</goals>
</execution>
</executions>
</plugin>
产物:my-lib-1.0.0-sources.jar
maven-javadoc-plugin
text
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
产物:my-lib-1.0.0-javadoc.jar
自动下载源码和文档
IDEA 配置
text
Settings → Maven → Importing
☑ Download sources
☑ Download documentation
命令下载
text
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
classifier 传递性
不传递
classifier 依赖不会传递:
text
A → B:jar:sources
A 的依赖者不会获得 B:sources
仓库存储结构
text
com/example/my-lib/1.0.0/
├── my-lib-1.0.0.jar
├── my-lib-1.0.0.pom
├── my-lib-1.0.0-sources.jar
├── my-lib-1.0.0-javadoc.jar
├── my-lib-1.0.0-tests.jar
└── maven-metadata.xml
要点总结
- classifier 区分同一构件的不同变体
- sources 引用源码包
- javadoc 引用文档包
- tests 引用测试类包
- 平台特定包用 classifier 区分
- classifier 依赖不传递
- maven-source-plugin 打包源码包
📝 发现内容有误?点击此处直接编辑