Recipe for Tiny Maven Repository

First, I must admit I’m an old fashioned guy that still uses Apache Ant. My reasons may not be easily justifiable and maybe I’m just too lazy to switch but that’s another topic. What I needed to do is to make a Maven repository (for the general public) on a shared hosting account containing jar files that my team creates using Ant.

There are several several applications out there that make maintenance of Maven repository easier. I didn’t try any of those as they looked too heavyweight for my purpose. All I needed was a simple directory structure that Maven repository is. I could not find an appropriate how-to instructions so here it is how I did it…

1. Prepare host for Maven repository

For Maven repository I got a virtual host (I’ll name it here maven.mycompany.com) with ftp access.

1.a) Ftp login to your maven host and make directory “/repos”. This will be repository root directory.

1.b) In the root directory of the sate create index.html with some basic information about your repository, eventually linking to the /repos sub-directory.

1.c) I thought it would be useful to instruct search engines not to index repository so I also created robots.txt in the root directory with the following content:

User-agent: *
Disallow: /

1.d) In order to make content of Maven repository browsable, make .htaccess file in the /repos with content:

Options +Indexes

2. Put host ftp credentials to Maven’s settings.xml

Deployment commands reference repository by identifier that has to be defined in Maven’s settings.xml file. Create (or edit) your settings.xml file and specify login credentials for the server. It could look like this:

<settings>
  <servers>
    <server>
      <id>mymavenrepos</id>
      <username>my_ftp_username</username>
      <password>my_ftp_password</password>
    </server>
  </servers>
</settings>

3. Make pom.xml for your library

Create pom.xml for your library:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>sample-library</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>Library name</name>
  <dependencies>
    <!-- library dependencies go here -->
  </dependencies>
  <build>
    <finalName>${project.groupId}-${project.artifactId}</finalName>
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
         <artifactId>wagon-ftp</artifactId>
         <version>1.0-beta-2</version>
      </extension>
    </extensions>
  </build>
</project>

You need to provide dependencies section if your library depends on other libraries. If some of third party libraries that you used are not available in any public Maven repository, you may want to deploy them to your repository. Notice also extension element with ftp wagon reference. It’s needed for library deployment using ftp.

Few more comments:

  • There’s a newer beta version of the ftp wagon available but it didn’t work for me in combination with Maven 2.0.9.

  • I tried to use library specific file name (e.g. mylibrary.xml) instead of pom.xml in order to separate my library from POM files related to dependencies. This however failed with some strange error (don’t recall any more) so I ended up creating separate directories and using pom.xml name for all deployed libraries.

  • Deploy library to your Maven host

    mvn deploy:deploy-file -Dfile=sample-library-1.0.jar -DpomFile=pom.xml -Durl=ftp://maven.mycompany.com/repos -DrepositoryId=mymavenrepos

That’s it.

Now when I understand how these things work, I could make my life easier automating the process with Ant tasks for Maven. Hopefully soon…

Comments