Creating Maven Repository on Shared Hosting

You can easily create a fully functional, basic Maven repository on your web hosting provider’s account. It will give others the possibility to access your released artifacts using clean URLs (like yoursite.com/maven or maven.yoursite.com) or use it as a repository straight from the Maven.

To achieve it, you’ll need to:

  1. create Maven FTP user,
  2. create a subdomain (optional),
  3. add Maven server configuration,
  4. add distribution management to your pom.xml,
  5. add Maven FTP wagon provider,
  6. use your repository in client projects.

Creating Maven FTP user

We’ll use the FTP as a transport protocol; SSH would also work, but in case of some hosting providers the SSH is not available or you can have only one SSH account for the whole site. Because I like to keep things organized, I’d go with the dedicated Maven FTP account which often can be easily created using your backstage administration panel like cPanel.

So, create a password-protected FTP user – we’ll use maven_user in this example – and assign it to some publicly visible directory (e.g. public_html/maven_user). Set all the other options like quota accordingly to your needs.

Create a subdomain

This step is optional because it just defines another URL for accessing your repository. As you’ve created an FTP account which points to the public_html/maven_user you should be able to access it through www.yourdomain.com/maven_user. However, I prefer the www.maven.yourdomain.com and it’s quite easy to achieve.

Once again go to your web account backend and create a subdomain which will point to the created directory. It should be defined as www.maven.yourdomain.com -> public_html/maven_user

Add Maven server configuration

You need to save FTP user/password information in a location where others will not be able to access it (you definitely don’t wont to publish your login credentials along with your source code!)

It can be configured in $MAVEN_HOME/settings.xml file (often it’s just ~/.m2/settings.xml):

<settings>
    <!-- other entries -->

    <servers>
        <server>
            <id>maven-ftp-repo</id>
            <username>maven_user</username>
            <password>maven_userPassword</password>
        </server>
    </servers>
</settings>

Remember the id you’ve typed above as you’ll use it just in a moment.

Add distribution management to your pom.xml

Next, your project should know where to deploy its artifacts during the deploy phase, so you need to add the <distributionManagement> element to your pom.xml:

<project>
    <!-- other entries -->

    <distributionManagement>
        <repository>
            <id>maven-ftp-repo</id>
            <url>ftp://maven.yourdomain.com</url>
        </repository>
    </distributionManagement>
</project>

As long as the FTP users are created for the whole account and not for the subdomain it doesn’t matter if you type ftp://maven.yourdomain.com or ftp://yourdomain.com.

Note that the <id> element value reflects the value from server settings in settings.xml you defined before.

Add Maven FTP wagon provider

Now you’ve got everything configured but the Maven doesn’t support the FTP transport protocol out-of-the-box. It supports http, https or file but not the ftp. For transport protocols Maven uses wagons which are transport abstraction layer.

You need to add the FTP wagon to your pom.xml. You do that by filling the <extensions> section as shown below:

<project>
    <!-- other entries -->

    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ftp</artifactId>
                <version>2.2</version>
            </extension>
        </extensions>
    </build>
</project>

Now you’re ready to deploy your artifacts whenever you invoke the deploy phase (i.e. mvn deploy.)

Using your repository in client projects

If your clients want to use your artifacts in their projects, they need to add appropriate <repository> entry in their pom.xml:

<project>
    <!-- other entries -->

    <repositories>
        <repository>
            <id>repository_id</id>
            <url>http://maven.yourdomain.com</url>
        </repository>
    </repositories>
</project>

Allowing Apache HTTP server to index your repository

If you’d like to view your repository structure using the browser, just type your URL and see if its indexed. If you get a 403 Forbidden error, you might have files indexing turned off by default (which is a good default setting because of security reasons.) If you want to change this, create the .htaccess file in your FTP user’s account with content like this:

Options +Indexes             # Show content of the directory if there is no index file in it
IndexOptions +FancyIndexing  # Add some additional formatting to the default generated HTML
IndexIgnore .ftpquota        # Hide any files you don't want to show publicly 

Summary

There are some drawbacks of using plain HTTP server instead of full-blown Maven repository like Nexus or Artifactory. You don’t have any search engine, authentication and authorization, multiple repositories are not grouped in once place and the artifacts removal is a PITA.

Nevertheless, in simple cases or when you’re not able to install those powerful repository manager, it should do the work (here see my exemplary repository.) As an alternative, if you don’t have your web hosting account or you don’t want to configure it, you can use the github as your maven repository.

References: