Maven project which adds another project as a dependency.

Hi Everyone, in this post, I will be discussing about how to add another project as a dependency in maven. Maven is a build automation tool primarily used for java projects. Currently, maven is the most common build tool used in the software market. Please check the wiki page for maven to know more topics.

I will be demonstrating this example by creating 2 projects named 'MavenProject' and 'MavenProject2'. The first project 'MavenProject' is added as a dependency to the second project 'MavenProject2'. To add a project as a dependency in Maven, the project is added as a dependency to pom.xml of the project which refers the latter project. pom.xml is the xml representation of maven project. It stands for project object model. Click here to read more about pom.xml.

The structure of the first project is as follows:
















The class file MavenProject.java is as follows:

package com.techjourney.maven;

public class MavenProject {
    
    public static void main(String[] args) {
    
        printMethod();
    }
    
    public static void printMethod()
    {
        System.out.println(" In MavenProject");
    }
}


The pom.xml file of MavenProject is shown below:

 <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.techjourney.maven</groupId>  
  <artifactId>MavenProject</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <name>MavenProject</name>  
 </project>  

The structure of 2nd project (MavenProject2) is as follows :




















The pom.xml file of MavenProject2 is as shown below:

 <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.techjourney.maven</groupId>  
  <artifactId>MavenProject2</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <name>MavenProject2</name>  
  <dependencies>  
    <dependency>  
      <groupId>com.techjourney.maven</groupId>  
      <artifactId>MavenProject</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <scope>compile</scope>  
    </dependency>  
  </dependencies>  
 </project>  

The class file MavenProject2.java is as follows:

package com.techjourney.maven;

public class MavenProject2 {

    public static void main(String[] args) {
        MavenProject.printMethod();
    }
}


When we run the program, we can see the following output:

 In MavenProject

This example shows how to refer another project in a maven project. Nowadays in industries there will be many projects and each one might depend upon some other projects. So mainly we use this method to add a project as a dependency.

I hope everyone liked this program. Please comment below if you have any doubts or questions. Also please feel free to mail me - sujith17889@gmail.com for doubts and queries.

Thank you for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++