How to use dependencies and modules in play-framework 2.0

1,819 views
Skip to first unread message

ses

unread,
Jan 30, 2012, 7:27:02 AM1/30/12
to play-framework
Currently in playframework 1.2.4 there is a such thing like module and
project, if a project depends on a module, user should put appropriate
line in configuration, into dependecies.yml file.

Now, as I can see, reading exiting documentation, there is an idea if
subprojects //play/documentation/manual/build/SBTSubProjects.md
instead of modules??

Taking into consideration that now there is no 'dependecies.yml' file
anymore, I would say that idea of suprojects is going to substitute
the modules?

Then, what I do:

cd projects
play new newProject
mkdir modules
cd modules
play new project1
play new project2
play new common
vi /projects/project/Build.scala

editing it like this:

object ApplicationBuild extends Build {

val appName = "newProject"
val appVersion = "1.2"

val common = PlayProject(
appName + "-common", appVersion, path = file("modules/common")
)

val project1 = PlayProject(
appName + "-project1", appVersion, path = file("modules/project1")
).dependsOn(common)

val project2 = PlayProject(
appName + "-project2", appVersion, path = file("modules/project2")
).dependsOn(common)

val main = PlayProject(
appName, appVersion
).dependsOn(
project1, project2
)
}
But, then ... if I try to run 'project1': cd /projects/module/project1
play run

It would know nothing about the dependency to 'common' project for
'project1' (i guess), because I did not modify project1/project/
Build.scala ..

So, what I should do with that? How 'project1' would know about its
dependencies in run-time?

Manuel Bernhardt

unread,
Jan 31, 2012, 3:33:20 AM1/31/12
to play-fr...@googlegroups.com
Hi,

the modules in Play 2 are SBT projects. So they at the very least need
to have a structure like

src/
java

and contain a SBT build definition. If you just want to do things such
as declaring dependencies you can use a simple build.sbt definition at
the root of your module that looks like e.g.

----
name := "my-module"

organization := "fooBar"

resolvers += "someRepo" at "http://maven.fooBar.org/maven2"

libraryDependencies ++= Seq(
"commons-collections" % "commons-collections" % "3.2.1",
"com.foo" % "bar" % "1.0")
----

(Note that all statements are separated by an empty lines)

Most likely tough you want to have a Play project scaffold there so
instead of the above just do

cd modules
play new my-module

and link that new project from the main build definition.

Then, in order to run it, run play in the main project and switch to
the sub-project:

play2
projects --> lists all projects
project my-module (you are in the project)

If you want to understand more about SBT, a good place to start is

https://github.com/harrah/xsbt/wiki/Getting-Started-Welcome

Manuel

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

ses

unread,
Feb 1, 2012, 11:32:05 AM2/1/12
to play-framework
Ok. when I did it. And switched to my module in the play-shell.

I type there: run

and see: Compilation error: not found: value routes in the browser
when go by localhost:9000

it marked red this line: <link rel="stylesheet" media="screen"
href="@routes.Assets.at("stylesheets/main.css")"> on the page.

my sub-project is just plain java application...

On Jan 31, 10:33 am, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:

Manuel Bernhardt

unread,
Feb 1, 2012, 12:07:13 PM2/1/12
to play-fr...@googlegroups.com
Ok, that looks like a bug then I'd think

Guillaume Bort

unread,
Feb 1, 2012, 12:24:23 PM2/1/12
to play-fr...@googlegroups.com
I don't exactly understand your problem, but anyway for now it is not
possible to defines router in sub projects. It's a bit more
complicated now that routes are compiled.

--
Guillaume Bort

Manuel Bernhardt

unread,
Feb 1, 2012, 12:38:38 PM2/1/12
to play-fr...@googlegroups.com
On Wed, Feb 1, 2012 at 6:24 PM, Guillaume Bort <guillau...@gmail.com> wrote:
> I don't exactly understand your problem, but anyway for now it is not
> possible to defines router in sub projects. It's a bit more
> complicated now that routes are compiled.

I think the question was if there was a way to run sub-projects (aka
modules) stand-alone, in the play console.

Regarding routes in sub-projects: do you plan on adding some kind of
support for these in a later release?

Thanks,

Manuel

Guillaume Bort

unread,
Feb 1, 2012, 2:54:32 PM2/1/12
to play-fr...@googlegroups.com
> Regarding routes in sub-projects: do you plan on adding some kind of
> support for these in a later release?

Yes.

On Wed, Feb 1, 2012 at 6:38 PM, Manuel Bernhardt

Ben McCann

unread,
Feb 2, 2012, 9:52:33 PM2/2/12
to play-fr...@googlegroups.com
Is there a way to do an action on server start form a module in Play 2?  Or do you have to make the call in the main project's global settings?
Also are there any docs regarding how to make a Play 2 module?

Thanks,
Ben

Pekka Mattila

unread,
Feb 16, 2012, 11:19:46 AM2/16/12
to play-fr...@googlegroups.com
Hi,

I am using Play RC2.

  val website1 = PlayProject(
    "Website", appVersion, path = file("website1"), mainLang = SCALA
  ).dependsOn(core)

  val website2 = PlayProject(
    "Website2", appVersion, path = file("website2"), mainLang = SCALA
  ).dependsOn(core)

  val main = PlayProject(
    "Base", appVersion
  ).dependsOn(website1, website2)

website 1 routes file is:

GET     /website1                           controllers.Application.index
GET     /website1/assets/*file               controllers.Assets.at(path="/public", file)

website 2 routes file is:

GET     /website2                           controller.Application.index
GET     /website2/assets/*file               controllers.Assets.at(path="/public", file)


Then I start DEV web server like this:
$ cd Base
$ run

Website1 routes file is ONLY loaded.


Then when I start DEV web server like this:
$cd Website1
$ run

Website 1 routes file is ONLY loaded.


Then when I start DEV web server like this:
$ cd Website2
$ run

Website 2 routes file is ONLY loaded.

How can I start all sub projects at once?

Best Regards,
Pekka

Guillaume Bort

unread,
Feb 16, 2012, 11:56:51 AM2/16/12
to play-fr...@googlegroups.com
As I said several times here, it is not yet possible to compose routers.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/d67XWpVqM5UJ.

Ike

unread,
Feb 16, 2012, 4:14:48 PM2/16/12
to play-fr...@googlegroups.com
Are you planning to make it possible for 2.0? Next point release?

In the meantime do you just have to create all your routes in the master project and then your controllers, templates, etc. can still live in sub-projects?

Guillaume Bort

unread,
Feb 16, 2012, 4:39:54 PM2/16/12
to play-fr...@googlegroups.com
No it's too late for 2.0. And it is really complicated to achieve
since everything is now compiled. You can put controllers and
templates in subprojects, but the problem is that they don't have
access to the reverse routers.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/_SA0fnM_KNoJ.

Ben McCann

unread,
Feb 16, 2012, 4:52:26 PM2/16/12
to play-fr...@googlegroups.com
Just an idea in case it helps...  Couldn't you compile the reverse routers for the subproject and put them in the subproject jar?  You would probably want something on startup that found all the reverse routers and registered them with the server and made sure there were no duplicates.

Pekka Mattila

unread,
Feb 17, 2012, 12:10:24 AM2/17/12
to play-fr...@googlegroups.com
Guillaume, I have checked email threads here regarding route composition, source code and your issue tracker. But I really don't understand the requirement for "composing routers". Sounds complicated and something that is not high priority at least for me. I agree with Ben without knowing the idea behind composing the routers.

Here is what I'd love to do with Play:
- define project CORE that is normal SBT Project (yeah, like PlayProject is if I saw correctly from SBT plugin's source code)
- define project A that has its own routers file (depends on CORE)
- define project B that has its own routers file (depends on CORE)
- projects A and B can be started with a single play SBT command (they could live in a different port or in a different context. Currently we have project A in context /a and B in /b (we are using Jetty without Play). So the REST API of project A  can be found from http:://localhost/a/<url> and so on.
- I can start only project A
- I can start only project B
- project A can be packaged to a single Jar so that I can deploy it to server A (this is really important)
- project B can be packaged to a single Jar so that I can deploy it to server B (this is really important)

So far I don't know why project A must see project B's routes? Perhaps it is useful if you want to share static assets or something like that.

I could almost do this by defining separate Play projects A and B but then I'd need to manage them in separate SBT projects and I am not a big fan of that.

I just need to ask these questions, since I am new to Play Framework and I don't know the vision of Play.

Any thoughts? 

- Pekka

Илья Скорик

unread,
Feb 17, 2012, 3:32:16 AM2/17/12
to play-fr...@googlegroups.com
I like variant with different contexts and assets and routes.

But it is not clear, how configure this, that would run all
subprojects from a single developer console. Is that possible?

2012/2/17 Pekka Mattila <pekka....@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/UhwOz-TzSZMJ.


>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
С уважением, Илья Скорик
Yours faithfully, Ilya Skorik

Guillaume Bort

unread,
Feb 17, 2012, 4:51:50 AM2/17/12
to play-fr...@googlegroups.com
Ok so this scenario should just work right now. Don't use the `play
new` command to create projects however. Create a layout like:

/
-- /projectA
-- /projectB
-- /common
-- /project
-- plugins.sbt
-- Build.scala

And create the sbt build file in Build.scala, projectA and projectB
being PlayProject, and common an simple Project. Make both A and B
depends of common.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/UhwOz-TzSZMJ.

Илья Скорик

unread,
Feb 17, 2012, 5:18:54 AM2/17/12
to play-fr...@googlegroups.com
Can not make this configuration. I would be grateful for a working example.

2012/2/17 Guillaume Bort <guillau...@gmail.com>:

--

Guillaume Bort

unread,
Feb 17, 2012, 5:21:43 AM2/17/12
to play-fr...@googlegroups.com
Here is my build file:

import sbt._
import Keys._
import PlayProject._

object ApplicationBuild extends Build {

val common = Project(id = "common", base = file("common"))

val projectA = PlayProject("projectA", "1.0-SNAPSHOT", path =
file("projectA"), mainLang = SCALA).dependsOn(common)

val projectB = PlayProject("projectB", "1.0-SNAPSHOT", path =
file("projectB"), mainLang = SCALA).dependsOn(common)

}

2012/2/17 Илья Скорик <il...@skorik.me>:

--
Guillaume Bort

Илья Скорик

unread,
Feb 17, 2012, 5:27:57 AM2/17/12
to play-fr...@googlegroups.com
I probably do not understand something important. How do I start this
project in the play console?

17 февраля 2012 г. 14:21 пользователь Guillaume Bort
<guillau...@gmail.com> написал:

Julien Richard-Foy

unread,
Feb 17, 2012, 5:31:38 AM2/17/12
to play-fr...@googlegroups.com
In the root directory (of the layout suggested by Guillaume), just run
the play command.

Илья Скорик

unread,
Feb 17, 2012, 5:36:16 AM2/17/12
to play-fr...@googlegroups.com
Hmm, i have:

test1/ -- root folder

test1/project -- folder with configs:
test1/project/Build.scala
test1/project/plugins.sbt

test1/projectA -- play project
test1/projectB -- play project

When I type "play" in "test1" folder, i have "This is not a play
application!" error.

2012/2/17 Julien Richard-Foy <j...@zenexity.com>:


> In the root directory (of the layout suggested by Guillaume), just run
> the play command.
>

Guillaume Bort

unread,
Feb 17, 2012, 7:31:27 AM2/17/12
to play-fr...@googlegroups.com
Yes of course this is not a play application anymore. This is a custom
sbt project with 2 applications and one commun project. A valid play
project requires at least a root play application.

But you can use sbt directly instead of play.

2012/2/17 Илья Скорик <il...@skorik.me>:

--
Guillaume Bort

Илья Скорик

unread,
Feb 17, 2012, 7:37:08 AM2/17/12
to play-fr...@googlegroups.com
How to work with this configuration? I want to run the application
with modules in console, edit code of modules in the Code Editor, see
the errors in the console, and so on, as if I'm working with a Play
application. Is that possible?

17 февраля 2012 г. 16:31 пользователь Guillaume Bort
<guillau...@gmail.com> написал:

Guillaume Bort

unread,
Feb 17, 2012, 7:42:22 AM2/17/12
to play-fr...@googlegroups.com
Using sbt or play command is the same. The play script is just a
wrapper on top of sbt.

2012/2/17 Илья Скорик <il...@skorik.me>:

Илья Скорик

unread,
Feb 17, 2012, 8:43:55 AM2/17/12
to play-fr...@googlegroups.com
It is not easy for a beginner. At the moment I got to start sbt and
getting error:

play#sbt-plugin;2.0-RC3-SNAPSHOT: not found

17 февраля 2012 г. 16:42 пользователь Guillaume Bort

Pekka Mattila

unread,
Feb 17, 2012, 10:07:49 AM2/17/12
to play-fr...@googlegroups.com
With this example. How can I define routes to projectA and projectB? Can I build two different jars? Can I start both projects at the same time?

Best Regards,
Pekka

Pekka Mattila

unread,
Feb 17, 2012, 10:09:37 AM2/17/12
to play-fr...@googlegroups.com
sbt-plugin;2.0-RC3-SNAPSHOT is not published to the TypeSafe repo. Use 2.0-RC2 instead of the RC3-SNAPSHOT (Or follow the hard way and publish it to your own local repository... I don't recommend this).
Reply all
Reply to author
Forward
0 new messages