Using an Artefact Repository

Many development teams still use a network drive to store artefacts. This method of transferring artefacts can be unstructured and differ between teams. Thus, it is better to use the advantages of an Artefact Repository.
repository is a collection of Software artefacts that are stored in a defined directory structure. These artefacts can be written to or read from a repository server by using a dependency management framework like MavenGradle, or Ivy. These frameworks provide standards for a project folder structure and a declarative description for build targets. They manage dependent artefacts through textual description and are compatible with many IDEs. Therefore, I made available a Artefact Repository server to developers across my team so they can use technologies like MavenGradle, or Ivy to work more effectively. The intent of this server was to replace the current infrastructure. Further, it is the first step to give developers the possibility to try out new technologies and enable the best usage of them.

Evaluation of an Artefact Repository

At first, I evaluated various repository servers and found Archiva as a potential candidate because it is free and Open Source. I investigated its development and source code on Open HUB – Apache Archiva. As you can see Archiva has a well established code-base and is supported by a large community.

Archiva in a Nutshell

As you can see on the image below, Archiva has a significant amount of commits. It is in development since 2005, and in the year 2009, the book Apache Maven 2 – Effective Implementation was released which covers the topic of Archiva. Additionally, I checked the Apache Archiva Website and analysed its tutorials and guides. The website has two tutorial videos. The Archiva Documentation is straightforward and contains detailed examples. This assures that Archiva is a well-known mature piece of Software with a good documentation. Therefore, I decided to use Apache Archiva as a Artefact Repository server. Other enterprise level repository servers, such as Artifactory or Nexus, are paid-for software.

Commits per Month

Setting up the Environment

The next step was to set up a server for Apache Archiva. There are different ways, like using Docker or Chef. However, I will show here the manual way with Maven. From my experience, Linux servers, such as RedHat, are easier to maintain than Windows servers. On my RedHat instance I installed Archiva as a service and added it to the start-up script. Also I set the port to 80 in the apache-archiva-x.x.x/conf/jetty.xml file in order to make it better accessible.

<Set name="port"><SystemProperty name="jetty.port" default="80"/></Set>

For a more detailed installation description take a look on the webpage Apache Archiva Docs – Installing Standalone.

Configuration for Archiva

To use Archiva as a Artefact Repository you need a Snapshot and a Release repository. A Snapshot is a regular build and is for testing purposes only. A Release is a stable version of source code which is tagged by a VCS (Version Control System). When you defined these settings in your configuration of your dependency management tool you are ready to go.

Using Maven 2 with Archiva

In Maven we need to specify the credentials and repository URIs first.

Configure settings.xml

Go to the folder apache-maven-x.x.x/conf and open the file settings.xml. Search for the xml tag <servers> and add the following lines to it, with your password. This allows you to login to the general Release and Snapshots repository of Archiva as well as the Sandbox repository that can be used for testing purposes. In the settings.xml , you can configure the redirection between the internal, snapshot and official Maven repository.

<server>
  <id>archiva.internal</id>
  <username>username</username>
  <password>*******</password>
</server>
<server>
  <id>archiva.snapshots</id>
  <username>username</username>
  <password>*******</password>
</server>

Configure pom.xml

Additionally, you have to specify the URIs to the Archiva repositories. Go to your pom.xml and add the following lines to it with the repository URIs. This will use the standard Archiva repository of the server.

<distributionManagement>
  <snapshotRepository>
    <id>archiva.snapshots</id>
    <url>http://yourdomain.com/repository/snapshots/</url>
  </snapshotRepository>
  <repository>
    <id>archiva.internal</id>
    <url>http://yourdomain.com/repository/internal/</url>
  </repository>
</distributionManagement>

Deploy with Maven to Archiva

If you want to deploy JARs to Archiva, all you need is the configuration above, and in the root folder of your Maven project perform the command mvn deploy. This will create a Snapshot in your configured repository. If you want to create a Release you have to specify a scm uri for your corresponding VCS.

Maven uses the scm uri to tag the source code in your VCS when you create a Release. This allows you to find the corresponding version of source code to any Release.

Using Gradle with Archiva

In your build.gradle file specify a block repositories and add the following lines to the repositories block. For more information take a look at the Gradle Userguide –  Dependency Management Basics.

repositories {
  …
  maven {
    url "http://yourdomain.com/repository/snapshots/"
    credentials {
      username 'username'
      password '********'
    }
  }
}

Using the Web UI of Archiva

To browse your repositories in a Web UI on Archiva, login with your credentials, or the username and password for Archiva. The web-interface allows you to upload missing artefacts and to browse the repository.

Conclusion

In summary, Archiva enables developers to deliver artefacts in a standardised way, not just files in a folder. In addition, it integrates very well with the Jenkins Maven Plugin, which can execute the Maven Lifecycle. Thus, it is the first step to Continuous Integration.

In my blog post Software Development Environment I used Maven with Archiva in order to provision a standardised tooling environment.