【问题标题】:Need different versions of the same dependency in maven child projects在 Maven 子项目中需要不同版本的相同依赖项
【发布时间】:2012-11-26 13:26:21
【问题描述】:

我想在 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

【问题讨论】:

  • 服务器和客户端是否在同一个项目结构中?将它们拆分为具有不同 pom 文件的两个不同项目怎么样?
  • 我有两个 Pom 文件(客户端和服务器),它们引用了第三个常见的 Pom 文件(模型)。所以我最终得到了三个 Eclipse 项目。问题是这两个项目中的每一个都可能引用不同版本的通用(模型)Pom 项目。
  • 我以为你想允许不同版本的 'common'...我不再很喜欢 Maven,但是 AFAIK 你可以创建一个 parent project-pom,在那里定义你的依赖项(共同的),并从客户端和服务器的 pom 派生。
  • 我可以,但是两个子项目都必须使用相同版本的依赖模块。这不是我需要的。我需要客户端和服务器使用不同版本的模块的能力。
  • 你有一个依赖于 common 的 server-pom 和一个依赖于 common 的 client-pom。因此您可以独立更改依赖项的版本(加上客户端和服务器中的eclipse:eclipse,它会更新eclipse中的common-lib)...

标签: java maven dependencies


【解决方案1】:

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

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

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

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

【讨论】:

  • 您尝试过使用dependencyManagement 部分吗?您的 hello-model 是否在同一个项目层次结构中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多