Tuesday, 9 July 2019

Maven: Grouping dependencies


We can consolidate all the dependencies in a single pom, and use it in other projects.

For example, a typical web application requires below things.
         a. ORM library like hibernate
         b. rest library like jersey
         c. unit testing library like junit
        
Instead of adding these dependencies in all the projects separately, you can create a pom file and specify the packaging artifact as "pom", and add this pom as dependency in all other projects.

pom.xml
<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.selflearningjava</groupId>
   <artifactId>rootWebPom</artifactId>
   <packaging>pom</packaging>
   <version>1</version>
   
   <name>Root pom for all the web application</name>

 <dependencies> 
 
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
   <scope>test</scope>
  </dependency>
  
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>${hibernate.version}</version>
  </dependency>

  <dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-server</artifactId>
   <version>${jersey.version}</version>
  </dependency>

 </dependencies>
   
   <properties>
  <junit.version>4.12</junit.version>
  <hibernate.version>5.2.16.Final</hibernate.version>
  <jersey.version>2.27</jersey.version>
   </properties>
   
</project>

As you observe above pom.xml file, I specified the packaging type as ‘pom’.


Go to the directory, where the above pom.xml file is located and run ‘mvn install’ command. This command installs the pom in your local repository.

C:\Users\krishna\rootWeb>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.selflearningjava:rootWebPom >-------------------
[INFO] Building Root pom for all the web application 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ rootWebPom ---
[INFO] Installing C:\Users\krishna\rootWeb\pom.xml to C:\Users\krishna\.m2\repository\org\selflearningjava\rootWebPom\1\rootWebPom-1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.600 s
[INFO] Finished at: 2018-04-20T09:05:43+05:30
[INFO] ------------------------------------------------------------------------

As you see the output, the pom file is installed in the local repository.


Now, you can use the above pom file as a dependency in other projects by specifying the dependency type as ‘pom’.

        <dependency>
   <groupId>org.selflearningjava</groupId>
   <artifactId>rootWebPom</artifactId>
   <version>1</version>
   <type>pom</type>
  </dependency>

Find the below pom file that uses other pom as dependency.


pom.xml

<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.selflearningjava</groupId>
   <artifactId>customerEngagement</artifactId>
   <packaging>jar</packaging>
   <version>1</version>
   
   <name>Root pom for all the web application</name>

 <dependencies>
 
  <dependency>
   <groupId>org.selflearningjava</groupId>
   <artifactId>rootWebPom</artifactId>
   <version>1</version>
   <type>pom</type>
  </dependency>

 </dependencies>
   
   <properties>
  <junit.version>4.12</junit.version>
  <hibernate.version>5.2.16.Final</hibernate.version>
  <jersey.version>2.27</jersey.version>
   </properties>
   
</project>



Previous                                                    Next                                                    Home

No comments:

Post a Comment