Run Spring by Maven2 in Vista

Run Spring by Maven2 in Vista

I give a basic helloworld Spring example which references the example from "Spring in Action" chapter2.

Of course JDK must be installed, %JAVA_HOME% and both %Path% and %Classpath% are filled with correct path.

Maven2 Installation

Because it's the first example showing how Maven works in Spring, all the mandatory steps are included without other optional configurations.

Windows 2000/XP/Vista

   1. Unzip the distribution archive, i.e. apache-maven-2.0.10-bin.zip to the directory F:\Development\j2eelib\apache_maven\apache-maven-2.1.0

   2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value F:\Development\j2eelib\apache_maven\apache-maven-2.1.0
 
Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven < 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.

   3. In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%"bin.

   4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.

   5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.

   6. In the same dialog, make sure that JAVA_HOME exists.

   7. Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.


Create First "Helloworld" Spring Project by Maven2.

Step 1.
Open windows cmd and go into a directory which is prepared to be the project's location.
e.g. F:\Development\Java\maven\

mvn archetype:create -DgroupId=com.springinaction.chapter01 -DartifactId=springinaction

Step 2.
Go into sub-directory springinaction which is created by mvn command above: cd springinaction
It's worth to mention that checking all the available dependencies on Maven's website as well as their version number is necessary:
http://mvnrepository.com/artifact/org.springframework/spring

Config pom.xml first:
 1  < project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 2    xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
 3     < modelVersion > 4.0.0 </ modelVersion >
 4     < groupId > com.springinaction.chapter01 </ groupId >
 5     < artifactId > springinaction </ artifactId >
 6     < packaging > jar </ packaging >
 7     < version > 1.0-SNAPSHOT </ version >
 8     < name > springinaction </ name >
 9     < url > http://maven.apache.org </ url >
10     < dependencies >
11       < dependency >
12         < groupId > junit </ groupId >
13         < artifactId > junit </ artifactId >
14         < version > 3.8.1 </ version >
15         < scope > test </ scope >
16       </ dependency >
17 
18       <!--  Choose to add each module as a dependency as it's needed. -->
19       < dependency >
20           < groupId > org.springframework </ groupId >
21       < artifactId > spring </ artifactId >
22       < version > 2.5.6 </ version >
23       </ dependency >
24      
25       < dependency >
26           < groupId > org.springframework </ groupId >
27       < artifactId > spring-aop </ artifactId >
28       < version > 2.5.6 </ version >
29       </ dependency >
30 
31        < dependency >
32           < groupId > org.springframework </ groupId >
33       < artifactId > spring-beans </ artifactId >
34       < version > 2.5.6 </ version >
35        </ dependency >
36 
37        < dependency >
38           < groupId > org.springframework </ groupId >
39       < artifactId > spring-core </ artifactId >
40       < version > 2.5.6 </ version >
41        </ dependency >
42 
43     </ dependencies >
44  </ project >


Edit App.java which have been created already or create any new java which is needed.
 1  package  com.springinaction.chapter01;
 2  import  org.springframework.beans.factory.BeanFactory;
 3  import  org.springframework.beans.factory.xml.XmlBeanFactory;
 4  import  org.springframework.core.io.FileSystemResource;
 5 
 6  import  com.springinaction.chapter01.service.GreetingService;
 7 
 8  public   class  App{
 9       public   static   void  main( String[] args ){
10          BeanFactory factory  =   new  XmlBeanFactory( new  FileSystemResource( " hello.xml " ));
11 
12      GreetingService greetingService  =  (GreetingService) factory.getBean( " greetingService " );
13      greetingService.sayGreeting();
14      }
15  }

Create service directory under com.springinaction.chapter01 and create GreetingService.java:
1  package  com.springinaction.chapter01.service;
2 
3  public   interface  GreetingService{
4       void  sayGreeting();
5 

Create serviceimpl directory under com.springinaction.chapter01.service and create GreetingServiceImpl.java
 1  package  com.springinaction.chapter01.service.serviceimpl;
 2 
 3  import  com.springinaction.chapter01.service.GreetingService;
 4 
 5  public   class  GreetingServiceImpl  implements  GreetingService{
 6       private  String greeting;
 7       public  GreetingServiceImpl(){}
 8       public  GreetingServiceImpl(String greeting){
 9           this .greeting  =  greeting;
10      }
11       public   void  sayGreeting(){
12          System.out.println(greeting);
13      }
14       public   void  setGreeting(String greeting){
15           this .greeting  =  greeting;
16      }
17  }

The directories structure should be like this in the red square.

The directories service and serviceimpl are created manually, but others are created by Maven automatically.

Create hello.xml under root directory springinaction
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2  < beans  xmlns ="http://www.springframework.org/schema/beans"
 3         xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation ="
 5  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
 6 
 7  < bean  id ="greetingService"  class ="com.springinaction.chapter01.service.serviceimpl.GreetingServiceImpl" >
 8       < property  name ="greeting"  value ="Hello World in Spring Bean!" />
 9  </ bean >
10 
11  </ beans >


Step3:
Under cmd mode and in the root directory springinaction:

mvn compile
mvn test

or

mvn package


And then:

java -cp target/springinaction-1.0-SNAPSHOT.jar;F:\Development\j2eelib\spring-framework-2.5.6\dist\spring.jar;F:\Development\j2eelib\spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar;F:\Development\j2eelib\spring-framework-2.5.6\dist\modules\spring-aop.jar;F:\Development\j2eelib\spring-framework-2.5.6\dist\modules\spring-beans.jar;F:\Development\j2eelib\spring-framework-2.5.6\dist\modules\spring-core.jar com.springinaction.chapter01.App

Notice: Change your cp parameters according to specific location in your environment.

It is important to include all necessary jar files which are needed in the project.
-cp means classpath.


Result:



After debug, make clean is necessary which will delete all compiled classes, jar and their directories.

mvn clean


你可能感兴趣的:(Run Spring by Maven2 in Vista)