maven的项目进行拆分、聚合**


1. 现在需要对maven_ssh项目进行拆分,准备把action、service和dao分别作为单独的项目拆分出来。但是必须还要有一个父工程,做聚合。
* 创建ssh_parent的父工程
* 父工程中的pom.xml配置文件引入所有jar包的坐标,这样子工程继承,就不再需要导入jar包了。
* 父工程起到聚合的作用。

* 创建ssh_web的子工程
* 编写web相关的代码

* 创建ssh_service的子工程
* 编写业务层相关的代码

* 创建ssh_dao的子工程
* 编写dao成相关的代码

![](./图片/01-maven的拆分.bmp)

2. 创建ssh_parent的父工程
* 创建maven project,注意打包方式选择pom
* 在pom.xml配置文件引入所有jar包的坐标(注意:把junit的jar包删除掉,一会做测试使用)
* 将父工程打包到仓库中

3. 创建ssh_dao的子工程
* 在父工程上点击右键,选择maven module,打包方式选择jar包。
* 拷贝相关的domian、dao程序和配置文件
* 在该工程的pom.xml配置文件中引入junit的jar包
* 编写测试方法
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class DaoTest {

@Resource(name="customerDao")
private CustomerDao customerDao;

@Test
public void run1(){
Customer customer = customerDao.findById(1);
System.out.println(customer);
}
}
* 给dao打包,保存到本地仓库中。

4. 创建ssh_service的子工程
* 在父工程上点击右键,选择maven module,打包方式选择jar包。
* 拷贝相关的service和实现类
* 拷贝配置文件等
* 编写测试的方法
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"classpath:applicationContext-service.xml","classpath:applicationContext.xml"})
public class ServiceTest {

@Resource(name="customerService")
private CustomerService customerService;

@Test
public void run1(){
Customer customer = new Customer();
customer.setName("测试");
customer.setAge(20);
customerService.save(customer);
}
}
* 给service打包,保存到本地仓库中。

5. 依赖传递是存在范围的,不能一直依赖下去。
* 例如:service依赖dao,而dao依赖junit。至于service是否有junit的传递依赖,取决于dao对junit依赖的scope属性。
* 查看大纲文档上的图对照。
* 总结:不用纠结于图形,如果没有传递依赖进来,那么就自己手动添加即可。

6. 创建ssh_web的子工程,注意:打包选择war包
* 拷贝web.xml的配置文件
* 拷贝action、struts.xml配置文件等
* 把applicationContext.xml拷贝过来
* 启动tomcat服务器,进行测试

posted @ 2017-06-08 23:01  changfengdunhaung  阅读(400)  评论(0)    收藏  举报