16

We are currently attempting to port a very (very) large project built with ant to maven (while also moving to svn). All possibilities are being explored in remodeling the project structure to best fit the maven paradigm.

Now to be more specific, I have come across classifiers and would like to know how I could use them to my advantage, while refraining from "classifier anti-patterns".

Thanks

from: http://maven.apache.org/pom.html

classifier: You may occasionally find a fifth element on the coordinate, and that is the classifier. We will visit the classifier later, but for now it suffices to know that those kinds of projects are displayed as groupId:artifactId:packaging:classifier:version.

and

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. As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 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.

5 Answers 5

10

I think the correct question would be How to use or abuse attached artifacts maven? Because basicaly that is why classifiers are introduced - to allow you to publish attached artifacts.

Well, Maven projects often implicitely use attached artifacts, e.g. by using maven-javadoc-plugin or maven-source-plugin. maven-javadoc-plugin publishes attached artifact that contains generated documentation by using a javadoc classifier, and maven-source-plugin publishes sources by using sources classifier.

Now what about explicit usage of attached artifacts? I use attached artifacts to publish harness shell scripts (start.sh and Co). It's also a good idea to publish SQL scripts in the attached artifact with a classifier sql or something like that.

How can you attach an arbitary artifact with your classifier? - this can be done with build-helper-maven-plugin.

0
5

... I would like to know how I could use them to my advantage ...

Don't use them. They are optional and arbitrary.

If you are in the middle of porting a project over to maven, keep things simple and only do what is necessary (at first) to get everything working as you'd like. Then, after things are working like you want, you can explore more advanced features of maven to do cool stuff.

This answer is based on your question sounding like a "This features sounds neat, how can I use it even though I don't have a need for it?" kind of question. If you have a need for this feature, please update your question with more information on how you were thinking of utilizing the classifier feature and we will all be more informed to help you.

2
  • Thanks for your answer. Firstly, in regards to your comment, the basic thoughts had been to use a classifier in order to create different jars for a) code b) static content (ie jsp files, properties files, etc.) where at the moment both these types belong to the same project and are "taken care of" by the same ant build file. Second, I had asked the question the way I did in order to, perhaps, gain other insight into classifiers. Thanks again
    – Yaneeve
    Nov 8, 2011 at 5:34
  • 2
    I maintain my original suggestion of not using classifiers at first. Get your build converted over to Maven with as little changes as possible. Then, once things are working as you expect, your can try new things. Classifiers are simply labels for humans; Maven has no purpose or use for them. If you try them out now, it is just one more thing that could go wrong and demotivate you from completing the switch. If your developers start questioning "What is in this JAR?", you can add classifiers then, OR you can rename your projects more meaningfully so the question doesn't come up. Good luck!
    – Jesse Webb
    Nov 8, 2011 at 17:39
4

In contrast to Jesse Web's answer, it is good to learn about classifiers so that you can leverage them and avoid having to refactor code in addition to porting to maven. We went through the same process a year or two ago. Previously we had everything in one code base and built together with ant. In migrating to maven, we also found the need to break out the various components into their own maven projects. Some of these projects were really libraries, but had some web resources (jsp, js, images, etc.). The end result was us creating an attached artifact (as mentioned by @Male) with the web resources, using the classifier "web-resources" and the type "war" (to use as an overlay). This was then, and still does after understanding maven better, the best solution to port an old, coupled, project. We are eventually wanting to separate out these web resources since they don't belong in this library, but at least it can be done as a separate task.

In general, you want to avoid having attached artifacts. This is typically a sign that a separate project should be created to build that artifact. I suggest looking at doing this anytime you are tempted to attach an artifact with a separate classifier.

1
  • Thanks for your two cents :) they are appreciated. This is a good answer, though I can't get over the bad vibes this question received when it had initially been posted - which is why so far I have refrained from using "optional and arbitrary" classifiers. As to the initially referenced project - it had never quite graduated into being built solely by maven... and in that respect attached artifacts where just zipped up and saved on a file server... sad...
    – Yaneeve
    Nov 9, 2014 at 6:29
2

I use classifiers to define supporting artefacts to the main artefact.

For example I have com.bar|foo-1.0.war and have some associated config called com.bar|foo-1.0-properties.zip

1
  • You can bundle the properties files directly into the jar and read them using the class loader.
    – Max
    Nov 22, 2015 at 11:21
1

You can use classifers when you have different versions of the same artifact that you want to deploy to your repository.

Here's a use case:

I use them in conjunction with properties in a pom. The pom has default values which can be overriden via the command line. Running without options uses the default property value. If I build a version of the artifact with different property values, I can deploy that to the repo with a classifier.

For example, the command:

mvn -DmyProperty=specialValue package install:install-file -Dfile=target/my-ear.ear -DpomFile=my-ear/pom.xml -Dclassifier=specialVersion

Builds a version of an ear artifact with special properties and deploys the artifact to my repo with a classifier "specialVersion".

So, my repo can have my-ear-1.0.0.ear and my-ear-1.0.0-specialVersion.ear.

1
  • If you have different versions of an artifact, you should probably just give them ... drumroll... different versions. I.e., change the Maven version. One important advantage is that you cannot accidentally pull in both versions as dependencies, which is possible with artifacts that only differ by classifier.
    – sleske
    Apr 26, 2016 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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