Navigation Menu

Skip to content

Commit

Permalink
Removal of play.Project in order to simplify project declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
huntc committed Apr 11, 2014
1 parent eac3e28 commit 885ffb9
Show file tree
Hide file tree
Showing 37 changed files with 358 additions and 417 deletions.
44 changes: 38 additions & 6 deletions documentation/manual/Migration23.md
Expand Up @@ -25,7 +25,7 @@ lazy val root = (project in file(".")).addPlugins(SbtWeb)

The above example shows `SbtWeb` being added to the root project of a build. In the case of `SbtWeb` there are other plugins that become enabled if it is e.g. if you also had added the `sbt-less-plugin` via `addSbtPlugin` then it will become enabled just because `SbtWeb` has been enabled. `SbtWeb` is thus a "root" plugin for that category of plugins.

Play itself is now added using the auto plugin mechanism. The mechanism used in Play 2.2 where `playJavaSettings` and `playScalaSettings` were used has been deprecated. You should now move toward one of the following:
Play itself is now added using the auto plugin mechanism. The mechanism used in Play 2.2 where `playJavaSettings` and `playScalaSettings` were used has been removed. You now use one of the following instead:

```java
lazy val root = (project in file(".")).addPlugins(PlayJava)
Expand All @@ -37,29 +37,61 @@ or
lazy val root = (project in file(".")).addPlugins(PlayScala)
```

> *It is important to note that using the new method of `addPlugins` with `PlayJava` or `PlayScala` is incompatible with `playJavaSettings`, `playScalaSettings` or the use of `play.Project`. While the latter methods are deprecated they may still be used for the time being. However we strongly encourage you to adopt the `addPlugins` with `PlayJava` or `PlayScala` method.*
If you were previously using play.Project, for example a Scala project:

```scala
object ApplicationBuild extends Build {

val appName = "myproject"
val appVersion = "1.0-SNAPSHOT"

val appDependencies = Seq()

val main = play.Project(appName, appVersion, appDependencies).settings(
)

}
```

...then you can continue to use a similar approach via native sbt:

```scala
object ApplicationBuild extends Build {

val appName = "myproject"
val appVersion = "1.0-SNAPSHOT"

val appDependencies = Seq()

val main = Project(appName, file(".")).addPlugins(play.PlayScala).settings(
version := appVersion,
libraryDependencies ++= appDependencies
)

}
```

By moving to the above style settings are now automatically imported when a plugin is enabled.

The keys provided by Play must now also be referenced within the `PlayKeys` object. For example to reference `jdbc` or `anorm` you must do so either by importing:
The keys provided by Play must now also be referenced within the `PlayKeys` object. For example to reference `playVersion` you must do so either by importing:

```scala
import PlayKeys._
```

or qualifying them as `PlayKeys.jdbc` or `PlayKeys.anorm`.
or qualifying it with `PlayKeys.playVersion`.

Outside of using a `.sbt` file i.e. if you're using Scala to describe your build then you may do the following to have `PlayKeys` within scope:

```scala
import play.PlayJava.autoImport._
import play.Play.autoImport._
import PlayKeys._
```

or

```scala
import play.PlayScala.autoImport._
import play.Play.autoImport._
import PlayKeys._
```

Expand Down
18 changes: 6 additions & 12 deletions documentation/manual/detailedTopics/build/Build.md
Expand Up @@ -5,7 +5,7 @@ The Play build system uses [sbt](http://www.scala-sbt.org/), a non-intrusive bui

## Play application directory structure

Most people get started with Play using the `play new foo` command which produces a directory structure like this:
Most people get started with Play using the `activator new foo` command which produces a directory structure like this:

- `/`: The root folder of your application
- `/README`: A text file describing your applicaiont that will get deployed with it.
Expand All @@ -20,11 +20,9 @@ For now, we are going to concern ourselves with the `/build.sbt` file and the `/

## The `/build.sbt` file.

When you use the `play new foo` command, the build description file, `/build.sbt`, will be generated like this:
When you use the `activator new foo` command, the build description file, `/build.sbt`, will be generated like this:

```scala
import play.Project._

name := "foo"

version := "1.0-SNAPSHOT"
Expand All @@ -35,18 +33,16 @@ libraryDependencies ++= Seq(
cache
)

play.Project.playScalaSettings
lazy val root = (project in file(".")).addPlugins(PlayScala)
```

The `name` line defines the name of your application and it will be the same as the name of your application's root directory, `/`, which is derived from the argument that you gave to the `play new` command.
The `name` line defines the name of your application and it will be the same as the name of your application's root directory, `/`, which is derived from the argument that you gave to the `activator new` command.

The `version` line provides the version of your application which is used as part of the name for the artifacts your build will produce.

The `libraryDependencies` line specifies the libraries that your application depends on. More on this below.

You should use `play.Project.playScalaSettings` or `play.Project.playJavaSettings` to configure sbt for Scala or Java respectively.

> Every sbt feature is available to either a Scala or a Play project.
You should use the `PlayJava` or `PlayScala` plugin to configure sbt for Java or Scala respectively.

## The `/project` directory

Expand All @@ -69,15 +65,13 @@ addSbtPlugin("com.typesafe.play" % "sbt-plugin" % playVersion) // where version
Adding dependencies is simple as the build file for the `zentasks` Java sample shows:

```scala
import play.Project._

name := "zentask"

version := "1.0"

libraryDependencies ++= Seq(javaJdbc, javaEbean)

play.Project.playJavaSettings
lazy val root = (project in file(".")).addPlugins(PlayJava)
```

...and so are resolvers for adding in additional repositories:
Expand Down
6 changes: 3 additions & 3 deletions documentation/manual/detailedTopics/build/SBTSettings.md
Expand Up @@ -13,14 +13,14 @@ confDirectory := "myConfFolder"

## Default settings for Java applications

Play defines a default set of settings suitable for Java-based applications. To enable them add the `play.Project.playJavaSettings` set of settings to your `build.sbt` file. These settings mostly define the default imports for generated templates e.g. importing `java.lang.*` so types like `Long` are the Java ones by default instead of the Scala ones. `play.Project.playJavaSettings` also imports `java.util.*` so that the default collection library will be the Java one.
Play defines a default set of settings suitable for Java-based applications. To enable them add the `PlayJava` plugin via your project's addPlugins method. These settings mostly define the default imports for generated templates e.g. importing `java.lang.*` so types like `Long` are the Java ones by default instead of the Scala ones. `play.Project.playJavaSettings` also imports `java.util.*` so that the default collection library will be the Java one.

## Default settings for Scala applications

Play defines a default set of settings suitable for Scala-based applications. To enable them add the `play.Project.playScalaSettings` set of settings to your `build.sbt` file. These default settings define the default imports for generated templates (such as internationalized messages, and core APIs).
Play defines a default set of settings suitable for Scala-based applications. To enable them add the `PlayScala` plugin via your project's addPlugins method. These default settings define the default imports for generated templates (such as internationalized messages, and core APIs).

## Play project settings with their default value

When you define your sbt project using the default settings explained above, use sbt's `settings` command via the play console for your project (the console is obtained by invoking the `play` command from the command line). You can then further show the value of a setting by using the sbt `show` command e.g. `show name` will output the project's name.
When you define your sbt project using the default settings explained above, use sbt's `settings` command via the play console for your project (the console is obtained by invoking the `activator` command from the command line). You can then further show the value of a setting by using the sbt `show` command e.g. `show name` will output the project's name.

> **Next:** [[Managing library dependencies | SBTDependencies]]
19 changes: 6 additions & 13 deletions documentation/manual/detailedTopics/build/SBTSubProjects.md
Expand Up @@ -10,15 +10,12 @@ It will be helpful to read the [SBT documentation on multi-project builds](http:
You can make your application depend on a simple library project. Just add another sbt project definition in your `build.sbt` file:

```
import play.Project._
name := "my-first-application"
version := "1.0"
playScalaSettings
lazy val myFirstApplication = project.in(file("."))
lazy val myFirstApplication = (project in file("."))
.addPlugins(PlayScala)
.aggregate(myLibrary)
.dependsOn(myLibrary)
Expand Down Expand Up @@ -98,7 +95,7 @@ libraryDependencies += fooDependency

## Splitting your web application into several parts

As a Play application is just a standard sbt project with a default configuration, it can depend on another Play application. You can make any sub module a Play application by including `playScalaSettings` or `playJavaSettings`, depending on whether your project is a Java or Scala project, in its corresponding `build.sbt` file.
As a Play application is just a standard sbt project with a default configuration, it can depend on another Play application. You can make any sub module a Play application by adding the `PlayJava` or `PlayScala` plugins, depending on whether your project is a Java or Scala project, in its corresponding `build.sbt` file.

> **Note:** In order to avoid naming collision, make sure your controllers, including the Assets controller in your subprojects are using a different name space than the main project
Expand All @@ -113,21 +110,17 @@ It's also possible to split the route file into smaller pieces. This is a very h
```scala
name := "myproject"

playScalaSettings
lazy val admin = (project in file("modules/admin")).addPlugins(PlayScala)

lazy val admin = project.in(file("modules/admin"))

lazy val main = project.in(file("."))
.dependsOn(admin).aggregate(admin)
lazy val main = (project in file("."))
.addPlugins(PlayScala).dependsOn(admin).aggregate(admin)
```

`modules/admin/build.sbt`

```scala
name := "myadmin"

playScalaSettings

libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.18",
jdbc,
Expand Down
3 changes: 2 additions & 1 deletion documentation/project/Build.scala
Expand Up @@ -6,7 +6,8 @@ import play.sbtplugin.Colors
import play.core.server.ServerWithStop
import sbt._
import sbt.Keys._
import play.Keys._
import play.Play.autoImport._
import PlayKeys._
import play.core.{ BuildDocHandler, BuildLink, PlayVersion }
import play.PlaySourceGenerators._
import DocValidation._
Expand Down
Expand Up @@ -4,8 +4,9 @@
package play

import sbt._
import sbt.Keys._
import Keys._
import play.PlayImport._
import PlayKeys._
import PlayExceptions._

// ----- Assets
Expand Down
34 changes: 18 additions & 16 deletions framework/src/sbt-plugin/src/main/scala/PlayCommands.scala
Expand Up @@ -3,8 +3,10 @@
*/
package play

import sbt.{ Project => SbtProject, Settings => SbtSettings, _ }
import sbt._
import sbt.Keys._
import play.PlayImport._
import PlayKeys._

import com.typesafe.sbt.web.SbtWeb.autoImport._

Expand Down Expand Up @@ -58,7 +60,7 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal
SbtIdeaPlugin.settings ++ Seq(
commands += Command("idea")(_ => args) { (state, args) =>
// Firstly, attempt to compile the project, but ignore the result
SbtProject.runTask(compile in Compile, state)
Project.runTask(compile in Compile, state)

SbtIdeaPlugin.doCommand(state, if (!args.contains(WithSources) && !(args.contains(NoSources) || args.contains(NoClassifiers))) {
args :+ NoClassifiers
Expand Down Expand Up @@ -217,7 +219,7 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal

val playPrompt = { state: State =>

val extracted = SbtProject.extract(state)
val extracted = Project.extract(state)
import extracted._

(name in currentRef get structure.data).map { name =>
Expand All @@ -242,14 +244,14 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal
}

// -- Utility methods for 0.10-> 0.11 migration
def inAllDeps[T](base: ProjectRef, deps: ProjectRef => Seq[ProjectRef], key: SettingKey[T], data: SbtSettings[Scope]): Seq[T] =
def inAllDeps[T](base: ProjectRef, deps: ProjectRef => Seq[ProjectRef], key: SettingKey[T], data: Settings[Scope]): Seq[T] =
inAllProjects(Dag.topologicalSort(base)(deps), key, data)
def inAllProjects[T](allProjects: Seq[Reference], key: SettingKey[T], data: SbtSettings[Scope]): Seq[T] =
def inAllProjects[T](allProjects: Seq[Reference], key: SettingKey[T], data: Settings[Scope]): Seq[T] =
allProjects.flatMap { p => key in p get data }

def inAllDependencies[T](base: ProjectRef, key: SettingKey[T], structure: Load.BuildStructure): Seq[T] = {
def deps(ref: ProjectRef): Seq[ProjectRef] =
SbtProject.getProject(ref, structure).toList.flatMap { p =>
Project.getProject(ref, structure).toList.flatMap { p =>
p.dependencies.map(_.project) ++ p.aggregate
}
inAllDeps(base, deps, key, structure.data)
Expand All @@ -272,7 +274,7 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal
}

val playCompileEverythingTask = (state, thisProjectRef) flatMap { (s, r) =>
inAllDependencies(r, playAssetsWithCompilation.task, SbtProject structure s).join
inAllDependencies(r, playAssetsWithCompilation.task, Project structure s).join
}

val buildRequireTask = (copyResources in Compile, crossTarget, requireJs, requireJsFolder, requireJsShim, requireNativePath, streams) map { (cr, crossTarget, requireJs, requireJsFolder, requireJsShim, requireNativePath, s) =>
Expand Down Expand Up @@ -317,7 +319,7 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal

val h2Command = Command.command("h2-browser") { state: State =>
try {
val commonLoader = SbtProject.runTask(playCommonClassloader, state).get._2.toEither.right.get
val commonLoader = Project.runTask(playCommonClassloader, state).get._2.toEither.right.get
val h2ServerClass = commonLoader.loadClass(classOf[org.h2.tools.Server].getName)
h2ServerClass.getMethod("main", classOf[Array[String]]).invoke(null, Array.empty[String])
} catch {
Expand Down Expand Up @@ -350,9 +352,9 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal

val classpathCommand = Command.command("classpath") { state: State =>

val extracted = SbtProject.extract(state)
val extracted = Project.extract(state)

SbtProject.runTask(dependencyClasspath in Runtime, state).get._2.toEither match {
Project.runTask(dependencyClasspath in Runtime, state).get._2.toEither match {
case Left(_) => {
println()
println("Cannot compute the classpath")
Expand All @@ -375,10 +377,10 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal

val playMonitoredFiles = TaskKey[Seq[String]]("play-monitored-files")
val playMonitoredFilesTask = (thisProjectRef, state) map { (ref, state) =>
val src = inAllDependencies(ref, sourceDirectories in Compile, SbtProject structure state).foldLeft(Seq.empty[File])(_ ++ _)
val resources = inAllDependencies(ref, resourceDirectories in Compile, SbtProject structure state).foldLeft(Seq.empty[File])(_ ++ _)
val assets = inAllDependencies(ref, sourceDirectories in Assets, SbtProject structure state).foldLeft(Seq.empty[File])(_ ++ _)
val public = inAllDependencies(ref, resourceDirectories in Assets, SbtProject structure state).foldLeft(Seq.empty[File])(_ ++ _)
val src = inAllDependencies(ref, sourceDirectories in Compile, Project structure state).foldLeft(Seq.empty[File])(_ ++ _)
val resources = inAllDependencies(ref, resourceDirectories in Compile, Project structure state).foldLeft(Seq.empty[File])(_ ++ _)
val assets = inAllDependencies(ref, sourceDirectories in Assets, Project structure state).foldLeft(Seq.empty[File])(_ ++ _)
val public = inAllDependencies(ref, resourceDirectories in Assets, Project structure state).foldLeft(Seq.empty[File])(_ ++ _)
(src ++ resources ++ assets ++ public).map { f =>
if (!f.exists) f.mkdirs(); f
}.map(_.getCanonicalPath).distinct
Expand Down Expand Up @@ -417,9 +419,9 @@ trait PlayCommands extends PlayAssetsCompiler with PlayEclipse with PlayInternal

val computeDependenciesCommand = Command.command("dependencies") { state: State =>

val extracted = SbtProject.extract(state)
val extracted = Project.extract(state)

SbtProject.runTask(computeDependencies, state).get._2.toEither match {
Project.runTask(computeDependencies, state).get._2.toEither match {
case Left(_) => {
println()
println("Cannot compute dependencies")
Expand Down

0 comments on commit 885ffb9

Please sign in to comment.