1

我认为这很简单,但有问题:

  • Project1 是战争类型。它创建了一个完整的 webapp .war 文件,包括一些 Apache 模块(solr/lucene)和我们的一些自定义代码。
  • Project2 是一个现有的应用程序。它需要启动嵌入式 Jetty 来对 Project1 的 war 文件进行查询。(见下面的代码)

主要问题:

  • Project2在实例化Jetty时,需要传入WAR文件的完整路径,但是每次都会改变。Maven 将版本号添加到 Project1 的 war 文件中。

集会救援?

  • 我能够让自定义程序集工作,但无法摆脱 Project1 的版本控制标记。

但我总是以 Project1-1.4.1-20120530.233546-2.war 结束。它在一个更方便的地方,但名字仍然很奇怪。

Project2中的码头代码:

    // Context
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    String jettyHome = System.getProperty( "jetty.home", ".." );
    String fullWarName = ...;    // Project1's WAR file. This path always changes
    webapp.setWar( fullWarName );
    // Server
    Server server = new Server( kPort ); // TODO: get from config
    server.setHandler(webapp);
    server.start();
    server.join();

其他注意事项:

  • 我意识到有一个 maven-jetty 插件,但我认为这在这里不合适。它似乎是针对单元测试的,而且我们的应用程序堆栈在运行时不使用 maven 来启动服务。
  • 我也知道 Solr 有一个完全嵌入式的版本,不需要 Web 容器,但它已经被弃用了一段时间,使用起来不是一个好主意。

有没有更好的方法来重构这个项目?也许这不是“行家之道”?

4

1 回答 1

1

事实证明我不需要一个程序集(我在内部得到的建议),而是在主 pom.xml 中有一些更简单的东西。此外,在这里解开战争的包装被证明是一个好主意。

在 Project1 的 pom.xml 的顶部,我有:

 <groupId>com.my.group</groupId>
 <artifactId>project-one</artifactId>
 <version>1.2.3-SNAPSHOT</version>
 <packaging>war</packaging>

这靠近 Project2 的 pom.xml 的底部

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack-webapp</id>
      <phase>package</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
          <artifactItems>
           <artifactItem>
            <groupId>com.my.group</groupId>
            <artifactId>project-one</artifactId>
            <version>1.2.3-SNAPSHOT</version>
            <type>war</type>
            <overWrite>true</overWrite>
            <outputDirectory>${project.build.directory}/webapps/project-one</outputDirectory>
           </artifactItem>
         </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

然后在启动 Jetty 时,我有:

webapp.setWar( "target/webapps/project-one" );

我仍然认为某些 Jetty 设置可能存在问题,但我认为这是正确的方向。

于 2012-06-07T19:52:36.430 回答