What is the purpose of Mavens dependency declarations classifier property?

91,117

Solution 1

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

Solution 2

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>

Solution 3

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.

Solution 4

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.

Share:
91,117
pushya
Author by

pushya

Updated on July 08, 2022

Comments

  • pushya
    pushya almost 2 years

    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> 
    
  • pushya
    pushya over 10 years
    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?
  • Ian Roberts
    Ian Roberts over 10 years
    @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.
  • pushya
    pushya over 10 years
    @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?
  • Ian Roberts
    Ian Roberts over 10 years
    @pushya More than likely, yes. Try it and see what happens.
  • Alan Snyder
    Alan Snyder over 3 years
    Where can I find an explanation of this syntax: <classifier>[openjpa|eclipselink]</classifier>
  • pirho
    pirho over 3 years
    @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.