Warning: Parameter 1 to Language::getMagic() expected to be a reference, value given in /home/wikija5/public_html/w/includes/StubObject.php on line 58
Using Maven with any jar dependency - WikiJava
Friday, 31st October 2014
Follow WikiJava on twitter now. @Wikijava

Using Maven with any jar dependency

From WikiJava

Jump to: navigation, search


In this article I'll show you how to deal with the situation when your maven java project requires a library that is not available on the main Maven repository.

If you are new to maven I recommend you to read my previous article Using Maven and Eclipse, the basics, which gives an introduction useful to understand this article.

I've found quite a few of these, and the most common solution used is to manually import the library via the install-file maven command.

Although this works the result is that you will have to manually import all these libraries manually on every computer you want to work on, which includes also having to search for the libraries, making sure you are installing the right thing.

This procedure is a waste of time, and it may also lead to a serie of inconveniences, where different developers may be even coding your project basing on different versions of certain libraries.

Below I will explain how to solve this situation elegantly, using an internal repository.

Contents

How this works

So here's the situation: you are developing your project, that builds nicely, and uses all maven dependencies so that all you store in the versioning system is the clean code. and all your libraries are automatically downloaded by Maven when necessary.

But you need to adopt a new library, maybe the ajt or some kind of library you created and needs to be used in your current project too.

This library is not available in the maven repository, so, unless you come up with something, you won't be able to keep using maven in your project as nicely as before.

most people would just use the install command:

mvn install:install-file -DgroupId=be.abeel \
     -DartifactId=ajt -Dpackaging=jar -Dversion=2.7 -Dfile=ajt-2.7.jar \
     -Dsources=ajt-2.7-src.jar -Djavadoc=ajt-2.7-api.jar

And install the jar manually in the local repository. This offers the inconveniences mentioned above.

My solution consists on installing the Library in a shared repository which is available to everyone and possibly available via web. I describe the new repository in the pom file, so that when one of my developers downloads the source code of the project maven will be able to download all the libraries, without the developer even noticing that he's using not only the main repository.

Keep reading for more details.

creating Maven repository on a web server

This is really simple, in facts if you are using Maven you have everything ready to be just put on a web server to be made available to your project.

Basically, when you run any Maven command, the program downloads the archetypes from the main repository and stores them in the local repository on your computer. This local repository has nothing different from the main one. and in facts can be just copied to your web server, to be made accessible and usable.

To do this I firstly download the library with sources and javadocs.

importing the Library to the local repository

The Ajt specifically comes in a zip package that contains (among the rest):

  • the file ajt-2.7-src.zip with the sources,
  • the file ajt-2.7.jar with the actual library,
  • a doc directory containing the javadocs for the library.

What I do is to rename ajt-2.7-src.zip to ajt-2.7-src.jar (I think this is not necessary, but I like having all jars) and I zip the doc directory (zip it so that the index.html file is in the base directory of the zip, not in a sub directory). rename the zip to ajt-2.7.javadoc.jar.

The library is now ready to be imported to the local repository. I do it with the command mentioned above (this is the only time I'm going to do it, after it will be automatic):

mvn install:install-file -DgroupId=be.abeel \
     -DartifactId=ajt -Dpackaging=jar -Dversion=2.7 -Dfile=ajt-2.7.jar \
     -Dsources=ajt-2.7-src.jar -Djavadoc=ajt-2.7-api.jar -DcreateCheckSum=true

It's the same command, with the exception that I also use the createCheckSum=true parameter, this makes things look a bit better, as when i will be downloading the library with maven it won't be complaining that it can't verify the checksum.

exporting the library to the web server

At this point my local repository contains a copy of the jar files I mentioned in the previous paragraph. I can find these files in :

{Base local Repository dir}/be/abeel/ajt/2.7

Where {Base local Repository dir} can be found as explained in this article Using Maven and Eclipse, the basics.

All I have to do is connecting (for example via FTP) to my web server, create in there a subdirectory you can call it "maven_repository" for example. and copy there all the content of the {Base local Repository dir}/be directory as the "maven_repository" directory would be the new base remote directory.

At this point the new repository is created and it's available (also from the web) at the address: http://www.yourwebserver.org/maven_repository/.

all is left to do is to write in your pom.xml file how to find this new repository and everything will be nice and ready.

Note: I know there's a way to do this command line directly via Maven, but I like to do this way, and it keeps me in contact with what's happening under the hood.

Linking the new repository in the pom.xml

This is also very straight forward. And note that we are going to write this information in the pom file, which is versioned together with the project. This implies that the new repository will stay linked with the project, and we won't really need to do anything additional to the normal procedures to have the files from it.

here's what you need to add to your pom file:

 <repositories>
 	<repository>
 		<id>yourRepo</id>
 		<name>Your repository</name>
 		<url>http://www.yourwebserver.org/maven_repository</url>
 	</repository>
 </repositories>

That's it. put this slice of xml in the pom file at the same level of dependencies, and the game will be done.

Save the pom file and execute mvn test (just a command that triggers the download of the dependencies really).

And see your maven downloading all your dependencies without a problem ..

Note: if your web server requires authentication, you can configure it in your settings.xml file by filling the following data:

 <servers>
   <server>
     <id>yourRepo</id>
     <username>your_login</username>
     <password>your_password</password>
     <privateKey>${user.home}/.ssh/id_dsa</privateKey>
     <passphrase>some_passphrase</passphrase>
     <filePermissions>664</filePermissions>
     <directoryPermissions>775</directoryPermissions>
     <configuration></configuration>
   </server>
 </servers>

as explained here.

conclusion

In this article I showed you how to create a private maven repository on a web server so that you can store in it any library you want. and I showed you how to configure your pom file to seamlessly use this repository to retrieve such libraries.

I hope you enjoyed it, I'm happy to receive your comments on this.

see also

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.