sbt创建web项目

###创建目录 mkdir sbtdemo cd sbtdemo scipio@localhost [04:31:59] [~/sbtdemo]

###sbt设置项目信息 -> % sbt [info] Set current project to sbtdemo (in build file:/Users/scipio/sbtdemo/)

set name := "sbt demo" [info] Defining *:name [info] The new value will be used by *:description, *:normalizedName and 6 others. [info] Run last for details. [info] Reapplying settings... [info] Set current project to sbt demo (in build file:/Users/scipio/sbtdemo/) session save [info] Reapplying settings... [info] Set current project to sbt demo (in build file:/Users/scipio/sbtdemo/) exit

会自动创建项目project目录以及build.sbt 注意build.sbt里头要用空行分隔

###添加eclipse支持 在project目录下创建plugins.sbt文件(如果不存在的话), 然后添加 addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0") sbt eclipse Import.... Navigate to General | Existing Projects into Workspace

###添加idea支持 addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % “1.5.2") 然后 sbt gen-idea

  • 尝鲜版sbt-idea resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/ repositories/snapshots/"

    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0-SNAPSHOT”)

###添加web-plugin,以运行在servlet容器上 addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.4.2")

  • 同时在build.sbt里头添加 seq(webSettings :_*)
    libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.22" % "container"
    libraryDependencies += "javax.servlet" % "servlet-api" % "2.5" % “provided"

  • 完整如下: name := "SampleProject"

    organization := "com.samples" version := "1.0" scalaVersion := "2.10.3" seq(webSettings :_*) libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.22" % "container" libraryDependencies += "javax.servlet" % "servlet-api" % "2.5" % "provided"

###新建servlet

import javax.servlet.http.HttpServlet
import scala.xml.NodeSeq
import javax.servlet.http._
class SimpleServlet extends HttpServlet{
  override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
    resp.setContentType("text/html")
    resp.setCharacterEncoding("UTF-8")
    val responseBody: NodeSeq =
      <html><body><h1>hello world from scala!</h1></body></html>
    resp.getWriter.write(responseBody.toString)
  }
}

###在src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>simpleservlet</servlet-name>
        <servlet-class>SimpleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>simpleservlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

###然后执行

sbt container:start

http://localhost:8080

###打包 sbt package,打包成war

###包括依赖打成一个jar包

  • 在app/project目录下新建assembly.sbt
  • 然后添加 addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
  • 然后在app目录下添加assembly.sbt import AssemblyKeys._ assemblySettings

###添加格式化插件 addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0") 每当你运行compile或者test:compile的时候,都会自动格式化

###添加http client封装 在build.sbt里头添加

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % “0.11.0"

scala mock
libraryDependencies +="org.scalamock" %% "scalamock-scalatest-support"
   % "3.0.1" % “test"



specs2
libraryDependencies +=
   "org.scalamock" %% "scalamock-specs2-support" % "3.0.1" % "test"

jetty7
libraryDependencies ++= Seq(
"org.eclipse.jetty" % "jetty-servlet" % "7.3.0.v20110203" % "container", "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "test,

container",
"org.eclipse.jetty" % "jetty-server" % "7.3.0.v20110203" % "container"

)
seq(com.github.siasia.WebPlugin.webSettings :_*)

转载于:https://my.oschina.net/scipio/blog/548070

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值