What is the purpose of Mavens dependency declarations classifier property?
I have a pom.xml file and in that i see that their are 3 dependencies referenced for same <artifactId>
the difference are in tags
<classifier>sources</classifier>
<classifier>javadoc</classifier>
I have deleted the dependencies that had the SOURCES/JAVADOC
and only kept one dependency. I tested my application and every thing work fine.
What is the purpose of using this classifier tag? and why i need to duplicate dependencies twice for adding <classifier>
tag with 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>
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
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>
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.