java - 在 Maven 子项目中需要相同依赖项的不同版本

标签 java maven dependencies

我想在 Eclipse 中测试 Java 休息服务器和休息客户端的集成。 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

最佳答案

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

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

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

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

关于java - 在 Maven 子项目中需要相同依赖项的不同版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13394768/

相关文章:

java - 如何将 Artifact 和所有依赖项递归下载到目录中?

logging - 集成 Dropwizard 和 Apache Spark

java - 如何将 Java 颜色转换从 RGB 反转为 CIEXYZ/CIELAB

java - 如何在 Java 中输入无效后重新运行输入请求?

java - JVM 内部发生了什么,以至于当您在代码中的其他地方调用 Java 中的方法时,调用它的速度变慢了?

node.js - NPM 锁定文件无法正确处理传递依赖项

c - 在 C 中查找所有头文件和源文件依赖项的工具

java - 对同一个对象使用两种不同的序列化方法

通过编辑pom在Maven项目中编码Java文件

maven - Swagger |通过 maven 命令将 YAML 转换为 JSON