Setting up your own Maven repository

I’ve just setup a local Maven repository so that I can share libraries I develop with other developers on campus. It was really easy, all you need is a filesystem that you can SCP to, you don’t need to install anything on it. Here’s how:

1. In the POM of the project you want to put in a repo:

<distributionManagement>  
  <downloadUrl>https://maven.repo.edu.au/</downloadUrl>  
  <repository>    
    <uniqueVersion>false</uniqueVersion>    
    <id>my-maven-repo</id>   
    <name>Enterprise Systems Maven Repository</name>    
    <url>scp://maven.repo.edu.au/file/system/path/</url>    
    <layout>default</layout>  
  </repository>
</distributionManagement>

You can also separate snapshot releases from point releases in two different repositories, by defining a snapshotRepository element as well as the repository element, same attributes as above.

2. Setup your username and password that will be writing to the repo:

2a. If you have not yet set a master password for Maven, you need to create one. If you have done this already, skip to 2c.

mvn --encrypt-master-password masterpasswordhere

returns:
{4utE9D2jMkQH9K0Lw7YuUnHkfCkZ3TtX55sWq1d2Ink=}

2b. Create a file .m2/settings-security.xml with that password:

<settingsSecurity>    
  <master>{4utE9D2jMkQH9K0Lw7YuUnHkfCkZ3TtX55sWq1d2Ink=}</master>
</settingsSecurity>

2c. Encrypt your password you are going to use for pushing to this repository:

mvn --encrypt-password hello

returns:
{g+cS+/xuAiEH87xfrsRmWIix5+ZtecvNAIzXDyzqD/E=}

2d. In .m2/settings.xml add a server block with that password, and matching id for the repo:

<servers>  ....  
  <server>    
    <id>my-maven-repo</id>
    <username>myusername</username>        
    <password>{g+cS+/xuAiEH87xfrsRmWIix5+ZtecvNAIzXDyzqD/E=}</password>
  </server>  ....
</servers>

3. Build and deploy your project to the repo:

mvn clean install source:jar javadoc:jar deploy

After it builds you should see it uploading the artifacts.

4. Now, in the POM of another project, you can set up your new maven repo as a repository to search and pull artifacts from:

<repositories>
  <repository>    
    <id>my-maven-repo</id>    
    <url>https://maven.repo.edu.au/</url>    
    <snapshots>      
      <enabled>true</enabled> 
    </snapshots>    
    <releases>      
      <enabled>true</enabled>
    </releases>  
  </repository>
</repositories>
Advertisements

One thought on “Setting up your own Maven repository

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s