Using Maven with any jar dependencyFrom WikiJava
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.
How this worksSo 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.jarAnd 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 serverThis 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 repositoryThe Ajt specifically comes in a zip package that contains (among the rest):
What I do is to rename 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=trueIt's the same command, with the exception that I also use the exporting the library to the web serverAt this point my local repository contains a copy of the jar files I mentioned in the previous paragraph. I can find these files in :
Where 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 At this point the new repository is created and it's available (also from the web) at the address:
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.xmlThis 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 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. conclusionIn 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.
|
