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.

package com.supun.spring;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

Then add the following to the pom.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>

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.

<dependency>
 <groupid>org.springframework</groupid>
 <artifactid>spring</artifactid>
 <version>2.5.5</version>
</dependency>

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.

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());
  }

}

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.

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();
    }
}

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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s