Steps to integrate Spring and Maven

Spring and Maven

Spring Series – Learn Maven integration by Example


In this article we are going to work on Maven integration with Spring framework. As in the earlier Spring series, we are going to directly jump into the coding part to explore the implementation. We will explain all the components and how they can be implemented by using working code examples.

Maven is very popular nowadays for building projects in a clean way. The meaning of the word ‘MAVEN’ is ‘Accumulator of knowledge’. The main advantage of maven is to handle building process in an easy way and it makes the entire process more manageable compared to ant script.

Following is a simple example of maven and spring integration.

  • The first component is a java class which contains a simple message.

package com.techalpine;

public class SpringMaven


public static void main( String[] args )


System.out.println( “Hello Maven!” );



  • The second component is xml file used by maven to specify dependencies and other details. We’re going to add a little bit to this file so that we can easily run our application though maven.





















  • Now the 3rd step is to compile the code by using the command ‘mvn compile’. It will display an output something like this.

$ mvn compile

[INFO] Scanning for projects…

[INFO] ————————————————————————


[INFO] ————————————————————————

[INFO] Total time: 1 second

[INFO] Finished at: Fri Sep 05 19:50:45 BST 2008

[INFO] Final Memory: 5M/12M

[INFO] ————————————————————————

$ mvn exec:java

[INFO] [exec:java]

Hello Maven

  • The following is a modification of pom.xml file to integrate the spring framework.








  • The following class has been created to test the integration of spring with maven. This is another simple java class to print a messge.

package com.techalpine;

public class helloMaven


private String name;

public void setName(String name)

{ = name;


public String getName()


return name;


public void greet()


System.out.println(“Hello ” + getName());



  • Following is the xml configuration file (xml) for spring framework.

<?xml version=”1.0″ encoding=”UTF-8″?>




<bean id=”hello” class=”com.techalpine.helloMaven”>

<property name=”name” value=”TechAlpine” />



  • Following is the java class to test the spring maven integration.

package com.techalpine;

import com.techalpine.helloMaven;


import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

public class SpringMavenTest


public static void main( String[] args )


BeanFactory factory = new XmlBeanFactory(

new ClassPathResource(“spring_maven.xml”));

helloMaven hello = (helloMaven) factory.getBean(“hello”);




Now if you build and run the application it will display the following output.

Hello TechAlpine


This article is mainly for the developers who wants to try things hands-on. We have kept some theoretical part with more stress on the coding part. The focus on this Spring series is to help the developers to write code and implement it in their project. Hope this article will serve that purpose.

Please follow and like us:
Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

× 3 = 24

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email