Setting up Maven 2 projects in Eclipse

Open eclipse IDE and Go to Help->Software Updates-> Find and Install

Add the following location to add Maven 2 Plugin in Eclipse as shown in Figure 1.1
http://download.eclipse.org/technology/m2e/releases

Figure 1.1 Figure 1.1

Now .m2 folder is created in “C:\Documents and Settings\ on Windows. Create a file settings.xml in .m2 folder and paste the following code.

<settings>
  <localRepository>C:\\Documents and Settings\\<loggedin_username>\\.m2</localRepository>
  <interactiveMode>true
  <usePluginRegistry/>
  <offline>false
  <profiles>
    <profile>
      <activation>
        
         <jdk/>
         <os>
           <name/>
           <family/>
           <arch/>
           <version/>
         </os>
         <property>
           <name/>
           <value/>
         </property>
         <file>
           <missing/>
           <exists/>
         </file>
       </activation>
       <properties/>
       <repositories>
         <repository>
           <releases>
             <enabled/>
             <updatePolicy>never </updatePolicy>
             <checksumPolicy/>
           </releases>
           <snapshots>
             <enabled/>
             <updatePolicy>never </updatePolicy>
             <checksumPolicy>fail </checksumPolicy>
           </snapshots>
           <id>central
           <name>Maven Central Repository </name>
           <url>http://repo2.maven.org/maven2 </url>
           <layout>default </layout>
         </repository>
       </repositories>
       <pluginRepositories>
         <pluginRepository>
           <releases>
             <enabled/>
             <updatePolicy>never </updatePolicy>
             <checksumPolicy/>
           </releases>
           <snapshots>
             <enabled/>
             <updatePolicy>never </updatePolicy>
             <checksumPolicy>fail </checksumPolicy>
           </snapshots>
           <id>central </id>
           <name>Maven Plugin Repository </name>
           <url>http://repo2.maven.org/maven2 </url>
           <layout>default </layout>
         </pluginRepository>
       </pluginRepositories>
       <id>default </id>
     </profile>
   </profiles>
 </settings>

1. Create Project

Create a New Maven Project as shown in Figure 1.2

Figure 1.2 Figure 1.2


In the New Maven Project wizard, select the Create a simple project (skip archetype selection) check box as shown in Figure 1.3

Figure 1.3 Figure 1.3

Click Next
In the New Maven Project wizard enter the following values as shown in Figure 1.4

Group Id: com.kruders.helloworld
Artifact Id: HelloWorld
Version: 0.0.1-SNAPSHOT
Packaging: jar

Figure 1.4 Figure 1.4

2. Add Spring Dependency

Write the following code in pom.xml to add Spring 3 dependency

<properties>
   <spring.version>3.0.6.RELEASE </spring.version>
 </properties>
 <dependencies>
 
   <!-- Spring 3 dependencies -->
   <dependency>
       <groupId>org.springframework </groupId>
       <artifactId>spring-core </artifactId>
       <version>${spring.version} </version>
   </dependency>
 
   <dependency>
       <groupId>org.springframework </groupId>
       <artifactId>spring-context </artifactId>
       <version>${spring.version} </version>
   </dependency>
 
</dependencies>

3. Create Source File

Now create a Java File HelloWorld.java in com.kruders package.

package com.kruders;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class HelloWorld {
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
      
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Bean.xml");
  
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloBean");
        System.out.println("Message : " + helloWorld.getName());
    }
}

4. Create Bean Configuration File

Now create Bean Configuration File as showin Figure 1.5 and name it Spring-Bean.xml

Figure 1.5 Figure 1.5

Add the following code in Spring-Bean.xml

<bean id="helloWorld" class="com.kruders.HelloWorld">
    <property name="message" value="Hello World!">
    </property>
</bean>

Run HelloWorld.java

The folder structure of the example is shown below in Figure 1.6

Figure 1.6 Figure 1.6

You can download the source code of this example here.

,

No comments yet.

Leave a Reply