Getting Started with Maven and Spring
What is Maven ?
Apache Maven originated as an attempt to simplify the build process for the now defunct Apache Jakarta Alexandria project. Its formative years were then spent in the Apache Turbine project where it eventually came to replace a brittle and fragile build system based on Apache ANT.
First verify the installation.
First create a sample application using maven.
$ mvn archetype:generate — batch-mode
-DarchetypeArtifactId=maven-archetype-quickstart
-DgroupId=com.supun.spring
-DartifactId=spring-hello
-Dversion=1.0-SNAPSHOT
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
Here is the App class which is generated by Maven.
[sourcecode language=”java”]
package com.supun.spring;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( “Hello World!” );
}
}
[/sourcecode]
Then add the following to the pom.xml
[sourcecode language=”xml”]
<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.supun.spring.App</mainclass>
</configuration>
</plugin>
</plugins>
</build>
[/sourcecode]
Now compile the project using mvn compile and run using mvn exec:java . Here is the output.
Now add the Spring dependencies to the project.
[sourcecode language=”xml”]
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring</artifactid>
<version>2.5.5</version>
</dependency>
[/sourcecode]
Then, when we compile the project, the spring jars will be downloaded automatically. Now create a new class inside the spring-hellosrcmainjavacomsupun folder as follows.
[sourcecode language=”java”]
package com.supun;
public class SayHello
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void greet()
{
System.out.println(“Hello “ + getName());
}
}
[/sourcecode]
Now add application-context.xml file to configure the spring beans. This should be placed inside the resources folder. src/main/resources
Then change the App.java class as follows.
[sourcecode language=”java”]
package com.supun.spring;
import com.supun.SayHello;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
BeanFactory factory = new XmlBeanFactory(
new ClassPathResource(“application-context.xml”));
SayHello hello = (SayHello) factory.getBean(“hello”);
hello.greet();
}
}
[/sourcecode]
Then clean and compile the project. Then run it. The output as following.
...[INFO] [exec:java]
....
INFO: Loading XML bean definitions from class path resource [application-context.xml]
Hello Supun