Monday, August 10, 2009

Development by Example: Maven & Archiva

Setup Maven:

  1. Download and Install latest Maven bundle.
  2. If you are accessing Internet via some proxy on your home and/or office networks than make sure you configured these proxy server settings in Maven Settings file.
  3. For example I’ve two proxy servers at my office network and one at home, and part of my maven settings file looks like this:
    proxy
  4. Notice how I enabled my desired proxy server using <active>true</active>. There were cases when I have direct access to internet with out any proxy servers in between and in such cases all I had to do is to make sure “active” property of all of my proxy server settings are set to ‘false’.

Setup Archiva:

  1. Pre-requisite Tomcat 5.5+
  2. Download latest Archiva
  3. Install it as a WAR, the easiest way.
  4. Use embedded derby DB, no fancy stuff.
  5. Goto Archiva web administration area http://<server>:<port>/archiva/ and configure your admin user
  6. By clicking on “Repositories” button from the lhs menu, you can view following preconfigured repositories:
    1. Two Managed Repositories
      1. Archiva Managed Internal Repository
        int-repo1
      2. Archiva Managed Snapshots Repository
        int-repo2
    2. Two Remote Repositories
      1. Central Repository
      2. Java.net Repository for Maven 2
        remote-repos
  7. Although, we can use above preconfigured repositories as it is, but for more clarity, we’ll going to do following changes
    1. Repository name of “Archiva Managed Internal Repository” to “Archiva Managed Internal Release Repository”
    2. Repository directory from “../internal” to “../releases”
    3. Repository URL from
      “http://server:port/archiva/repository/internal/”
      to
      “http://server:port/archiva/repository/releases/”
    4. Repository name of “Archiva Managed Snapshots Repository” to “Archiva Managed Internal Snapshot Repository”
  8. While changes to name and directory can be done by simply editing the repository record, the changes to URL can’t. So we will going to delete “Archiva Managed Internal Repository” and create a new record as follows:
    1. Select “Repositories” from the left hand side menu of Archiva web application. Then from the list of repositories click on “Delete” option of “Archiva Managed Internal Repository”del-internal-repo
      Click on “Delete Configuration and Contents”.
    2. Again, select “Repositories” from the left hand side menu. Then click on “Add” option on the top right hand side of the page. Create new “Archiva Managed Internal Release Repository as follows
      internal-releases-repo
  9. Because we don’t have to change the URL of “Archiva Managed Snapshots Repository” we can simply edit to make needed changes. Goto “Repositories”, click on “Edit” option from the “Archiva Managed Snapshots Repository” and change name from “Archiva Managed Snapshots Repository” to “Archiva Managed Snapshot Repository”:
    edit-snapshot-repo
  10. As shown in diagram below, all artifact requests from your maven clients should be directed to Archiva server which, on the bases of artifact type, release or snapshot, try to search in (local) internal repositories. If a matching artifact found than Archiva will serve the request directly, else, it will forward this request to other Remote repositories available over internet for resolution. Once, an artifact is recieved from remote repository, it will be cached first, in (local) internal repositories and then sent to the client as a response to its initial request.
    maven
  11. Next, we’ll going to configure network proxies. Select “Network Proxies” from left hand side menu of Archiva web application. Then, click on “Add” button on the top right hand side of the page. Fill the form with your network’s proxy information and click “Save Network Proxy”.
    add-network-proxy
  12. Repeat step 11 to create as many proxies as you wish
  13. Now, we’ll going to create proxy connectors where each local or internal repository will be configured as a proxy to external or remote repositories.
  14. Click on “Proxy Connectors” from the left hand side menu, then select “Add” option from the top right hand side of the page and fill the form as follows:
    add-proxy-con1
    Do note “White List” entry
  15. Repeat the step again with following values
    add-proxy-con2 Do note “White List” entries
  16. Repeat the step again with following values
    add-proxy-con3
  17. Repeat the step again with following values
    add-proxy-con4
  18. Now your Proxy Connectors list will look like this
    proxy-con-list1
    proxy-con-list2

Configure local internal repositories in your Maven Settings file:

  1. Create a new profile to configure newly created archiva managed repositories as follows:
    profile1
    profile2
  2. Profiles and their activation is a very handy feature because you can create as many profiles as you like and can activate or deactivate them based on different network environments.
  3. For example, there were cases when our Archiva Server hosting both internal Releases and Snapshots repositories was unavailable for some time. I simply deactivated “archiva” profile from my system’s maven settings file. I was then using remote maven repositories directly via internet. No idle time, no delay, no hurdles.
  4. Do note that we are using same repositories for plugins as well.

Ok, now you are all set to start using local internet repositories. Go ahead create a new project. Once done, point your browser to archiva web application. Click “Browse” from the left hand side menu, you can view all those groups, sub groups and artifacts which have been cached and served to the maven client up till now.

The story doesn’t ends here. We are still short of deploying our custom artifacts to local internal repositories.

Deploying to local internal repositories:

  1. In order to deploy your custom artifacts to local internet repositories, you need network access to machine hosting archiva repositories.
  2. Open your maven’s settings file and add server credentials as follows:
    server
  3. Now, open your project’s POM file add following:
    dm
  4. All set, run maven’s deploy command and then browse your project’s artifact on archiva web application to verify that your it is uploaded successfully.

1 comment:

  1. Nice Post.
    Can you tell us how to do release:prepare, release:perform with Maven/Archiva.
    Thanks,

    ReplyDelete