Search code examples
scalatra

Scalatra without SBT


I'm trying to get started with Scalatra, but I'm running into major issues with SBT. To put it plainly, I hate it. It's the Scala equivalent of Maven. It tries to take over every single aspect of your project for a few supposed benefits. Plus, it doesn't integrate with IDEs very well. So is there a way to work with Scalatra without SBT? I really like what Scalatra has to offer, but if I can't get rid of SBT, I'm probably going to have to settle with something more bare bones, like building the features I need with Jetty.


Solution

  • I should have been a bit more clear. But after several hours last night, I found the answer. First, you'll need a main object like this (or similar):

    object Sandbox {
        def main(args: Array[String]) {
            val server = new Server(3000)
            val context = new WebAppContext()
            context.setServer(server)
            context.setContextPath("/")
            context.setWar("webapp")
            server.setHandler(context)
    
            try {
                server.start()
                server.join()
            } catch {
                case e: Exception =>
                    e.printStackTrace()
                    System.exit(-1)
            }
        }
    }
    

    Then you'll need a servlet:

    class Test extends ScalatraServlet with ScalateSupport {
        get("/") {
            <html>
                <body>
                    Hello World
                </body>
            </html>
        }
    }
    

    You'll also need a ScalatraBootstrap class in your default package (must be named ScalatraBootstrap.scala:

    class ScalatraBootstrap extends LifeCycle {
        override def init(context: ServletContext) {
            context.mount(new Test, "/test/*")
        }
    }
    

    Almost done. You will need a single XML file. Create a webapp/WEB-INF folder and put this web.xml file in there.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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_3_0.xsd"
             version="3.0">
        <listener>
            <listener-class>org.scalatra.servlet.ScalatraListener</listener-class>
        </listener>
    
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/img/*</url-pattern>
            <url-pattern>/css/*</url-pattern>
            <url-pattern>/js/*</url-pattern>
            <url-pattern>/assets/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

    Finally, you'll need the JAR files. There's a pretty long list, but there's a fairly simple way to get them. I hate Maven, but I love the dependency management. So create a test folder somewhere and download the pom.xml file in this repo. Then go to the folder where the pom.xml is and run mvn dependency:copy-dependencies. Grab the JARs from the dependencies folder and you're good to go. No Maven, no SBT, no magic. Run and debug in your IDE like you always do. :)