Setting up your own personal maven repository   Leave a comment

In this post we shall use the source code we created for experimenting with Github and SmartGit and convert it into a maven project and deploy it to a maven repository. We shall setup our own Maven repository on our local box using a third party tool Artifactory from JFrog. Most of you might already have a maven repo and a settings.xml file. So in order to not mess up any existing artifacts and to be able to see the effects of what we are doing easily we can create a new local repo and new settings.xml file.

1. In eclipse, open the GitTest project, right-click on it and click ‘ Configure –> Convert to Maven Project’. You will now see a pom.xml file in the project.

2. Create a directory C:\local.maven.repo which will act as our local maven repo.

3. In order to setup a distribution management system download Artificatory. There is a 1 minute screencast on the page that says how to setup Artifactory. Follow the simple instructions and startup Artifactory.

4. Once Artifactory is started go to the start page http://localhost:8081/artifactory/ . You will see a page like this. Note the page says that Artifactory is serving no artifacts and the ‘Artifacts’ tab is disabled.

Capture

5. Click on ‘Maven Setting’ on the left-hand side menu. Click ‘Generate Settings’ and in the dialog box that pops up click ‘Download Settings’ and save it to a local directory, for eg. C:\Git\personal\settings.xml. This is the settings file that we will use for this project. Edit this file and add this line ‘<localRepository>C:/local.maven.repo</localRepository>’ to point to your local repo created in Step 2.

6. One more thing we need to do is we need to setup our settings.xml and pom.xml to deploy to Artifactory that we just setup. For this click on the ‘Artifacts’ tab and then click on the ‘lib-snapshot-local’ node on the left hand tree. Now copy the whole ‘<distributionManagement>’ xml snippet into your pom.xml file. This setups up your maven project to deploy snapshots to the ‘lib-snapshot-local’ repo setup on the local Artifactory.

Capture

If you want to setup a release repository then you would have to repeat the same step but with the ‘libs-release-local’ node and copy just the ‘<repository>’ XML snippet into the already existing ‘distributionManagement’ element in your POM.

7. You have to enter the username/password for the snapshot repository into your settings.xml file as described in this video. For this enter the XML snippet below into your settings.xml file.

      <servers>
		<server>
			<username>admin</username>
			<password>password</password>
			<id>salilsurendran-snapshots</id>
		</server>
	</servers>

8. Your whole settings.xml file should look like this. Note the repositories now point to Artifactory server at localhost:8081.

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>C:/local.maven.repo</localRepository>  
	<servers>
		<server>
			<username>admin</username>
			<password>password</password>
			<id>salilsurendran-snapshots</id>
		</server>
	</servers>
	<profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>http://localhost:8081/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>http://localhost:8081/artifactory/libs-snapshot</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>plugins-release</name>
          <url>http://localhost:8081/artifactory/plugins-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>plugins-snapshot</name>
          <url>http://localhost:8081/artifactory/plugins-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>  
</settings>

9. Your pom.xml should look something like this:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.salil</groupId>
  <artifactId>gittest</artifactId>
  <version>0.0.1-SNAPSHOT</version>  
  <properties>
    	<internal.repo.path>C:/distribution.maven.repo</internal.repo.path>
  </properties>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>      
    </plugins>     
  </build>
  <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
        </repository>
    </repositories>
 <distributionManagement>
    <snapshotRepository>
        <id>salilsurendran</id>
        <name>salilsurendran-snapshots</name>
        <url>http://localhost:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
    <repository>
        <id>salilsurendran</id>
        <name>salilsurendran-releases</name>
        <url>http://localhost:8081/artifactory/libs-release-local</url>
    </repository>    
</distributionManagement>
</project>

10. Now deploy your project by using ‘mvn –settings C:\Git\personal\settings.xml deploy’ .

11. Once the build is successful go back to your Artifactory home page and click on the Artifacts tab . Now expand the ‘lib-snapshots-local’ node completely. You will see your pom and built jar deployed in this repo.

Capture

12. Now if you go back to the Artifactory home page you will see that it says “Artifactory is happily serving 2 artifacts.”

Advertisements

Posted January 10, 2013 by salilsurendran in Technology

Tagged with , , ,

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

%d bloggers like this: