January 11, 2013

How to Setup and Use Artifactory as Proxy/Mirror for Remote Repositories

In this blog I will discuss and show you how to setup and use Artifactory as proxy (mirror) for remote repositories, such as Central Maven 2 repository or other remote repository, but also using for internal distribution for corporate releases.

First if you have not installed a Artifactory server, start with that. You can read about the installation steps in my previous blog - http://magnus-k-karlsson.blogspot.se/2013/01/how-to-install-artifactory-on-rhel-63.html.

The next step is to create a settings.xml for you company, that uses this newly installed Artifactory server as proxy/mirror. Creating you own settings.xml can be daunting task, but fear not Artifactory can create one for you. Simply open you web browser and open http://192.168.0.3:8081/artifactory/webapp/mavensettings.html. Here we will also select which internal repository we will use for internal distribution. You can create a new, but here we will use the already created repository 'libs-release'.

Then click Generate Settings and save it to ~/.m2/settings.xml. That was not so hard, lets try it out. Before you continue, make sure that you have installed Maven 3. If you are on a RHEL platform, please read my previous blog http://magnus-k-karlsson.blogspot.se/2013/01/how-to-install-maven-3-on-rhel-63.html.

Now lets create a simple project. I will use Apache Wicket Quickstart here, but you can use any maven project. Open http://wicket.apache.org/start/quickstart.html and copy past the maven command to your terminal.

$ mvn archetype:generate -DarchetypeGroupId=org.apache.wicket -DarchetypeArtifactId=wicket-archetype-quickstart -DarchetypeVersion=6.4.0 -DgroupId=com.mycompany -DartifactId=myproject -DarchetypeRepository=https://repository.apache.org/ -DinteractiveMode=false

...
Downloading: http://192.168.0.3:8081/artifactory/remote-repos/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar
Downloaded: http://192.168.0.3:8081/artifactory/remote-repos/commons-cli/commons-cli/1.0/commons-cli-1.0.jar (30 KB at 60.3 KB/sec
...

As you can see that maven now request all dependency from you local artifactory server instead from a remote repository. And if you open the artifactory server log you will see that the mirrored remote repository is populated.

$ less /var/log/artifactory/artifactory.log

...
2013-01-11 12:52:44,496 [http-bio-8081-exec-1] [INFO ] (o.a.r.HttpRepo      :252) - repo1: Downloading content from 'http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar'...
2013-01-11 12:52:44,502 [http-bio-8081-exec-1] [INFO ] (o.a.r.HttpRepo      :252) - repo1: Downloading content from 'http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar.sha1'...

...

Now if everything is working, lets continue by adding a new remote repository. Lets log into artifactory and select the Admin tab and then Repositories from the left menu and add a new remote repository. Here I will use the new JBoss EAP 6 repository, but you can choose any valid remote repository.

Repository Key: jboss-eap-repository
URL: http://repo1.maven.org/maven2
Public Description: JBoss EAP Maven Repository
Includes Pattern: **/*
Checksum Policy: Generate if absent
Max Unique Snapshots: 0
Handle Releases: true
Handle Snapshots: true
Use default for the rest of the settings.

After added, we will add this remote repository to virtual repository remote-repos. The advantage of using virtual repositories, is that we do not need to updated our settings.xml. The remote-repos is already there by default.

Al right. Now lets test it. Lets open up our wicket pom.xml and add the JBoss EAP Java EE Spec dependency.

<repositories>
    <repository>
        <id>jboss-eap-repository</id>
        <name>JBoss EAP Maven Repository</name>
        <url>http://maven.repository.redhat.com/techpreview/all/</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>jboss-eap-repository</id>
        <name>JBoss EAP Maven Repository</name>
        <url>http://maven.repository.redhat.com/techpreview/all/</url>
    </pluginRepository>
</pluginRepositories>

<properties>
    <wicket.version>6.4.0</wicket.version>
    <jetty.version>7.6.3.v20120416</jetty.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jboss.eap.spec.version>3.0.1.Final-redhat-1</jboss.eap.spec.version>
    <jboss.eap.impl.version>6.0.1-redhat-1</jboss.eap.impl.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>${jboss.eap.spec.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.component.management</groupId>
            <artifactId>jboss-dependency-management-eap</artifactId>
            <version>${jboss.eap.impl.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

After editing you pom.xml, lets test it.

$ mvn clean install

...
Downloading: http://192.168.0.3:8081/artifactory/remote-repos/org/jboss/component/management/jboss-dependency-management-all/6.0.1-redhat-1/jboss-dependency-management-all-6.0.1-redhat-1.pom
Downloaded: http://192.168.0.3:8081/artifactory/remote-repos/org/jboss/component/management/jboss-dependency-management-all/6.0.1-redhat-1/jboss-dependency-management-all-6.0.1-redhat-1.pom (2 KB at 13.2 KB/sec
...

And in your artifactory server log.

...
2013-01-11 12:49:51,074 [http-bio-8081-exec-18] [INFO ] (o.a.r.HttpRepo      :252) - jboss-eap-repository: Downloading content from 'http://maven.repository.redhat.com/techpreview/all/org/jboss/spec/jboss-javaee-6.0/3.0.1.Final-redhat-1/jboss-javaee-6.0-3.0.1.Final-redhat-1.pom'...
2013-01-11 12:49:51,518 [http-bio-8081-exec-18] [INFO ] (o.a.r.HttpRepo      :252) - jboss-eap-repository: Downloading content from 'http://maven.repository.redhat.com/techpreview/all/org/jboss/spec/jboss-javaee-6.0/3.0.1.Final-redhat-1/jboss-javaee-6.0-3.0.1.Final-redhat-1.pom.sha1'...
...

No comments: