如果还没有搭建好Maven环境,请参考上一节内容
http://blog.csdn.net/autfish/article/details/51008788
直接勾选 Create a simple project
Group Id是项目组织代码,这里填写包名demo.maven; Artifact Id填写项目名称。其他忽略 -> Finish
新建的项目目录结构如下图:
这里添加了junit和spring用于演示
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo.maven</groupId> <artifactId>maven-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.1.RELEASE</version> </dependency> </dependencies> </project>
在源码目录 src/main/java下新建Package: demo.maven
新建类Pet
package demo.maven; public class Pet { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
在src/main/resources下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="pet" class="demo.maven.Pet"> <property name="name" value="Tom"></property> </bean> </beans>
在 src/test/java下新建 testcase.TestPet.java
package testcase; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import demo.maven.Pet; public class PetTest extends TestCase { public void testPet() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Pet pet = context.getBean("pet", Pet.class); System.out.println(pet.getName()); assertTrue(true); } }
完成上述步骤后,目录结构如下:
在PetTest.java上右键 -> Run as -> JUnit test
源码下载 http://pan.baidu.com/s/1nuZ4VNj
至此,完成第一个Maven项目,下一节来一步步演示创建第一个Web项目