1. 程式人生 > >使用 Maven 管理項目

使用 Maven 管理項目

lock 範圍 tom 進行 auto getc round 百度 -s

最近的練手項目使用的是 Maven 在管理項目,在使用 Maven 管理項目時,三層的開發時分模塊開發的,parent-dao-service-web,所有的spring+struts + Hibernate的依賴都是加在 parent 上,dao-service-web都是作為子模塊,在模塊之間的關系處理的時候出現了幾個問題:

  1. junit測試包的傳遞依賴失效了
  2. 多個配置文件的讀取問題

我在 parent 工程沒有添加 Junit 的依賴,在編寫 dao 模塊是添加了 Junit 的 jar 包,理所當然的在 scope 中寫了 test 範圍,但是在 service 模塊中進行 Junit 測試時,顯示沒有依賴上 Junit 包,那是為什麽呢?百度了才想通,原來是 service 依賴的 dao 模塊的 install 之後的 jar 包,當 dao 模塊 install 時,scope 為 test 的 Junit包當然沒有被發布出來,service中也就不能傳遞依賴到 Junit了,這樣的解決辦法只能在 service 中添加 Junit 包的依賴。

因為采取的是模塊式的開發,spring的配置文件就被分布在各個模塊中了,在測試項目時需要讀取多個模塊中的 spring 配置文件時,使用到了之前沒有使用到的一個註解:

@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"}) 這個註解中的*號通配符表示會加載本模塊和依賴的jar包中的類路徑下的applicationContext-開頭的配置文件(只有spring配置文件才會這樣命名)

//@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"})
@ContextConfiguration(locations={"classpath:applicationContext-dao.xml","classpath:applicationContext-service.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class CustomerServiceImplTest { @Autowired private CustomerService customerService; @Test public void test() { Customer customer
= customerService.findById(1L); System.out.println("********************"+customer.getCustName()); } }

使用 Maven 管理項目