|
WebApplications
sbt has movedsbt has now completely moved to GitHub. See https://github.com/harrah/xsbt/wiki. Web ApplicationsThis page describes some features of sbt that are useful for developing web applications. Basic UsageInstead of extending sbt.DefaultProject in your project definition, extend sbt.DefaultWebProject. This currently does two things. First, the package action creates a war file instead of a jar file. Second, if you add Jetty as a dependency (manually or automatically), you can use jetty-run to start your web application from sbt. Use jetty-stop to stop it. If you are running in batch mode, use the jetty action, which will wait for a key press before completing. As of sbt 0.7.0, Jetty 7 is supported in addition to Jetty 6. Configurationsbt uses the directory structure of Maven by default, so your web application files should go in src/main/webapp by default. You can override webappPath to change this. If you need to include extra files in your web application, override extraWebappFiles with a PathFinder that selects the extra files that you want (see Paths for information on PathFinder). To change the port Jetty starts up on, override jettyPort. Continuous RedeploymentYou can also continuously recompile and reload the web application when using Jetty: > jetty-run > ~ prepare-webapp jetty-run starts Jetty and the ~ prepare-webapp recompiles and recreates the web application whenever sources files change (see TriggeredExecution for details on ~). The jetty-run action monitors the directories given by scanDirectories and redeploys on changes. By default, the entire temporary web application directory is monitored. You might want to change scanDirectories in some cases. For example, set scanDirectories to Nil if you do not want to redeploy on any changes. Or, set scanDirectories to only monitor the library and classes directories: override def scanDirectories = ( temporaryWarPath / "WEB-INF" * ("classes" | "lib") ).get.toSeqYou might use one of these options if your web application picks up changes to resource files and therefore does not need to be redeployed or if you are using JRebel. Another possibility is to directly run the web application out of the the source web application path: override def jettyWebappPath = webappPath override def scanDirectories = mainCompilePath :: testCompilePath :: Nil Jetty 6 ExampleA minimal project definition for Jetty 6: class WebappBuild(info: ProjectInfo) extends DefaultWebProject(info)
{
val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test" // jetty is only need for testing
}For a runnable example, see the WebApplicationExample page, which describes running the Hello Lift example with sbt. Jetty 7 ExampleA minimal project definition for Jetty 7: class WebappBuild(info: ProjectInfo) extends DefaultWebProject(info)
{
val jetty7 = "org.eclipse.jetty" % "jetty-webapp" % "7.0.2.RC0" % "test"
}Manually Managed ExampleDownload Jetty and its dependencies and put them in a directory, say 'jetty-libs/'. Add them to jettyClasspath. For example: class WebappBuild(info: ProjectInfo) extends DefaultWebProject(info)
{
override def jettyClasspath = path("jetty-libs") * "*.jar"
}JRebelTo use JRebel, override scanDirectories as described above so that sbt does not reload Jetty on changes to your classes: override def scanDirectories = Nil Pass the following options to java in your sbt startup script: -noverify -javaagent:/path/to/jrebel/jrebel.jar | |
I needed to include jasper too, before jetty would start.
val jasper = "org.apache.tomcat" % "jasper" % "6.0.18"
This is a minimal project definition for Jetty support. Other dependencies are needed if your project project requires them. HelloLiftExample? works for me without needing this dependency, for example. If this doesn't address your comment, please feel free to follow up here or on the mailing list.
Thanks, Mark
Continuous Redeployment doesn't seem to work on classes. Changes to web.xml, e.g. do take effect.
Scala users can get a free license for JavaRebel? via http://www.zeroturnaround.com/scala-license/ and that allegedly will reload your changed classes.
You might have run into issue #35 . This is fixed in the 0.5.3-p1 snapshot1?. If it is still broken for you, please report it as a bug.
Thanks, Mark
1? Note that you can update the version of sbt used by your project by typing the following at the sbt prompt:
Not only will it allegedly reload your classes, it does. Awesome. Highly recommend JavaRebel?
I think a note needs to be added about what you "name" your jetty dependency - because calling it "jetty" clashes with the jetty definition in super.
Rename "JavaRebel?" -> "JRebel" as it changed its name.
I have ended up with such configuration for JRebel (while running cc action):
instead of:
Please, inline my config instead of this reference: "override scanDirectories as described above", because with (mainCompilePath :: testCompilePath) on scan path Jetty will be reloaded, what is not desired with JRebel.
And maybe note about cc in JRebel section too.
The minimal "Jetty 7 Example" will die if you attempt sbt jetty-run
The config needs jetty-server:
Works fine for me if I drop the original example in project/build, do 'update' and 'jetty-run'. Depending on how you are using Jetty, you might need to add additional dependencies.
It basically works, but I have a problem with JPA. I have the persistence.xml in src/main/webapp/WEB-INF/classes/META-INF but still it isn't found seemingly:
"javax.persistence.PersistenceException?: No Persistence provider for EntityManager? named MyPU"
MyPU being defined in the persistence.xml. I have also tried to put the file into src/main/resources but that didn't help either.