Using maven in a Java project is great. It manages builds (as customized as you may need), executions, dependencies… In fact, dependencies is, in my opinion, the key feature of maven. Whenever a dependency is added to a project, maven will search for it at repositories, download it and store it, tagging versions.
Some weeks ago I had to get an old project without maven and make it work with it. Everything went right at first: I had only to search for the correct dependency on the repository and config my project to get it. But suddenly I found a jar that wasn’t available on the repository: it was a custom generated jar to include some fonts that were being used by Jasper on a pdf generation.
I had to get it working without adding the jar to the repository. I got some working solutions:
Adding a system dependency
<dependencies> <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/xx.jar</systemPath> </dependency> </dependencies>
I wouldn’t do it. System scope was created to add dependencies that once where external libraries but now are packed into the JDK. Also, the Assembly plugin ignores this kind of dependencies and it doesn’t pack them with the others.
Creating a maven repo inside the project
The idea is to create a maven repo that runs on our own computer instead or a remote server. Every file needed would be on the version control system so every developer can build the project once it is downloaded.
Three steps are needed to accomplish this:
1. Add the local repository to the project pom:
<repositories> <repository> <id>my-local-repo</id> <url>file://${basedir}/my-repo</url> </repository> </repositories>
In this case the repository will be stored on a directory called “my-repo” and it will be located in the project root directory.
2. Install jar in the repository through install-plugin.
On maven 2.2:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \ -Dfile=<path-to-file> -DgroupId=<myGroup> \ -DartifactId=<myArtifactId> -Dversion=<myVersion> \ -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
On maven 2.3 and onwards:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \ -DartifactId=<myArtifactId> -Dversion=<myVersion> \ -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
There are several options to be filled on the command:
path-to-file
: the path to the file of the artifact to install.myGroup, myArtifactId, myVersion
: the group, artifact name and version of the artifact to install.myPackaging
: the packaging of the artifact (ie: jar).path
: the path to the local repo.
3. Add the dependency to the project as usual.
After running the command of the second step, several files will be created on the local repository (like pom and checksum files). This is the reason I don’t like this solution, I find it ugly with those files added to the VCS.
Installing the jar by using install-plugin
The idea is to store the jar into a project directory and install it into the m2repo directory (where all mvn dependencies are downloaded to) on build time. To get this working the dependency should be added to the repository1 as usual, and also add the plugin to the pom configuration:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>install-file</goal> </goals> <configuration> <groupId>myGroupId</groupId> <artifactId>myArtifactId</artifactId> <version>myVersion</version> <packaging>jar</packaging> <file>${basedir}/lib/xxx.jar</file> </configuration> </execution> </executions> </plugin>
This is the solution I decided to use on the project I was working on. It’s clean and it’ll work from the first moment a new developer downloads the project from the VCS.
- I mean the source code repository: SVN, GIT or whatever you use. [↩]
Pingback: Add “mpicbg” to Maven dependency for SIFT | Guangyu Wu
Thanks man! This is REALLY useful!!!
Thanks for the post. It was very useful to me!
There is only one problem: the correct phase for install plugin is ‘install’, not ‘initialize’
Thanks!
Can you please share your whole pom.xml if you were to choose the option “Installing the jar by using install-plugin”, I can’t seem to get it working? What command did you run after changing the pom? Thanks
Thank you for this post. But you dont need to create a maven project. You can insert you maven xml here http://jar-download.com/. After that you will get the jar with all dependencies.
Another solution is to download everything from here http://jar-download.com/ and after adding the custom jar
Thanks. Very concise.
thanks,get it
Thank you.
I’m new to Maven.
Could you explain a bit more what “the dependency should be added to the repository as usual” means?
Thanks in advance
Sorry for the late response. I’m very busy these days, I wasn’t able to take a look to new comments…
It’s been a while since I wrote the post, I think I meant to add the library to your project sources in a folder (in the example, in a folder called “lib” inside the project base dir).
I hope this fast response has been helpful 🙂
Thank you. This was very helpful. Just to clarify:
Do we use the same code and just change the jar file name if we want to include more than one library?
e.g twilio and spring jar files
Great post. Thx
Thanks for this post.. Its really useful for me. Because I’m new with Maven Project..
thanks a lot for this note. I’ve spent quite a lot of time to find this solution. Because initialize doesn’t work.
Really good and useful post!
Thanks!
If you are interested in topic: how to earn online through facebook contempt – you should read
about Bucksflooder first
Thanks man, you saved my day.
For not so interesting reasons I had to use the second way (Creating a maven repo inside the project).
The problem was that maven required the repo to had checksums (I don’t know if it’s because is a more modern version).
[WARNING] Checksum validation failed, no checksums available for file: …
The solution was to add the following to the command that imports the file into the repo:
-DcreateChecksum=true
Maybe you want to update the post, or at least someone finds useful the comment.
Million thanks i was searching for 2 days ultimately i found a good solution
I see you don’t monetize your website, don’t waste your traffic, you
can earn extra bucks every month because you’ve got hi quality content.
If you want to know how to make extra money, search for: Mertiso’s tips best adsense alternative
This is a great post. Was kinda held back due to the external jar not configured properly in the pom.xml.
Thank you.
Interesting thanks for your post.
Hi @Eduardo
With maven install plugin how i can install that jar file in remote repository instead of local repository.
Thank you very much
Intéressant merci pour ton article
Merci pour votre message
I see you don’t monetize your website, don’t waste your traffic,
you can earn extra bucks every month. You can use the best adsense alternative for any type of website (they approve all websites), for
more info simply search in gooogle: boorfe’s tips monetize your website
I have noticed you don’t monetize valdaris.com, don’t waste your traffic, you can earn extra cash every month with new monetization method.
This is the best adsense alternative for any type of website (they approve all sites), for more
details simply search in gooogle: murgrabia’s tools