Professional Documents
Culture Documents
com
Setting Up a Maven Repository
1. Introduction
Maven is a popular build tool available for java developers. One of the benefits of maven is that helps to reduce the duplication of dependent software libraries (jars) required to build an application. The maven suggested approach is to store all the software libraries in a remote store called a repository. Maven synchronizes with the public repository at ibiblio.org This repository is slow, unreliable and does not have the latest versions of some libraries(jars) available elsewhere. Libraries private to the organization cannot be uploaded there either. By setting up an internal maven repository, an organization can avail of the benefits of a maven repository and bypass some of the shortcomings of the repository at ibiblio. This article looks at some of the functionality that a maven repository should provide. Criteria for choosing a maven repository are listed. Steps involved in setting up a maven repository using Artifactory are explained. The process of setting up the repository is same for both Linux and Windows and the minor differences are highlighted in the article. Examples of maven pom files which use this repository are shown. The article is illustrated with screenshots to guide the user in setting up a repository. Sample maven and artifactory configuration is shown. 1.1 Background knowledge It is assumed that the reader is familiar with the following concepts and technologies: Maven 2 JEE web servers deployment process (e.g. deploying web applications to Tomcat) XML 1.2 Purpose of maven repository The purpose of maven repository is to serve as an internal private repository of all software libraries used within an organization. Storing maven artefacts(jars and poms) in a dedicated maven repository is preferable to storing them in version control systems(such as CVS or Subversion) for the following reasons: Libraries(jars) are binary files and dont belong in version control systems which are better at handling text files Keeps the version control database small Checkouts, updates and other actions on the version control system will be quicker 1.3 Advantages of having an internal private repository: Reduced likelihood of version conflicts Less manual intervention required for doing a build first time A single central reference repository of all dependent software libraries rather than several independent local libraries It is quicker to do a clean build when using an internal repository as maven artifacts are retrieved from a server on the intranet rather than the ibiblio server on the internet 1.4 Types of maven repository Local repository exists on developers machine and is maintained by the developer. It is in sync with the maven repositories defined in the settings.xml in their ~home/.m2 folder. If no private remote internal repository is setup and listed in the pom.xml file or settings.xml file, then this local repository on developers machine is synchronized with the public maven repository at ibiblio.org Private remote internal repository This the repository which we will setup. We will change the maven pom.xml or settings.xml to use this repository Public remote external repository This is the public external repository at ibiblio.org. By default, maven synchronizes with this repository.
Download code from version control, change settings.xml to point to the internal repository and do a clean build without any manual intervention Install new version of an artefact Bulk import artefacts into the repository Bulk export artefacts from the repository Backup the repository manually and setup task to do this automatically
Following review of all products, Artifactory has all the criteria that we are looking for. Proximity maven repository also seems to have most of the features we are looking for. We will look at implementing the maven repository using Artifactory. Other points about Artifactory are: Meets all our requirements Apache 2.0 license Runs on JDK 1.6 and Tomcat 6. Artifactory is deployed as a war file in Tomcat. Uses Derby DB to store artefacts. Data is stored in a published and well known format All artefacts can be bulk exported to local repository and then imported into some other repository so it is easy to move artefacts from one implementation to another. It also makes upgrades of maven repository very easy Compliant with Java Content Repository standard (JSR-170).
Uses Lucene as search engine. Repository is indexed and can be re-indexed UI done using Ajax Easy to customise the UI Uses Jira as an issue tracker
For Windows, it can be added to Tomcat startup options as shown below: 6.3 Setup the maven repositories A suggested approach is to create 3 repositories(or sub repositories) in our maven repository. They are: Private-internal-repository: This repository contains artifacts which are used only within the organization. These are manually uploaded by the developer. This does not synchronize with remote repository with ibiblio as the artifacts in this repository(or sub-repository) are private to the organization. 3rd -party: This contains artifacts which are publicly available but not in ibiblio repository. Example could be latest versions of libraries which are not yet available on ibiblio or jdbc drivers. This repository is not synchronized with ibiblio as ibiblio does not have these jars Ibiblio-cache: This repository is synchronized with ibiblio repository and is a cache of the artifacts from ibiblio. This is configured in the <ARTIFACTORY_INSTALLATION_FOLDER>/etc/artifactory.config.xml. The configuration to setup these 3 repositories is shown below:
< c o n f i gx m l n s : x s i = " h t t p : / / w w w . w 3 . o r g / 2 0 0 1 / X M L S c h e m a i n s t a n c e " x m l n s = " h t t p : / / a r t i f a c t o r y . j f r o g . o r g / x s d / 1 . 0 . 0 " x s i : s c h e m a L o c a t i o n = " h t t p : / / a r t i f a c t o r y . j f r o g . o r g / x s d / 1 . 0 . 0 h t t p : / / w w w . j f r o g . o r g / x s d / a r t i f a c t o r y v 1 _ 0 _ 0 . x s d " > < ! -B a c k u pe v e r y1 2h o u r s> < ! < b a c k u p C r o n E x p > 00/ 1 2**? < / b a c k u p C r o n E x p > > < l o c a l R e p o s i t o r i e s > < l o c a l R e p o s i t o r y >
< k e y > p r i v a t e i n t e r n a l r e p o s i t o r y < / k e y > < d e s c r i p t i o n > P r i v a t ei n t e r n a lr e p o s i t o r y < / d e s c r i p t i o n > < h a n d l e R e l e a s e s > t r u e < / h a n d l e R e l e a s e s > < h a n d l e S n a p s h o t s > t r u e < / h a n d l e S n a p s h o t s > < / l o c a l R e p o s i t o r y > < l o c a l R e p o s i t o r y > < k e y > 3 r d p a r t y < / k e y > < d e s c r i p t i o n > 3 r dp a r t yj a r sa d d e dm a n u a l l y < / d e s c r i p t i o n > < h a n d l e R e l e a s e s > t r u e < / h a n d l e R e l e a s e s > < h a n d l e S n a p s h o t s > f a l s e < / h a n d l e S n a p s h o t s > < / l o c a l R e p o s i t o r y > < / l o c a l R e p o s i t o r i e s > < r e m o t e R e p o s i t o r i e s > < r e m o t e R e p o s i t o r y > < k e y > i b i b l i o < / k e y > < h a n d l e R e l e a s e s > t r u e < / h a n d l e R e l e a s e s > < h a n d l e S n a p s h o t s > f a l s e < / h a n d l e S n a p s h o t s > < e x c l u d e s P a t t e r n > o r g / a r t i f a c t o r y / * * , o r g / j f r o g / * * < / e x c l u d e s P a t t e r n > < u r l > h t t p : / / r e p o 1 . m a v e n . o r g / m a v e n 2 < / u r l > < / r e m o t e R e p o s i t o r y > < / r e m o t e R e p o s i t o r i e s > < / c o n f i g >
Start Tomcat and navigate to http://localhost:8080/artifactory The artifactory home page is shown below: Sign in using username admin and password password. Click on the Browse repository link and you should be able to view the contents of the repository.
7.2 Configure maven using project pom.xml The repository settings can also be done in the project pom.xml. A simple pom.xml is shown below:
< p r o j e c tx m l n s = " h t t p : / / m a v e n . a p a c h e . o r g / P O M / 4 . 0 . 0 " x m l n s : x s i = " h t t p : / / w w w . w 3 . o r g / 2 0 0 1 / X M L S c h e m a i n s t a n c e " x s i : s c h e m a L o c a t i o n = " h t t p : / / m a v e n . a p a c h e . o r g / P O M / 4 . 0 . 0 h t t p : / / m a v e n . a p a c h e . o r g / m a v e n v 4 _ 0 _ 0 . x s d " > < m o d e l V e r s i o n > 4 . 0 . 0 < / m o d e l V e r s i o n > < g r o u p I d > t e s t < / g r o u p I d > < a r t i f a c t I d > t e s t < / a r t i f a c t I d > < p a c k a g i n g > j a r < / p a c k a g i n g > < v e r s i o n > 1 . 0 S N A P S H O T < / v e r s i o n > < n a m e > t e s t < / n a m e > < u r l > h t t p : / / m a v e n . a p a c h e . o r g < / u r l > < r e p o s i t o r i e s > < r e p o s i t o r y > < i d > c e n t r a l < / i d > < u r l > h t t p : / / l o c a l h o s t : 8 0 8 0 / a r t i f a c t o r y / r e p o < / u r l > < s n a p s h o t s > < e n a b l e d > f a l s e < / e n a b l e d > < / s n a p s h o t s > < / r e p o s i t o r y > < r e p o s i t o r y > < i d > s n a p s h o t s < / i d > < u r l > h t t p : / / l o c a l h o s t : 8 0 8 0 / a r t i f a c t o r y / r e p o < / u r l > < r e l e a s e s > < e n a b l e d > f a l s e < / e n a b l e d > < / r e l e a s e s > < / r e p o s i t o r y > < / r e p o s i t o r i e s > < p l u g i n R e p o s i t o r i e s > < p l u g i n R e p o s i t o r y > < i d > c e n t r a l < / i d > < u r l > h t t p : / / l o c a l h o s t : 8 0 8 0 / a r t i f a c t o r y / r e p o < / u r l > < s n a p s h o t s > < e n a b l e d > f a l s e < / e n a b l e d > < / s n a p s h o t s > < / p l u g i n R e p o s i t o r y > < p l u g i n R e p o s i t o r y > < i d > s n a p s h o t s < / i d > < u r l > h t t p : / / l o c a l h o s t : 8 0 8 0 / a r t i f a c t o r y / r e p o < / u r l > < r e l e a s e s > < e n a b l e d > f a l s e < / e n a b l e d > < / r e l e a s e s > < / p l u g i n R e p o s i t o r y > < / p l u g i n R e p o s i t o r i e s > < d e p e n d e n c i e s > < d e p e n d e n c y > < g r o u p I d > j u n i t < / g r o u p I d > < a r t i f a c t I d > j u n i t < / a r t i f a c t I d > < v e r s i o n > 3 . 8 . 1 < / v e r s i o n > < s c o p e > t e s t < / s c o p e > < / d e p e n d e n c y > < / d e p e n d e n c i e s > < / p r o j e c t >
7.3 Building using the new maven repository When building the maven project, all the repositories should be downloaded using the new repository. The console will show the server maven uses as shown below: Login to the new repository using your web browser and you will see that artifactory has downloaded and cached the artifacts from ibiblio. 7.4 Installing artefacts to the repository Artifacts can be installed using the web UI or using maven command line. Installation using the web UI is simple and faster and does not require any configuration changes. Installation using the command line requires configuration changes in settings.xml and the it can be used in other scripts. 7.4.1 Installing artifacts using the web UI The steps involved are shown below: 1. Upload the artifact to deploy (jar file or pom file) 2. Artifactory will create the pom file if you are uploading a jar file. You can also specify which repository to upload to. 3. Once uploaded, the artifact appears in the repository along with the pom file created by artifactory. 7.4.1 Installing artifacts from maven command line When using mvn clean install command, maven only packages and installs the artifact to the local repository. To install it to the AHP internal repository, we have to add an additional configuration section in the settings.xml. The steps involved are shown below:
< s e t t i n g s > < s e r v e r s >
< s e r v e r > < i d > o r g a n i s a t i o n i n t e r n a l < / i d > < u s e r n a m e > a d m i n < / u s e r n a m e > < p a s s w o r d > p a s s w o r d < / p a s s w o r d > < / s e r v e r > < / s e r v e r s > < / s e t t i n g s >
The repository id should match the server id defined in the settings.xml. The url should include the name of the repository the artefact is to be installed in. The new artifact appears in the repository and artifactory has created the pom file for us automatically.
Backups are stored in <ARTIFACTORY_INSTALLATION_FOLDER>/backups. The backups are in the same format as the local repository on developers machine. This makes it very easy to migrate the repository contents to another implementation of maven repository. 8.2 Other features Delete artifacts using the web UI Search for artifacts using the web UI Bulk import/export all artifacts in repository The included jetty web server can be used if tomcat is not required
9. Conclusion
An internal private maven repository speeds up the build process and makes it easier to do clean builds. It also helps to avoid conflicts due to different versions of libraries. Amongst the 4 common maven repositories available, Artifactory seems to be the better product. Artifactory makes it easy to setup a maven repository. It provides all the features which a good maven repository should implement. The organization will not be locked into this tool as it is easy to migrate the repository contents to another implementation. A web UI makes the repository easy to use even for people who dont know how the repository works.
Biography
Avneet Mangat has 5 years experience in Java/J2EE, and is currently working as Lead developer at Active Health Partners (www.ahp.co.uk). He has a Bachelors degree in Software Engineering, and is a Sun Certified Java programmer, Adobe certified Flash Designer and Prince2 certified (foundation). He is the lead developer of open source tool DBBrowser, please see http://databasebrowser.sourceforge.net/ . His outside interests include photography and traveling, and he can be contacted at avneet.mangat@ahp.co.uk or avneet.mangat@gmail.com. All Rights Reserved, Copyright 2000 - 2013 , TechTarget | Read our Privacy Statement