Mavens 依赖声明分类器属性的用途是什么?

我有一个 pom.xml 文件,在这个文件中,我看到它们是相同 <artifactId>引用的3个依赖项,不同之处在于标记

<classifier>sources</classifier>
<classifier>javadoc</classifier>

我已经删除了具有 SOURCES/JAVADOC的依赖项,并且只保留了一个依赖项。我测试了我的应用程序,一切工作正常。

使用这个分类器标签的目的是什么?以及为什么我需要重复依赖两次添加 <classifier>SOURCES/JAVADOC标签。

<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>javadoc</classifier>***
<scope>compile</scope>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>sources</classifier>***
<scope>compile</scope>
</dependency>
96084 次浏览

The classifier distinguishes artifacts that were built from the same POM but differ in content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.

Source

Example for Classifier
As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.8 but at the same time also an artifact that still supports JRE 1.7. The first artifact could be equipped with the classifier jdk18 and the second one with jdk14 such that clients can choose which one to use.

Another common use case for classifiers is the need to attach secondary artifacts to the project's main artifact. If you browse the Maven central repository, you will notice that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files.

It allows distinguishing two artifacts that belong to the same POM but were built differently, and is appended to the filename after the version.

For example if you have other artifacts in your repository (docs, sources ...) you can reference them and add them to your project as dependency. in this code by adding the <classifier>sources</classifier> we are getting the sources.jar from repository.

    <dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
<type>jar</type>
***<classifier>sources</classifier>***
<scope>compile</scope>
</dependency>

actually It lets you locate your dependencies with the further level of granularity.

According to the following: https://blog.packagecloud.io/eng/2017/03/09/how-does-a-maven-repository-work/ classifier tag has implies "Secondary Artifact", which its "transitive dependency" will be cut off! Thus classifier tag not only change "Maven Coordinate" by $artifactId-$version-$classifier.jar!

Yet another more pragmatic answer by an example to help to understand the usefulness of classifier better.

Suppose you have a need for two versions of an artifact: for openjpa and for eclipselink - say because jar contains entities that are needed to be enhanced JPA implementation specifically.

You might have some different handling for these builds defined in Maven profiles and the profiles used then have also property <classifier />.

To build the differently classified versions, in pom the maven-jar-plugin would then be configured followingly

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<classifier>${classifier}</classifier>
</configuration>
</plugin>

Installing both would result to files in repo something like this:

org/example/data/1.0.0/data-1.0.0.pom
org/example/data/1.0.0/data-1.0.0-openjpa.jar
org/example/data/1.0.0/data-1.0.0-eclipselink.jar

Now it would be only matter of classifier to which one use, so for OpenJPA, for example:

<dependency>
<groupId>org.example</groupId>
<artifactId>data</artifactId>
<version>1.0.0</version>
<classifier>openjpa</classifier>
</dependency>

and for EclipseLink you would switch classifier as:

<classifier>eclipselink</classifier>