【问题标题】:How to write an Ant task to start the embedded jetty in eclipse?如何编写 Ant 任务以在 Eclipse 中启动嵌入式码头?
【发布时间】:2016-01-12 14:58:56
【问题描述】:

我可以像这样使用 pom.xml 中的jetty-maven-plugin 配置成功配置和启动嵌入式码头,

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <stopKey>webappStop</stopKey>
        <stopPort>9191</stopPort>
        <httpConnector>
          <host>localhost</host>
          <port>9090</port>
        </httpConnector>
    </configuration>
</plugin>

我可以右键单击项目并运行 maven 目标,jetty:run,项目开始在端口 9090 上运行,

[INFO] jetty-9.3.0.M1
[INFO] No Spring WebApplicationInitializer types detected on classpath
[INFO] Started ServerConnector@1023a4c5{HTTP/1.1,[http/1.1]}{localhost:9090}
[INFO] Started @8012ms
[INFO] Started Jetty Server

现在我需要为服务器启动和停止命令编写一个 Ant 任务,而不是每次都右键单击并运行 maven 目标。

【问题讨论】:

    标签: ant embedded-jetty maven-jetty-plugin


    【解决方案1】:

    创建如下简单的build.xml,并创建两个ant任务来启动和停止服务器,

    build.xml:

    <project name="Demo Project" basedir=".">
    
      <path id="jetty.plugin.classpath">
        <fileset dir="jetty-lib" includes="*.jar"/>
      </path>
    
      <taskdef classpathref="jetty.plugin.classpath" resource="tasks.properties" loaderref="jetty.loader" />
    
        <target name="jetty.run">
            <jetty.run />
        </target>
    
        <target name="jetty.stop">
            <jetty.stop />
        </target>
    
    </project>
    

    在项目根目录下创建一个jetty-lib文件夹,并在其中放置以下jar,

    javax.servlet-3.0.jar
    jetty-ant-9.3.0.M1.jar
    jetty-http-9.3.0.M1.jar
    jetty-io-9.3.0.M1.jar
    jetty-security-9.3.0.M1.jar
    jetty-server-9.3.0.M1.jar
    jetty-servlet-9.3.0.M1.jar
    jetty-util-9.3.0.M1.jar
    jetty-webapp-9.3.0.M1.jar
    

    更多配置见jetty-ant官方文档。

    【讨论】:

    • 建议您使用官方/稳定版本的 Jetty,而不是 Milestone。如果您想要最新的稳定版本,请使用 9.3.5.v20151012
    • @JoakimErdfelt 感谢您的建议。我也会更新我的 pom.xml 和 jetty-lib *.jars。但我无法通过&lt;jetty.stop /&gt; 停止服务器。如果我在 jetty-stop 之后运行 jetty-run,我仍然可以看到端口是打开的并抛出我 Address already in use : bind exception。我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多