Thursday, April 4, 2013

Maven-Spring

OPTION1

1. install m2Eclipse in Eclipse
2. Create maven project
3. create SOURCE FOLDER -> "sre/main/java/resources"
4. add config xml files to resources folder
5. add dependencies to pom.xml

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
...

Maven will download those dependencies automatically.




OPTION2

In mkyong's example, he established the maven project in linux and then import it into eclipse.
http://www.mkyong.com/spring/quick-start-maven-spring-example/

1. Generate project structure with Maven

In command prompt, issue following Maven command :
mvn archetype:generate -DgroupId=com.mkyong.common -DartifactId=SpringExamples 
 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Maven will generate all the Java’s standard folders structure for you (besides resources folder, which you need to create it manually)

2. Convert to Eclipse project

Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.
mvn eclipse:eclipse
Later, import the converted project into Eclipse IDE.
Create a resources folder (not folder)
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

3. Add Spring dependency

Add Spring dependency in Maven’s pom.xml file.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mkyong.common</groupId>
 <artifactId>SpringExamples</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>SpringExamples</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 
  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.6</version>
  </dependency>
 
 </dependencies>
</project>
Issue “mvn eclipse:eclipse” again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.


I have try this one but the eclipse complained that ApplicationContext and ClassPathXmlApplicationContext cannot be resolve although the spring framework are in the lib path.

Solution :
Convert the project to maven project in Eclipse. (Download m2eclipse firstly: help->install->m2eclipse)


The reason might be that eclipse didn't know what maven is.


OK, LET'S MOVE ON...


Why use XML to config the project?
loose coupling;






No comments:

Post a Comment