CopyDisable

Friday 2 November 2012

Configuring Maven to use Artifactory

In my previous post Installing Artifactory 2.3.3 with Tomcat and MySQL I wrote about installation of Artifactory. Now my Artifactory server is ready and I will use it with Maven. When I build some project with Maven I will use this Artifactory instance and my project dependencies will come from Artifactory. Initially Artifactory repository will not have any artifacts. Whenever I build a project, the request for dependencies will go to Artifactory and if the requested artifact is not present in the repository, Artifactory will download the artifact from the configured live repositories and will store it in its own repository. In turn it will work as a proxy. There are couple of ways to make maven to use this in-house repository. I will write about few of them.
Mirroring all repository requests:
We can force Maven to use Artifactory by having it mirror all repository requests. Insert the following into your settings.xml (the settings.xml file locates in USER_HOME/.m2 folder)
  <mirrors>
    <mirror>
      <id>internal-repository</id>
      <name>My Artifactory</name>
      <url>http://localhost:8081/artifactory/repo</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  

The URL http://<host>:<port>/artifactory/repo is Artifactory's built-in global virtual repository.It will make Artifactory look through all repositories (local and remote) that are configured in it, for any artifact maven requests.
In this example I will use Apache common lang project (http://commons.apache.org/lang/) for building.
Right now my Artifactory is not storing any artifact, so we can see the message Artifactory is happily serving 0 artifacts.
image
 
Now I will build the Apache common lang project. We can see that download is happening from our Artifactory server.
image



Our build process completed successfully.

image



Before running this build process our Artifactory had 0 artifacts, now after build process it contains 513 artifacts. So next request for any of these artifacts will directly be served from Artifactory repository.

image





Using Artifactory’s Auto-generated settings

The simplest way to configure Maven to use Artifactory is to use the automatic settings generated by the Artifactory GUI.

Go to Home –> Client Settings –> Maven Settings

image



Select the repositories for respective types and click on Generate Settings

image



We can see the settings generated by the auto generator.

image



Click on Download Settings to download the settings.xml file generated by Artifactory auto generator.

image



Replace your settings.xml file (USER_HOME/.m2/settings.xml) with this settings.xml file and maven is ready to use this repository.



A general way:

Our Artifactory setup is working perfectly, so we can override the built-in central and snapshots repositories of Maven to use this repository. I can insert the the repositories settings in my parent POM or in my settings.xml file under an active profile.

My settings.xml file displayed below, here I have inserted my Artifactory repository settings (it is in red color) inside the active dev profile.



<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<profiles>
      <profile>
            <id>dev</id>
           
            <repositories>
                  <repository>
                        <id>central</id>
                        <url>
http://localhost:8081/artifactory/libs-release</url>
                        <snapshots>
                              <enabled>false</enabled>
                        </snapshots>
                  </repository>
                  <repository>
                        <id>snapshots</id>
                        <url>
http://localhost:8081/artifactory/libs-snapshot</url>
                        <releases>
                              <enabled>false</enabled>
                        </releases>
                  </repository>
            </repositories>
            <pluginRepositories>
                  <pluginRepository>
                        <id>central</id>
                        <url>
http://localhost:8081/artifactory/plugins-release</url>
                        <snapshots>
                              <enabled>false</enabled>
                        </snapshots>
                  </pluginRepository>
                  <pluginRepository>
                        <id>snapshots</id>
                        <url>
http://localhost:8081/artifactory/plugins-snapshot</url>
                        <releases>
                              <enabled>false</enabled>
                        </releases>
                  </pluginRepository>
            </pluginRepositories>


      </profile>
</profiles>


<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>
</settings>




Now if we placed the repository settings under some non active profile, then we have to specify the profile at the time of building a project. Then maven will use the profile at the time of building and it will use our Artifactory repository.

For example, if dev is not the active profile, then at the time of building we have to add the –P <profile> option of  Maven.

Example:

root@redmine:~/commons-lang3-3.1-src# mvn compile -P dev



In the above settings.xml file for each type of repository URL I have specified the virtual repository configured for that specific purpose (e.g. for plugin repository <pluginRepository> I used the URL http://localhost:8081/artifactory/plugins-release , where plugings-release is the virtual repository which is specifically configured for this purpose) . Instead of specifying four different URLs as shown above, I could use only the single http://localhost:8081/artifactory/repo (Artifactory’s built-in global virtual repository) URL for all the four types, but this would make Artifactory to do a little more searching.



That’s it for today….So enjoy happy building with Maven-Artifactory setup Nerd smile.





1 comment:

Anonymous said...

Following your example, we get several Authorization failed: access denied to: and then it points to our repository. The user defined in the server section of settings.xml has admin privileges. Have you encountered this before?

Post a Comment