我如何让sbt的Jetty使用本地域名?

时间:2011-03-01 11:42:40

标签: scala jetty lift sbt

我希望sbt> ~jetty推出的Jetty版本能够在my.name.local上收听,我在127.0.0.1设置为/etc/hosts。它是seems to be possible从sbt中改变Jetty的设置。

以下是我的项目内容:

import sbt._

class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
  // ...

  val jetty = "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "test"

  override lazy val jettyInstance = new JettyRunner(customJettyConfiguration) 

  def customJettyConfiguration = { 
    val myLog = log 
    val myJettyClasspath = jettyClasspath 
    val myScanDirectories = scanDirectories
    val myScanInterval = scanInterval

    new CustomJettyConfiguration { 
      def classpath = jettyRunClasspath
      def jettyClasspath = myJettyClasspath
      def war = jettyWebappPath
      def contextPath = jettyContextPath
      def classpathName = "test"
      def parentLoader = buildScalaInstance.loader
      def scanDirectories = Path.getFiles(myScanDirectories).toSeq
      def scanInterval = myScanInterval
      def port = jettyPort
      def log = myLog      
      override def jettyConfigurationXML = 
        <Configure class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="virtualHosts">
            <Array type="java.lang.String">
              <Item>my.name.local</Item>
            </Array>
          </Set>
        </Configure>
    }
  }
}

虽然它似乎没有投诉而发布,但就我所知,访问my.name.local并没有打到Jetty。

2 个答案:

答案 0 :(得分:3)

我个人更喜欢在Linux上使用iptables将端口80重新路由到8080,而不是以sbt身份运行(危险):

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

仅在下次重启之前有效。为了使设置在Ubuntu 10.04上持久化,我使用:

sudo bash -c "iptables-save > /etc/iptables.rules"
echo "#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
" > /etc/network/if-pre-up.d/iptablesload
echo "#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi
exit 0
" > /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-pre-up.d/iptablesload

(见this Ubuntu iptables wiki

答案 1 :(得分:1)

我发布的太快了。我需要做的就是覆盖jettyPort

override def jettyPort = 80

通过sbt运行sudo