Archive for January 2013

Creating dependencies between different maven projects   Leave a comment

In this post we shall see how we can create dependencies between different Maven projects using pom.xml. We shall use the project ‘GitTest’ we created and uploaded to our local maven repository in the last post. In this post, we shall create another Maven project called ‘jarproject’ and then use it to our project. We shall also define a parent pom where we shall lay out modules so that our ‘GitTest’ project automatically builds the corresponding library project.

1. Using Eclipse create another Maven project and name it ‘jarproject’. Give it a group id, artifactId etc. In this project introduce a new class com.jarproject.LibraryClass and put a method printLibraryLine() inside that class. We shall call the printLibraryLine() method from the ‘GitTestMain.java’ present in the ‘GitTest’ project. This is how your project will look in Eclipse.

Capture

2. Now in order to build ‘jarproject’ along with ‘GitTest’ we need a parent pom, one that will link both of these projects together. So go ahead and create another Maven project named ‘parent’ in Eclipse and in that pom enter both ‘GitTest’ and ‘jarproject’ as modules. The parent pom will now 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>parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>parent</name>
  <modules>
  	<module>../GitTest</module>
  	<module>../jarproject</module>
  </modules>
  <distributionManagement>
    <snapshotRepository>
        <id>salilsurendran-snapshots</id>
        <name>salilsurendran-snapshots</name>
        <url>http://localhost:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
    <repository>
        <id>salilsurendran-releases</id>
        <name>salilsurendran-releases</name>
        <url>http://localhost:8081/artifactory/libs-release-local</url>
    </repository>    
  </distributionManagement>
</project>

3. Add a dependency on the ‘jarproject’ module in ‘GitTest’ pom.

 <dependencies>
 	<dependency>
 		<groupId>com.salil</groupId>
 		<artifactId>jarproject</artifactId>
 		<version>0.0.1-SNAPSHOT</version>
 	</dependency>
 </dependencies>

4. Now add a line of code in the GitTestMain.java file calling the LibraryClass.printLibraryLine() method so that ‘GitTest’ project now depends on ‘jarproject’. Note: In eclipse you will have to do a ‘Update Maven Dependencies’ in order to be able to get a reference to LibraryClass.

5. This is how your project structure will now look like. Execute maven deploy on the parent pom.xml either in eclipse or command line.

Capture

 

6. Next now go to your local maven repository hosted by Artifactory which we setup in the previous post by entering the URL ‘http://localhost:8081/artifactory/webapp/home.html‘ in the browser. Go to the ‘Artifacts’ tab and browse to the ‘libs-snapshot-local’ repo and expand it. You will see 3 projects ‘GitTest’, ‘jarproject’ and ‘parent’ in the repo.

Capture1

Advertisements

Posted January 18, 2013 by salilsurendran in Technology

Tagged with