How to integrate Play Framework 2.0 into Gradle build management using Maven dependencies?
Asked Answered
H

4

34

Play framework 2.0 is a full-stack standalone framework for creating web applications. Probably, many people need to integrate it into their build management, nevertheless. Unfortunately, I did not find much information about his.

Here is my use case: I want to create a new project, which uses Scala and Play 2.0. I do NOT want to use sbt. I want to use Gradle, and dependency management should be done via Maven repositories.

I have only found this play module: http://www.playframework.org/modules/maven-1.0/home which supports dependency management via Maven.

I am looking for something like these examples in Grails: https://github.com/grails/grails-gradle-plugin or http://grails.org/doc/latest/guide/commandLine.html#4.5%20Ant%20and%20Maven

Of course, I could write scripts / tasks which call "play console commands". Though, I do not like this solution. Is there a better way to use Gradle / Maven for build management? If this is the only solution, then I would use Gradle, which then calls Play commands (i.e. sbt internally), right? Does this even work, or will there emerge other problems?

Hamelin answered 24/4, 2012 at 14:16 Comment(0)
H
24

This is a very tricky business. SBT in Play is used for fetching dependencies, compiling source and templates, and for the SBT incremental compilation + auto-reloading feature. I have written a build.gradle script to resolve all Play 2.0 dependencies and set-up Eclipse or IntelliJ IDEA classpaths, and made it public here.

I will try to implement the compilation later when I have time, but that would require some research. Of course, you can add compile and run tasks that just delegate to SBT but that would require describing all your project dependencies in both SBT and Gradle, which will become difficult to manage.

Edit:

I have updated the sample build.gradle file. I have added playCompile and playClean tasks that should help in a CI environment. The playCompile task does the following:

  1. Copy all user dependencies (defined in compile configuration) to lib/ folder. This works because Play will kindly pick up all jars from under lib/.
  2. Execute play compile command to compile all sources, templates and other Play framework stuff.

You can use cleanCopyPlayLibs and playClean to remove the output of the above commands, respectively.

Note that there appears to be a strange problem (bug?) on Windows, which means that even if play compile fails, gradle will tell you it succeeded.

Reply to comment:

I think you are simply missing

repositories{
  mavenCentral()
}

in your file. Check this doc out.

Heerlen answered 25/4, 2012 at 0:48 Comment(12)
Thanks for this build file. How to continue now? Create a new Play app in the same directory using "play new"? The gradle build can be added to a CI server. Though, e.g. Play tests have to be run via calling a script "play test myApp". Is this the best way to do this? I think that's an acceptable solution for the moment...Plowman
You'd have to use play new for making new apps. Then copy the gradle file into your project dir and run gradle idea/eclipse to setup your IDE. Compiling with gradle will not work yet because a lot of sources will depend on compiled Play templates and routes classes (views and routes packages), that you cannot compile with current setup. You will still have to stick with play set of commands to do this for now.Heerlen
Thanks. Great work. Now I am sure we can use Gradle and Play 2.0 in our next project as desired.Plowman
Though, if I add additional dependencies, in my case: compile 'org.mongodb:mongo-java-driver:1.3', then I get this error ##### Could not resolve all dependencies for configuration ':testRuntime'. > Could not find group:org.mongodb, module:mongo-java-driver, version:1.3. Required by: :gradlePlayApp:unspecifie ##### As I am a Gradle newbie, I think I have to read the Gradle documentation about "Configurations", "Groups", etc. Though, probably you see whats wrong instantly?Plowman
@KaiWähner missing maven central repo declaration, perhaps? see the Edit.Heerlen
Damn it, sometimes the solution is so easy! Thank you. I will read some more Gradle documentation nevertheless, so that I can add further changes by myself...Plowman
@rodion, is it possible to adapt your gradle script for Play 1.2? For java project.Indices
@Indices I am not very familiar with Play1.2, but it seems that dependencies are all in PLAY_HOME/framework/lib so you can grab them from there. copyPlayLibs task should work as-is, and other commands are probably pretty close too. Is there some particular aspect of the sample script you are not sure with?Heerlen
PLAY_HOME/framework/lib cannot be added as a repository due to its flat structure, can it? So I am not sure how my gradle build can find dependencies on play libras?Indices
Don't add it as a repo, just reference all *.jar files from the directory in your dependencies{...} block. See this: gradle.org/docs/current/userguide/…Heerlen
@Heerlen I would love an example adapting this for a multi-project buildAbuse
Also, if you are interested in adding any support directly to gradle you may want to contact the gradle folks who appear to have it on their radar github.com/gradle/gradle/blob/master/design-docs/…Abuse
R
5

Good news, as of Gradle 2.7 there is an official play plugin: https://docs.gradle.org/current/userguide/play_plugin.html

Roulette answered 19/9, 2015 at 13:50 Comment(0)
C
3

LinkedIn is currently making a Gradle plugin to support this. They are rolling this out in 3 milestones:

  1. Using Gradle to build a Play application
  2. Continuous Mode and Hot-Reload
  3. Scala interactive REPL

Milestone 1 is already complete. It's available via the Gradle nightly builds. On the above linked blog post, they encourage people to give it a try.

Concubine answered 22/6, 2015 at 3:38 Comment(0)
H
0

I designed a simple build script in Gradle for Play Framework 1.2.x which you could also use for Play 2.x

https://github.com/kirang89/play-gradle

Howes answered 2/9, 2012 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.