Friday, June 1, 2012

How to install a jar into local Maven repository

Hello.
I often  face a problem when I need to use some Java library in my Maven based project, but this library is not available in any Maven repository and is distributed as a .jar file. In this post I'll describe two ways how to fix this problem.


The simple way
The simple way is to use the following command:
 
For example if I want to install a jar file that is located in a file javacv-linux.jar  I can use the following command:

After executing this command Maven will install a jar file into a local repository. Now this file can be specified as a dependency in a pom.xml file. To do it we need just to add the following lines:


The parameters of an installed file should be the same as the parameters that we specified in the dependencies section of the pom.xml.

The correct way
The simple way will work correctly, but it have several problems. First you need to add your dependencies manually if you have clean local repository (it can happen if you reinstall your OS or use other development environment). Second, every new developer of your project should be informed about this dependency and should perform the same operation on his/her own computer.

This is tedious and boring. And how people do the boring things? They use a build tool :)
You can perform the same operation in pom.xml file in the following way:

To do this the jar file should be located in the folder 'lib' at the root folder of your project. Make sure that 'phase' parameter is set to validate. This is used to execute this plugin before the compilation phase.

Now you need to execute the following command in the directory of your project:

Dependency should also be added to pom.xml as in a previous example.

That's all.
Good luck.

No comments:

Post a Comment