96

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/JAVADOCand 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> 

5 Answers 5

78

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

4
  • 2
    According to the document says 'that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files' what do that mean? I think that's the reason why my pom.xml uses it. Why do you need to deploy the API docs and Source code along with packaged classes. Isn't deploy packaged classes not good enough?
    – pushya
    Commented Jan 3, 2014 at 18:28
  • 7
    @pushya typically when you deploy your artifacts to a public repository such as Maven central, you include the javadocs and sources so that IDEs with Maven support can do proper code completion and JavaDoc popups, and can step into the library code when debugging. Commented Jan 3, 2014 at 18:36
  • @IanRoberts that make sense now. so that mean i can remove the dependencies that has "SOURCE/JAVADOC" and they are optional and mainly serves the purpose being developer friendly when coding?
    – pushya
    Commented Jan 3, 2014 at 18:52
  • 1
    @pushya More than likely, yes. Try it and see what happens. Commented Jan 3, 2014 at 19:06
25

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>
2
  • Where can I find an explanation of this syntax: <classifier>[openjpa|eclipselink]</classifier> Commented Sep 15, 2020 at 17:31
  • @AlanSnyder it was just a "lazy coder shortcut" not any actually working syntax. I edited that part to make it more clear. [openjpa|eclipselink] was just a "selector" for choosing either one.
    – pirho
    Commented Sep 19, 2020 at 16:47
8

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.

3

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.

0

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.