【问题标题】:Maven run Jetty Plugin by command line specifying contextPathMaven 通过指定 contextPath 的命令行运行 Jetty Plugin
【发布时间】:2021-11-12 10:04:15
【问题描述】:

我在 IntelliJ IDEA CE 上,我正在通过 Maven Jetty 插件运行一个战争应用程序。

我的 pom.xml 中没有插件(我也不想),所以我使用这个命令直接运行 Web 服务器:

mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run-exploded

它工作正常,但它不应用在 xml 文件 src/main/webapp/META-INF/context.xml

中指定的 contextPath

我想从终端命令中指定正确的 contextPath。 documentation 没有具体说明这一点。 我所做的测试(没有任何成功的结果)如下:

mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run-exploded -Dproject.artifactId='/project'
mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run-exploded -DcontextPath='/project'
mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run-exploded -Dconfiguration.webApp.contextPath="/project"
mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run-exploded -Djetty.configuration.webApp.contextPath="/project"

我错过了什么?

【问题讨论】:

  • 根据文档, 您的 webapp 的上下文路径。默认情况下,它设置为项目 pom.xml 中的 /${project.artifactId}。因此我不明白有必要尝试覆盖它吗?
  • contextPath 可配置在 Jetty 7.6.13 中被弃用,最终在 Jetty 9.0.0 中被移除。 <webApp><contextPath> 在 9.x 中的值一直是 / 而不是 /${project.artifactId}

标签: java maven jetty


【解决方案1】:

这最终是一个通用的 maven 技巧,而不是特定于 Jetty。

换句话说,如何弄清楚你可以用一个 maven 插件做什么。

$ mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:help
...(snip)...

jetty:help
  Display help information on jetty-maven-plugin.
  Call mvn jetty:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

那么让我们看看目标:run-exploded 的详细信息...

$ mvn org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:help -Ddetail=true -Dgoal=run-exploded
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- jetty-maven-plugin:9.4.26.v20200117:help (default-cli) @ standalone-pom ---
[INFO] Jetty :: Jetty Maven Plugin 9.4.26.v20200117
  Jetty maven plugins

jetty:run-exploded
  This goal is used to assemble your webapp into an exploded war and
  automatically deploy it to Jetty.
  
  Once invoked, the plugin runs continuously, and can be configured to scan for
  changes in the pom.xml and to WEB-INF/web.xml, WEB-INF/classes or WEB-INF/lib
  and hot redeploy when a change is detected.
  
  You may also specify the location of a jetty.xml file whose contents will be
  applied before any plugin configuration. This can be used, for example, to
  deploy a static webapp that is not part of your maven build.

  Available parameters:

    contextHandlers
      List of other contexts to set up. Consider using instead the <jettyXml>
      element to specify external jetty xml config file. Optional.

    contextXml
      Location of a context xml configuration file whose contents will be
      applied to the webapp AFTER anything in <webApp>.Optional.

    dumpOnStart (Default: false)
      Use the dump() facility of jetty to print out the server configuration to
      logging
      User property: dumponStart

    excludedGoals
      List of goals that are NOT to be used

    httpConnector
      A ServerConnector to use.

    jettyXml
      Comma separated list of a jetty xml configuration files whose contents
      will be applied before any plugin configuration. Optional.

    loginServices
      List of security realms to set up. Consider using instead the <jettyXml>
      element to specify external jetty xml config file. Optional.

    nonBlocking (Default: false)
      Determines whether or not the server blocks when started. The default
      behavior (false) will cause the server to pause other processes while it
      continues to handle web requests. This is useful when starting the server
      with the intent to work with it interactively. This is the behaviour of
      the jetty:run, jetty:run-war, jetty:run-war-exploded goals.
      
      If true, the server will not block the execution of subsequent code. This
      is the behaviour of the jetty:start and default behaviour of the
      jetty:deploy goals.

    reload (Default: automatic)
      reload can be set to either 'automatic' or 'manual' if 'manual' then the
      context can be reloaded by a linefeed in the console if 'automatic' then
      traditional reloading on changed files is enabled.
      User property: jetty.reload

    requestLog
      A RequestLog implementation to use for the webapp at runtime. Consider
      using instead the <jettyXml> element to specify external jetty xml config
      file. Optional.

    scanIntervalSeconds (Default: 0)
      The interval in seconds to scan the webapp for changes and restart the
      context if necessary. Ignored if reload is enabled. Disabled by default.
      Required: Yes
      User property: jetty.scanIntervalSeconds

    server
      A wrapper for the Server object

    skip (Default: false)
      Skip this mojo execution.
      User property: jetty.skip

    stopKey
      Key to provide when stopping jetty on executing java -DSTOP.KEY=<stopKey>
      -DSTOP.PORT=<stopPort> -jar start.jar --stop

    stopPort
      Port to listen to stop jetty on executing -DSTOP.PORT=<stopPort>
      -DSTOP.KEY=<stopKey> -jar start.jar --stop

    supportedPackagings
      Per default this goal support only war packaging. If your project use an
      other type please configure it here.

    systemProperties
      System properties to set before execution. Note that these properties will
      NOT override System properties that have been set on the command line or
      by the JVM. They WILL override System properties that have been set via
      systemPropertiesFile. Optional.

    systemPropertiesFile
      File containing system properties to be set before execution Note that
      these properties will NOT override System properties that have been set on
      the command line, by the JVM, or directly in the POM via systemProperties.
      Optional.
      User property: jetty.systemPropertiesFile

    useProvidedScope (Default: false)
      Whether or not to include dependencies on the plugin's classpath with
      <scope>provided</scope> Use WITH CAUTION as you may wind up with duplicate
      jars/classes.

    war (Default: ${project.build.directory}/${project.build.finalName})
      The location of the war file.
      Required: Yes

    webApp
      An instance of org.eclipse.jetty.webapp.WebAppContext that represents the
      webapp. Use any of its setters to configure the webapp. This is the
      preferred and most flexible method of configuration, rather than using the
      (deprecated) individual parameters like 'tmpDirectory', 'contextPath' etc.

这告诉您webApp 的配置是您设置contextPath 的位置

很遗憾,这是一个复杂的对象,您无法在命令行中指定它。

所以编辑您的pom.xml 以包含它。

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <configuration>
            <webApp>
              <contextPath>/foo</contextPath>
            </webApp>
          </configuration>
        </plugin>
  ...

另见How to define complex Maven properties in the comand line

【讨论】:

  • 我认为最初的问题不愿意将插件添加到 pom 中,实际上,上面的答案中提到了。我无法编辑此答案,因此在下面添加了另一个版本(也需要添加插件)。优点是 contextPath 可以从命令行替换为所需的原始问题。
  • 插件配置中的复杂对象会阻止您在不编辑 pom.xml 的情况下成功。 “另请参阅”中的后续链接是如何在不硬编码 pom 中的 contextPath 并允许在命令行上配置它的情况下做到这一点。您的后续答案只是重复了另一个答案。
  • 同意此评论
【解决方案2】:
  1. 这是链接 https://www.eclipse.org/jetty/documentation/jetty-9/index.html#jetty-maven-plugin

  2. 这是命令行:

mvn jetty:run -Dcontext=/abc
  1. 此命令行适用于以下 pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rahul.soAnswer</groupId>
    <artifactId>jetty-run</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jetty-answer</name>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.44.v20210927</version>
                <configuration>
                    <webApp>
                        <contextPath>${context}</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

您可以根据应用程序的需要选择向配置中添加更多详细信息

【讨论】:

  • 我最初删除了这个答案,但后来最终添加回来,因为添加插件似乎没问题。但这个答案确实是第一个答案的延伸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
相关资源
最近更新 更多