How to deploy a single JAR file to Maven (oneliner)
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
- -DurlThe full URL to the Maven repository you want to deploy to.
- -DrepositoryIdThe repository id you defined in- ~/.m2/settings.xmlfor authentication (more info)
- -DgroupIdThe- GroupIdof the package you want to deploy
- -DartifactIdThe- ArtifactIdof the package you want to deploy
- -DversionThe version of the package you want to deploy
- -DpackagingThe packaging type of the library (- pomor- jar)
- -DgeneratePomIf a pom should be generated
- -DgeneratePom.descriptionThe 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.
If you have any improvements or comments, please let me know!
