TechAlpine – The Technology world

Steps to integrate Spring and Maven

Spring and Maven

Spring Series – Learn Maven integration by Example

Overview

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 SpringMaven.java 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.

<project

<build>

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<executions>

<execution>

<goals>

<goal>java</goal>

</goals>

</execution>

</executions>

<configuration>

<mainClass>com.techalpine.SpringMaven</mainClass>

</configuration>

</plugin>

</plugins>

</build>

</project

  • 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] BUILD SUCCESSFUL

[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.

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

<version>2.5.5</version>

</dependency>

</dependencies

  • 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)

{

this.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″?>

<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”

“http://www.springframework.org/dtd/spring-beans.dtd”>

<beans>

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

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

</bean>

</beans

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

package com.techalpine;

import com.techalpine.helloMaven;

import org.springframework.core.io.ClassPathResource;

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”);

hello.greet();

}

}

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

Hello TechAlpine

Conclusion

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.








Tagged on: ,

Leave a Reply

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


5 + = 7

TechAlpine Books
-----------------------------------------------------------