在maven子项目中需要相同依赖项的不同版本

时间:2012-11-15 09:39:56

标签: java maven dependencies

我想在Eclipse中测试Java rest服务器和rest客户端的集成。 hello-client和hello-server项目都依赖于相同的hello-model jar文件(包含POJO)。

问题在于我想查看客户端或服务器的不同版本,能够在eclipse中编辑代码并能够调试测试 - 即使它们依赖于不同版本的hello-model

我尝试使用Maven shade插件重命名服务器中的模型包:

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>

这是单元测试代码:

@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();
}

stackoverflow上的相关问题: Best Git strategy for testing different client and server versions

完整代码: https://github.com/itaifrenkel/hellopojo/blob/master/hellopojo-test/src/test/java/hellopojo/test/HelloTest.java

1 个答案:

答案 0 :(得分:3)

我们使用Maven项目的dependencyManagement概念来处理这个问题。

步骤1 - 在客户端和服务器项目的父pom中声明dependencyManagement。

步骤2 - 在客户端和服务器项目的依赖项部分中覆盖版本信息。

这可以帮助您完成测试范围类路径。我不确定它会对编译范围类路径做什么。

相关问题