Skip to content

Instantly share code, notes, and snippets.

@zzhik
Forked from EronWright/README.md
Created September 15, 2021 19:03
Show Gist options
  • Select an option

  • Save zzhik/19061e7693584b793b5533b7d411cf37 to your computer and use it in GitHub Desktop.

Select an option

Save zzhik/19061e7693584b793b5533b7d411cf37 to your computer and use it in GitHub Desktop.
Simple Maven repository hosted on Apache httpd web server

Simple Maven Repository

Hosting a shared Maven repository on a remote server may be accomplished with a simple web server such as Apache HTTP server. This works because most of the resolution and publishing logic of Maven is implemented in the client. That said, it works only if you know the groupId and artifactId to be published, since appropriate directories must be created by hand.

Do consider using more sophisticated server software such as Artifactory or Sonatype Nexus. Below is the 'bare minimum' alternative.

Caution: this example exposes a writable directory without any authentication, and thus assumes a trusted network.

Configure Apache HTTP

You'll need to load the following modules:

  • WebDAV (mod_dav, mod_dav_fs, mod_dav_lock)

Define a virtual host for your repository. Create a configuration file named /etc/apache2/extra/httpd-maven2.conf:

<VirtualHost *:8081>
    DocumentRoot "/var/repository/root"
    ErrorLog "/var/repository/log/error_log"
    CustomLog "/var/repository/log/access_log" common

    DavLockDB "/var/repository/lock/DavLock"

    <Directory /var/repository/root>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

        Dav On

        <RequireAny>
            Require method GET PUT DELETE POST OPTIONS
        </RequireAny>
    </Directory>
</VirtualHost>

Adjust the port number 8081 as appropriate.

Include the configuration file in /etc/apache2/httpd.conf:

Include extra/httpd-maven2.conf

Create the repository directory with correct permissions. On MacOS the httpd user is _www.

$ mkdir /var/repository
$ chown _www:_www /var/repository
$ mkdir -p /var/repository/root/maven2

Finally restart the server.

$ sudo apachectl restart

Configure your Maven project

Configure your project to upload to your server. In pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  
	<groupId>org.example</groupId>
	<artifactId>myjar</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>
  
	<distributionManagement>
		<repository>
			<id>myrepo</id>
			<name>My Repository</name>
			<url>http://127.0.0.1:8081/maven2</url>
		</repository>
	</distributionManagement>
  
  ...

Create Folder Structure

You need to create the folder structure to support a given groupId and artifactId.

$ mkdir -p /var/repository/root/maven2/org/example/myjar/1.0

Publish

At last you should be able to publish to the server.

$ mvn deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment