[Play 2.3.x Java] Getting Started Confusion for Play Java

275 views
Skip to first unread message

alotatakada

unread,
Feb 13, 2015, 8:24:46 PM2/13/15
to play-fr...@googlegroups.com

I'm wondering if you could recommend a tutorial or template for Play Java, that is up-to-date for Play 2.3.x, that demonstrates how to use 1) CoffeeScript, LESS, RJS, GZIP, Digest, JSHint, Uglify, 2) using Hibernate, JPA & PostGres, 3) Webjars and finally 4) Java 8.

Some specific things I find confusing:

1) The SBT Plugins such as sbt-rjs (https://github.com/sbt/sbt-rjs#sbt-rjs), say I need this configuration line:

lazy val root = (project in file(".")).enablePlugins(SbtWeb)

Each example app/template I've seen though has:

lazy val root = (project in file(".")).enablePlugins(PlayJava)

So should it instead be?:

lazy val root = (project in file(".")).enablePlugins(PlayJava, SbtWeb)

2) How the Pipeline configuration works. In https://www.playframework.com/documentation/2.3.x/Assets I found the following line:

pipelineStages := Seq(rjs, digest, gzip)

How would Less, CoffeeScript, JSHint, Uglify etc work within the pipeline? Do they not need to be added to the Pipeline?

3) What is the Build.scala file? I've found some Java 2.3 tutorials which reference it such as here: https://github.com/jamesward/play2torial/blob/master/JAVA.md#update-the-app-on-heroku

When I create a new Play Java App using Activator there isn't a Build.scala file in the root or project directory. I only have build.sbt in the root directory and plugins.sbt in the /project directory.

Along those same lines should I be doing something like this:

val appDependencies = Seq(
"org.webjars" % "webjars-play" % "2.0",
"org.webjars" % "bootstrap" % "2.1.1"
)

Or do Webjars now go directly into libraryDependencies like:

libraryDependencies ++= Seq(
"org.webjars" %% "webjars-play" % "2.3.0-2",
"org.webjars" % "bootstrap" % "3.1.1-2"
)

4) SBT Plugins vs WebJars? For RequireJS for example, its unclear if I should be using it as a WebJar or SBT Plugin. In the Play Documentation for Assets here https://www.playframework.com/documentation/2.3.x/Assets it says to use RequireJS as a WebJar (under the WebJars section). Here is a specific line:

For example if you declared a dependency on RequireJs then you can reference it from a view using a line like:

<script data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/requirejs/require.js")"></script>

But 4 pages later in the Public Assets documentation for "Using RequireJS" (https://www.playframework.com/documentation/2.3.x/RequireJS-support) it says to use RJS as an SBT Plugin that also has a corresponding Pipeline configuration.

Which is the recommended way?

5) Is the following line necessary within build.sbt:


scalaVersion := "2.11.1"

Within the default Play 2.3.x templates, the "play-java" template (https://github.com/playframework/playframework/blob/2.3.x/templates/play-java/build.sbt) includes that line, whereas "play-java-intro" template (https://github.com/playframework/playframework/blob/2.3.x/templates/play-java-intro/build.sbt) doesn't have the "scalaVersion" line.


6) Would you recommend including the following lines (using Java 8) in the build.sbt if I also want to use JavaJpa & hibernate? I read somewhere that Java 8 wasn't working yet with Hibernate.


javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint")

initialize := {

  val _ = initialize.value

  if (sys.props("java.specification.version") != "1.8")

    sys.error("Java 8 is required for this project.")

}

Thanks and sorry for the long list of questions!

Onezino Gabriel Moreira

unread,
Feb 15, 2015, 10:14:30 PM2/15/15
to play-fr...@googlegroups.com
I can't anwser because I using play to a API service. But I'll keep my eyes here because I wanna lern more about play.

Megazord

unread,
Feb 18, 2015, 11:21:10 AM2/18/15
to play-fr...@googlegroups.com
1) The SbtWeb plugin is enabled by Play(Java|Scala) SBT plugins. You don't need to enable both. That is why the activator templates are explicitly enabling SbtWeb. I think this is documented somewhere, but I can't find it right now.

2) Each sbt-web plugin has its own configurations. Per instance, you can see how to configure sbt-less here:

https://github.com/sbt/sbt-less

The pipeline defines the order that assets will be processed **after** they were compiled. In your example:

1. First it will run RequireJs optimizer
2. Then it will run the digest
3. Then it will run the compression

Of course, each of these plugins can be configured as documented in their repositories. See a (very) complete list of sbt-web plugins here:

https://github.com/sbt/sbt-web#available-plugins

3) SBT supports both build.sbt and project/Build.scala to configure your project. That is what sbt docs says:

http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Full-Def.html#when-to-use-scala-files

When to use .scala files:
In .scala files, you can write any Scala code, including top-level classes and objects. Also, there are no restrictions on blank lines, since they are standard .scala files.
The recommended approach is to define most configuration in .sbt files, using .scala files for task implementations or to share values, such as keys, across .sbt files.

I'm not sure about that, but I think build.sbt was introduced later in SBT, then some tutorials refer to project/Build.scala instead of build.sbt.

4) I'm not a WebJars user, but as far as I know, it is only a way to package your client side dependencies and they should work fine with sbt-web pipeline. Maybe using RequireJs as an example is misleading, since RequireJs is packaged with Play. But it is just that. You can use both.

5) Not sure if it is necessary, but I think it is a good practice to know exactly what scala version (including minor) are you using. SBT probably has its own default.

6) If your Hibernate version does not support Java 8 yet, I suggest that you keep these lines commented until a compatible version is released.

HTH
Reply all
Reply to author
Forward
0 new messages