Adding dependencies for Spring and Hibernate

In this post we will see how to use maven for building dependencies for Spring, Hibernate etc. To start with, click on File -> New -> Other as shown below.
In the New window, type maven and select "Maven Project" and click Next.
Do not select anything here. Leave the default check box as it is and click Next.
In this window, the default ArtifactId select would be "maven-archetype-quickstart". Click Next.
In this window, give the following details and click Finish. ArtifactId is the name of the project.
This creates folders and a pom.xml file. This "pom.xml" file is the back bone for maven. We can add jar files to our project by adding dependencies to the pom.xml file. Open pom.xml file. Suppose we want to add spring-orm jar file. To do that, open google.com and search for "hibernate-core maven". Click on the first link (http://mvnrepository.com/artifact/org.hibernate/hibernate-core/3.6.10.Final). This link will display the list of jar file versions and its related binary file to be down loaded. Let us suppose that we have chosen the version "3.6.10.FINAL" for our project. So click on the version and it will give the piece of code to be added for the pom.xml file. Copy this code and paste it in the pom.xml file under the dependencies tag. Once the piece of code has been added and saved, you can observe that maven downloads the jar that has been added for dependency. The following screens will make it more clear for understanding.
<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>Spring_Hibernate_Maven</groupId>
 <artifactId>SpringHibernateMavenIntegration</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>SpringHibernateMavenIntegration</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <junit.version>3.8.1</junit.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>3.6.10.Final</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>3.2.6.RELEASE</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.2.6.RELEASE</version>
  </dependency>

 </dependencies>
</project>

We can also add the dependencies by clicking on the "Add" button in the "Dependencies" tab of the pom.xml file as shown in the below screens.

1 comment: