Apr 24 2006
Setting up a Maven2 Internal Repository.
This is going to be a very very dull post unless you are trying to figure out how to set up an internal company repository for maven2. If you are, and you are desperately looking for information on how you do this, then welcome.
I have just spent the morning trying to get a communal internal repository set up for our development environment, and it turns out that there is next to no information available on how to do it. This is mainly because it’s very easy, but still, it would have been a more productive morning if there was a web page somewhere explaining it.
Maven manages your dependencies, and can automatically download various jar files and so on for you. Telling it “I need to use Junit†for example will cause it to download Junit, and put it on your classpath. It gets files it downloads from a repository.
By default Maven will talk to two repositories. One is the central repository maintained at the Maven site, and the other is a local copy, which will be in the .m2 directory of your home directory.
We wanted to have a something that falls between these two settings; something that all our developers can download from, but isn’t publicly accessible. We use various commercial third party libraries and making them available to the entire world is clearly bad.
It turns out that a maven repository is just a directory somewhere, and that’s about it. In order to use it there are very few steps you need to do.
- Choose a directory
- Make sure its accessible via some network protocol to everyone who needs it (i.e. serve it via http or ftp or something
- Put the libraries you need in the directory, in a specific format.
- Set up your maven builds to read from the repository
- (optional) set up your maven builds to deploy your repository
I will assume you know how to do the first two steps. A point of interest about the second step is that to deploy anything to the repository you must use a protocol that supports writing. I suggest scp. In fact I have made the repository available via scp for deployment, and http(s) for reading (makes authentication easier). Our server is going to change only to https soon, and I need to figure out how to get maven to support self signed certificates. At the moment it just dies.
To put existing libraries into the repository, use the maven deploy command.
To set up your build to read from the repository add the following xml to your pom.xml file:
The URL must be accessible for read permission, but write is not required.
Finally, to get your maven build to deploy to the repository, add in:
Obviously there are lots of things you can tweak with the xml elements etc. But hopefully this will get you far enough along for the maven2 website’s documentation on xml elements to be of use.






hi laurie! we are your biggest fans! keep your fantastic blog going!!!!
lots of love and hope to see you soon,
love fleur and bob and gordon
xxxxx
Laurie,
I’m really new to Maven and your posted helped me get my jars deployed to an internal repository. Thanks. To help others here is the command I used:
mvn deploy:deploy-file -DgroupId=apache -DartifactId=xercesImpl -Dversion=2.5.0 -Dpackaging=jar -Dfile=./xercesImpl.jar -DrepositoryId=repository -Durl=file:///home/mike/public_html/repository
And this linked helped a lot: http://maven.apache.org/guides/mini/guide-deploying-3rd-party-jars.html
I hope my post helps others who are just getting started.
Hi Laurie,
indeed I am a bit desperate searching for info on setting up internal maven respositories.
Unfortunately the XML in your post is still mangled and View Source does not show any XML tags.
Any chance you could add the XML, be it only as e.g. [tag]blah[/tag]?
Cheers, Maarten
Well I have tried, and tried, and tried and tried to get Wordpress to LEAVE MY XML ALONE so that it gets displayed on the page in a format that can be read. But no. If I put XML inside a PRE or a CODE tag, the html editor thinks i made a mistake and just deletes it. Same if I try and encode the tags with <: etc. If you really need the xml and can’t figure it out what what Wordpress has decided to display you will need to email me and I will pass it back in a sensible format, because this software, it’s broke!
Right, I had another go, and got it fixed. The trick it seems is to accept that the wordpress pretty editor is CRAP for anything other than pretty text, and turn it off. Then things start to work in almost (but not quite) the way that would just make sense!
You have provided a way for Setting up a Maven2 Internal Repository. i wish to do the same thing using maven1.0. please help.
Sorry Sam, I never used maven1, though I suspect the tricky part, of realising its just a direcotry somewhere is the same… maybe some of the links onYin Weijun’s page will lead to something of help
Setting up a local maven repository is pretty straight-forward. Maven lacks a decent getting-started guide for the mentally hadicapped (like myself) who lack the ability to learn everything there is to know about something before they can figure out how to actually use it.
Here’s what I did (unix users adjust as necessary, or just do all of this from emacs like you were going to anyway :-):
Fist I found the directory where all the code was I wanted to restructure from a command prompt — it was a mix of webapps and java code projects as well as other junk.
Used this command to capture all jar files: “dir /s /b > jars.txt”
I opened the jars.txt file in a text editor which supports regex find/replace (I use JGSoft EditPadPro) and removed unnecessary duplicate jars.
Now I wanted to get a list of jar paths and jar file names, for which I did a regex replace of “(.*)\.jar” with “\t\1\t\t” (in my editor I had to actually use tab characters by typing shift-tab in the replace window… you mileage may vary
Copy all the new text and pasted it into OpenCalc (you can use Excel if you must…)
Now you should hopefully have four columns, with two columns (1 and 3) empty. The order of the columns is: groupId (empty), artifactId (filled in from jar name), version (empty), and file path (filled in from directory listing)
Fill in the artifact id’s based on where the jars supposedly came from (e.g. com.sun, javax, org.apache.commons… etc)
If the version is indicated in the jar file name, copy it over to the version column. Otherwise you might want to open up the jar and look in the manifest to see if it might be in there.
Change the artifact id column so that it is only the name of the jar sans groupId and version info.
Copy all the data and paste back to your trusty text editor
Replace “(.*?)\t(.*?)\t(.*?)\t(.*)” with “mvn deploy:deploy-file -DgroupId=\1 -DartifactId=\2 -Dversion=\3 -Dpackaging=jar -Dfile=”\4″ -DrepositoryId=local -Durl=file://REPO_DIRECTORY” where REPO_DIRECTORY is where you want your local repository to exist. You might have to create the directory in advance — I’m not really sure.
Now save this file as createRepo.bat and execute it. Watch it churn. You’re done!
Oops… saw that “\t\1\t\t†was incorrect above and should be “\t\1\t\t†with a backslash and a zero on the end. Sorry!
Thanks for this - it seems like a no brainer, but I didn’t know where to start.