Search code examples
mavenmaven-install-plugin

Installing maven project locally with a classifier


If the version of a project foo is 1.0-SNAPSHOT, should running the command

 mvn install -Dclassifier=bar

install foo-1.0-SNAPSHOT-bar.jar in my local .m2 directory? I tried doing this but it installs foo-1.0-SNAPSHOT.jar and maven install plugin doesn't give too much detail about the -Dclassifier option.

Is there a way to install (locally) a jar with a classifier?


Solution

  • That -Dclassifier parameter is associated with the install:install-file goal, which is not the goal executed by the install phase of the default build lifecycle. Rather, it is used to install secondary artifacts in an ad hoc command line fashion. In other words, it's for sticking stuff in the repository outside the context of a normal running maven build.

    When you invoke the default build lifecycle with mvn install, he install phase executes the [install:install][1] goal. So, when you executed mvn install -Dclassifier=bar, you executed the default built lifecycle, and that parameter was not used for anything.

    The install:install goal, as it states in the docs, installs the primary artifact as well as an secondary, aka attached, artifacts. The jar produced by your build is the primary artifact. It does NOT have a classifier. All of the attached artifacts require a fourth maven coordinate to uniquely identify them from the primary artifact. This coordinate is the classifier.

    You have no need for a classifier unless you have attached artifacts. Where do attached artifacts come from? They are things that your build produces in addition to the primary artifact. So, it could be something built by the assembly plugin. It could be another jar containing the source files of your project, etc. The classifier is determined by the mechanism that attaches the secondary artifact, e.g. the assembly plugin.