Pagine

Wednesday 31 May 2017

Learning maven classifier with examples

The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.
A 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.

Example:

The following dependency refers to my-artifact-1.0.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>
The following dependency refers to my-artifact-1.0.source.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>sources</classifier>
</dependency>
The following dependency refers to my-artifact-1.0.javadoc.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>javadoc</classifier>
</dependency>
Another example, we suppose we have two jars with the same artifact id (my-artifact) but different group id as below:
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>com.othercompany</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>
Both jars have following name: my-artifact-1.0.jar. The use of classifier can be helpful:
 <dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>info</classifier>
</dependency>
So in this case the jar name is "my-artifact-1.0.info.jar" in this way simplifying the distinction between the two jars.

No comments:

Post a Comment