Build your own projects repository using Maven

What is Maven?

Apache Maven is a development tool that simplify building and managing dependencies of projects. It uses a XML file called pom.xml for configuration.

What is a Maven repository?

A repository in Maven is a list of ready to use artifacts. If you add a repository to your pom.xml file, you can use any of its artifacts as a dependency for your project. You can also use projects that are locally stored on your computer without repository. If you don’t need to share libraries between workstations, there is no need to make one.

What I need to make a repository?

First of all you need some space to store your projects. Then you need to transfer your project to this storage. To help you accomplish this, Maven created wagons that handle this task for you. There are available many wagons that allow uploading your projects to a selected destination using different protocols like: SSH, HTTP, FTP or WebDAV.

Wagon-SSH Example

Library example

I will show you a simple Java project configured to deploy on a remote server using SSH protocol. Let’s take a look at pom.xml file of this project.

<project>
    ...
    <properties>
        <server.user>user</server.user>
        <server.host>localhost</server.host>
        <server.path>repo</server.path>
    </properties>
    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.7</version>
            </extension>
        </extensions>
    </build>
    <distributionManagement>
        <repository>
            <id>snapshots</id>
            <url>scp://${server.user}${server.host}/${server.path}</url>
        </repository>
    </distributionManagement>
</project>

If you don’t want to pass your password during deployment, create SSH keys for yourself and copy your public key to the server. You can check it here how to do it. Remember that path in URL must be absolute.

When we have already had our project configured properly, we have to create very useful methods to share.

package com.github.wpanas.maven.library;

public class Hello {
    public void world() {
        System.out.println("Hello world");
    }
}

To deploy a library to the server simple, put this line to a terminal and execute it. If you skip -D arguments, Maven will use default values stored in properties element. You can also ignore properties and type whole URL in pom.xml file.

mvn deploy -Dserver.user={user} -Dserver.host={host} -Dserver.path={path}

Remember to always change project version before deployment.

Usage example

This project will be an example of using deployed library to our remote Maven repository. We have to add the repository to pom.xml file and add library as a dependency.

<project>
    ...
    <properties>
        <server.host>localhost:8082</server.host>
        <server.path>repo</server.path>
    </properties>
    <dependencies>
        ...
        <dependency>
            <groupId>com.github.wpanas.maven</groupId>
            <artifactId>library</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>snapshots</id>
            <name>My Maven Repository</name>
            <url>http://${server.host}/${server.path}</url>
        </repository>
    </repositories>
</project>

Remember that directory used for Maven repository needs to have autoindex enabled.

To use deployed library, import proper package and you are ready to use it.

package com.github.wpanas.maven.usage;

import com.github.wpanas.maven.library.Hello;

public class Usage {
    public static void main(String[] args) {
        Hello hello = new Hello();
        hello.world();
    }
}

Here is the result.

Hello world

Process finished with exit code 0

You can avoid adding repositories element to your projects. It can be set globally in ~/.m2/settings.xml file. You can look it up here.

Summary

Maven allows you to create personal repositories with your projects. That way project can be shared with your colleagues/coworkers. To make it simple, Maven’s developers introduced wagons - small extensions that upload artifacts to a repository.

Projects created for this article are available on GitHub.

comments powered by Disqus