3

Java レスト サーバーとレスト クライアントの統合を Eclipse でテストしたいと思います。hello-client プロジェクトと hello-server プロジェクトはどちらも、同じ hello-model jar ファイル (POJO を含む) に依存しています。

問題は、クライアントまたはサーバーのさまざまなバージョンをチェックアウトし、eclipse でコードを編集してテストをデバッグできるようにしたいということです。

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 に関する関連する質問: さまざまなクライアントとサーバーのバージョンをテストするための最良の Git 戦略

完全なコード: https://github.com/itaifrenkel/hellopojo/blob/master/hellopojo-test/src/test/java/hellopojo/test/HelloTest.java

4

1 に答える 1

3

これを処理するために、Maven プロジェクトの依存関係管理の概念を使用します。

ステップ 1 - クライアントおよびサーバー プロジェクトの親 pom で、dependencyManagement を宣言します。

ステップ 2 - クライアント プロジェクトとサーバー プロジェクトの依存関係セクションで、バージョン情報をオーバーライドします。

これは、テスト スコープのクラスパスを通過するのに役立つ場合があります。コンパイルスコープのクラスパスに何が起こるかわかりません。

于 2012-11-26T05:26:21.010 に答える