I sometimes stumble upon Java libraries that are not available in Maven (or Gradle which seems to use the same ecosystem). To have to most convenient way of developing (ie. contributors should not have to download dependencies for themselves) I used to have my own Maven repository where I can upload such libraries and download them on any machine I try to run a build.

For this to work you can also invoke the maven deploy plugin using the pom.xml of the library in question but that requires you to change files with is not that convenient with libraries I don’t intend to change. And on top of that, you sometimes only have a .jar file available.

That’s why I tend to use the following oneliner to push a .jar file into my Nexus. To use this, you have to change some properties given in here.

The Command

mvn deploy:deploy-file -Durl=https://myrepo.example.com/repository/myrepo-releases/ \
  -DrepositoryId=myrepo-releases \
  -Dfile=/home/downloads/mylib.jar
  -DgroupId=com.example \
  -DartifactId=mylib \
  -Dversion=1.0.0 \
  -Dpackaging=jar \
  -DgeneratePom=true \
  -DgeneratePom.description="My library."

Properties and their meanings

  • -Durl The full URL to the Maven repository you want to deploy to.
  • -DrepositoryId The repository id you defined in ~/.m2/settings.xml for authentication (more info)
  • -DgroupId The GroupId of the package you want to deploy
  • -DartifactId The ArtifactId of the package you want to deploy
  • -Dversion The version of the package you want to deploy
  • -Dpackaging The packaging type of the library (pom or jar)
  • -DgeneratePom If a pom should be generated
  • -DgeneratePom.description The description contained in the generated pom

Scripting/Aliases

You can also turn this into a shell function. For example if I want to be able to deploy like this:

mdep /home/downloads/mylib.jar com.example mylib 1.0.0 "My library."

I can put the following code into my ~/.zshrc (should also work with bash):

alias mdep="mvn deploy:deploy-file -Durl=https://myrepo.example.com/repository/myrepo-releases/ -DrepositoryId=myrepo-releases -Dfile=$1 -DgroupId=$2 -DartifactId=$3 -Dversion=$4 -Dpackaging=jar -DgeneratePom=true -DgeneratePom.description=$5"

Note that this will deploy to the same repo everytime. If you regularly deploy to different repositories, it would make sense either to add more aliases or add a parameter for the url and repo id.

I hope you found this post helpful. Have a great day.