Multi-module Maven project and jetty:run

拈花ヽ惹草 提交于 2019-11-28 16:05:47

There is no magical solution and the only one I know is a bit hacky and rely on the extraClasspath element that you can use to declare extra class directories, relatively. Like this (from 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>

Create a profile inside the war module (project-war). Within this profile, configure jetty to attach to a lifecycle phase and execute the run goal explicitly. Now when maven runs from the toplevel project with that profile enabled, it will invoke jetty:run and have sister module dependency resolution (as is normal when executing maven commands from the toplevel project).

The example configuration, when placed in the pom.xml of the web module (project-war), arranges for jetty:run to execute during the test phase. (You may choose another phase, but make sure it's after compile.)

Run from toplevel: mvn test -Pjetty-run or mvn test -DskipTests=true -Pjetty-run. This will compile dependencies as required and make them available but invoke jetty:run within the correct module.

<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>

Using extraClasspath in jetty configuration works... but for some reason, if dependant jars (from other modules) are outdated some things won't work correctly.

Add jetty plugin to the root pom and configure a contextHandler pointing to the desired war. This works for me in a project with multiple jar modules and two overlay wars.

<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

I know you're asking for plugin configuration, but you could just define the project in the maven command:

$ mvn jetty:run --projects project-war
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!