2012-11-15 81 views
3

我想在Eclipse中測試Java rest服務器和其他客戶端的集成。 hello-client和hello-server項目都依賴於相同的hello-model jar文件(包含POJO)。在Maven子項目中需要不同版本的相同依賴項

問題是,我想查看客戶端或服務器的不同版本,能夠在eclipse中編輯代碼並能夠調試測試 - 即使它們依賴於不同版本的hello-模型。

我試圖使用Maven插件蔭的模型包在服務器重命名:

hellopojo.model - > hellopojo.server.model

,但它不影響Eclipse的(錯誤的Maven階段我假設)。

<build> 
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>2.0</version> 
    <executions> 
     <execution> 
     <phase>package</phase> 
     <goals> 
      <goal>shade</goal> 
     </goals> 
     <configuration> 
      <relocations> 
      <relocation> 
       <pattern>hellopojo.model</pattern> 
       <shadedPattern>hellopojo.server.model</shadedPattern> 
      </relocation> 
      </relocations> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 
</build> 

下面是單元測試代碼: Best Git strategy for testing different client and server versions

完整代碼:

@Parameters({"port"}) 
@BeforeClass 
public static void startWebapp(
@Optional("8081") int port) throws Exception { 

    restUri = "http://localhost:"+port+"/rest"; 
    client = new HelloClient(new URI(restUri)); 

    server = new Server(port); 
    server.setHandler(createWebAppContext()); 
    server.start(); 
} 


private static ServletContextHandler createWebAppContext() { 
    ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    handler.setContextPath("/"); 
    ServletHolder servlet = new ServletHolder(new ServletContainer()); 
    servlet.setInitParameter(
     "com.sun.jersey.config.property.packages", 
     HelloResource.class.getPackage().getName()); 
    servlet.setInitParameter(
     "com.sun.jersey.api.json.POJOMappingFeature" , 
     "true"); 
    handler.addServlet(servlet, "/rest/*"); 
    return handler; 
} 

@AfterClass 
public static void stopWebapp() throws Exception { 
    server.stop(); 
} 

在計算器相關問題 https://github.com/itaifrenkel/hellopojo/blob/master/hellopojo-test/src/test/java/hellopojo/test/HelloTest.java

+1

您是否將服務器和客戶端都放在同一個項目結構中?把它們分成兩個截然不同的項目,包括獨特的pom文件呢? – Andy

+0

我有兩個POM文件(客戶端和服務器),指的是第三個普通的Pom文件(模型)。所以我最終得到了三個eclipse項目。問題是這兩個項目中的每一個都可能引用不同版本的通用(模型)Pom項目。 – itaifrenkel

+2

我以爲你想允許不同版本的'普通'...我不再是Maven,但是AFAIK你可以創建一個* parent * project-pom,定義你的依賴關係(共同的),以及從客戶端和服務器的pom派生而來。 – Andy

回答

3

我們使用Maven項目的dependencyManagement概念來處理這個。

步驟1 - 在客戶端和服務器項目的父pom中聲明dependencyManagement。

步驟2 - 在客戶端和服務器項目的依賴項部分中,覆蓋版本信息。

這可能會幫助您通過測試作用域類路徑。我不確定它對編譯範圍類路徑會做什麼。

+0

你是否嘗試過使用dependencyManagement部分?您的hello-model是否在同一個項目層次結構中? – SSR

相關問題