36

我正在尝试将 Maven WAR 项目拆分为两个模块,以便可以使用命令行工具构建单独的 JAR 文件。结果具有以下结构:

  • pom.xml(包装pom,有两个模块)
  • project-jar/
    • pom.xml(包装jar
  • project-war/
    • pom.xml(包装war,取决于project-jar

如果我mvn从根目录运行命令,一切正常。我想继续使用mvn jetty:run,但为此我需要在 WAR 子项目中执行命令。如果我这样做,找不到project-jar子项目,所以它不会运行。即使目录mvn jetty:run-war中有一个完全组装的 WAR 文件也会target失败,因为它首先尝试“构建”项目。我只能通过安装project-jar到本地 Maven 存储库来使其工作,这不是很好。

有没有办法在多模块 Maven 配置中使用 Jetty 插件?

4

6 回答 6

29

project-war在 war 模块 ( ) 中创建配置文件。在此配置文件中,将 jetty 配置为附加到生命周期阶段并run明确执行目标。现在,当 maven 从启用该配置文件的顶层项目运行时,它将调用 jetty:run 并具有姊妹模块依赖关系解析(从顶层项目执行 maven 命令时是正常的)。

示例配置放在 web 模块 ( project-war) 的 pom.xml 中时,会安排 jetty:run 在该test阶段执行。(您可以选择另一个阶段,但要确保它在 之后compile。)

从顶层运行:mvn test -Pjetty-runmvn test -DskipTests=true -Pjetty-run. 这将根据需要编译依赖项并使它们可用,但在正确的模块中调用 jetty:run。

<profiles>
  ...
  <!-- With this profile, jetty will run during the "test" phase -->
  <profile>
    <id>jetty-run</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>7.1.6.v20100715</version>
          <configuration>
            ...
            <webAppSourceDirectory>
              ${project.build.directory}/${project.build.finalName}
            </webAppSourceDirectory>
            ...
          </configuration>
          <executions>
            <execution>
              <id>jetty-run</id>
              <phase>test</phase>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
...
</profiles>
于 2011-05-09T18:47:57.270 回答
11

没有神奇的解决方案,我知道的唯一一个有点老套extraClasspath,相对而言,它依赖于可以用来声明额外类目录的元素。像这样(来自JETTY-662):

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>7.0.1.v20091125</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webAppConfig>
      <contextPath>/my-context</contextPath>
      <extraClasspath>target/classes;../my-jar-dependency/target/classes</extraClasspath>
    </webAppConfig>
    <scanTargets>
      <scanTarget>../my-jar-dependency/target/classes</scanTarget>
    </scanTargets>
  </configuration>
</plugin>
于 2010-09-03T16:49:10.073 回答
1

在码头配置中使用 extraClasspath 是可行的……但由于某种原因,如果依赖的 jars(来自其他模块)已经过时,有些事情将无法正常工作。

于 2010-10-14T13:20:39.717 回答
1

将 jetty 插件添加到根 pom 并配置一个 contextHandler 指向所需的战争。这适用于具有多个 jar 模块和两个覆盖战争的项目。

<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.M2</version>
<configuration>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <contextHandlers>
            <contextHandler
                implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
                <war>${project.basedir}/project-war/target/project-war-${project.version}.war</war>
                <contextPath>/</contextPath>
            </contextHandler>
        </contextHandlers>
    </configuration>
</plugin>

http://eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#running-more-than-one-webapp

于 2015-05-21T09:24:36.640 回答
0

我知道你要求插件配置,但你可以在 maven 命令中定义项目:

$ mvn jetty:run --projects project-war
于 2016-03-07T21:27:58.020 回答
0

注释的文档jetty:run

从 Jetty 9.4.7 开始,在多模块构建中使用 jetty:run 时,不再需要先构建每个构成 webapp 依赖项的模块。因此,如果您的 webapp 依赖于项目中的其他模块并且它们同时存在于反应器中,则 jetty 将使用它们编译的类而不是它们来自本地 maven 存储库的 jar 文件。

因此,添加jetty-maven-plugin到 Maven 根项目并运行mvn jetty:run

于 2020-07-31T06:47:11.380 回答