修复Jetty分叉项目停止问题 | AI生成和翻译

Home 2025.09

你实际上大部分操作都是正确的——出现卡顿几乎总是因为停止目标无法连接到分叉的Jetty(错误的stopHost/端口/密钥作用域)或者因为run-forked意外保持Maven连接。以下是两个可靠的解决方案,任选其一。

方案A —— 可靠模式:jetty:start(守护进程)+ jetty:stop

这种方式完全避免使用run-forked,是集成测试生命周期中最稳定的方案。

需要调整的内容

  1. pre-integration-test阶段固定插件版本并使用带daemon=truestart目标
  2. 将Python集成测试保留在integration-test阶段
  3. post-integration-test阶段使用stop目标
  4. 两个执行块中提供相同stopHost/stopPort/stopKey配置
<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.4.54.v20240208</version>

  <configuration>
    <httpConnector>
      <port>8080</port>
      <host>127.0.0.1</host>
    </httpConnector>
  </configuration>

  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>start</goal>
      </goals>
      <configuration>
        <daemon>true</daemon>
        <stopHost>127.0.0.1</stopHost>
        <stopPort>8081</stopPort>
        <stopKey>stop</stopKey>
        <jvmArgs>
          -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${jacoco.it.exec},append=true
        </jvmArgs>
        <webApp>
          <contextPath>/</contextPath>
        </webApp>
      </configuration>
    </execution>

    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop</goal>
      </goals>
      <configuration>
        <stopHost>127.0.0.1</stopHost>
        <stopPort>8081</stopPort>
        <stopKey>stop</stopKey>
        <stopWait>15</stopWait>
      </configuration>
    </execution>
  </executions>
</plugin>

此方案解决”卡住”问题的原理


方案B —— 保留run-forked,但确保停止功能实际生效

如果仍想使用run-forked,常见陷阱包括:

按以下方式收紧配置:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.4.54.v20240208</version>

  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>run-forked</goal>
      </goals>
      <configuration>
        <stopHost>127.0.0.1</stopHost>
        <stopPort>8081</stopPort>
        <stopKey>stop</stopKey>
        <stopWait>15</stopWait>

        <httpConnector>
          <host>127.0.0.1</host>
          <port>8080</port>
        </httpConnector>

        <jvmArgs>
          -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${jacoco.it.exec},append=true
        </jvmArgs>
        <webApp>
          <contextPath>/</contextPath>
        </webApp>
      </configuration>
    </execution>

    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop</goal>
      </goals>
      <configuration>
        <stopHost>127.0.0.1</stopHost>
        <stopPort>8081</stopPort>
        <stopKey>stop</stopKey>
        <stopWait>15</stopWait>
      </configuration>
    </execution>
  </executions>
</plugin>

额外保护措施


可选安全网 —— 如果stop失败则强制终止子进程

通过写入文件的PID强制终止分叉的Jetty:

  1. run-forked/start配置中添加: ```
${project.build.directory}/jetty.pid

```

  1. post-integration-test之后,在verify阶段添加exec-maven-plugin,检查pid文件并在进程仍存活时强制终止

从日志中发现的几个小问题


如果需要,请提供完整的Jetty插件配置块及任何settings.xml镜像配置(用于本地仓库路径),我会为您的代码库量身定制最精确的最小修改方案。


Back Donate