When I tried to touch Play Framework for the first time in a few years, there was a change and I wanted to use Gradle, so I forgot to start the project using Gradle. I will keep it as a record. Play Framework is a full-stack web application framework that can be developed using Java or Scala. This time, I am writing assuming a project in Java that is relatively easy to enter.
--Install the tools needed to use the Play Framework --Start the Play Framework development server using Gradle
Java
If you check the official required environment information (https://www.playframework.com/documentation/2.8.x/Requirements), it says Java SE 1.8 or higher
, so the latest Java version. Install the version.
brew cask install java
sbt
A build tool used to create applications using Java or Scala.
There is a description of sbt
in the official required environment information (https://www.playframework.com/documentation/2.8.x/Requirements), so install it as well.
brew install sbt
Gradle It is a build automation system that mainly targets Java and Scala, and can support various frameworks and JVM languages by using plugins. If you look at the official plugin information (https://plugins.gradle.org/), you will find plugins such as kotlin and Spring boot. Regarding Play Framework, it seems that there was support on the main body side up to Gradle 5, but from Gradle 6 it has been changed to support with a plug-in. (Https://docs.gradle.org/current/userguide/play_plugin.html)
brew install gradle
Before you start this, check out the Gradle Play Plugin Official. The supported Play Framework versions are listed.
Actually, at the moment (* 2019/12), only Play Framework 2.6 series is supported.
So, this time I aim to create it with 2.6.25
which is the final version of 2.6 series.
If you check Play Framework official new project creation explanation, there are two types of descriptions, Java template
and Scala template
. ..
This time, I will use the Java template
because I am aiming for development in Java.
…… But if you execute the described command as it is, the latest template will be downloaded and created from Template project on Github. I will end up.
This time, we will create it with 2.6.25
instead of the latest ( 2.8.0
as of December 2019), so we need to specify the branch.
Looking around for a way to specify a branch, I found a page in the sbt formula mentioning the --branch
parameter.
sbt - Giter8 parameters
In the terminal, move to any directory and execute the following command.
sbt new playframework/play-java-seed.g8 --branch 2.6.x
After a while, you will be asked the following items in order on the terminal.
name [play-java-seed]:
organization [com.example]:
You can skip these two with enter as they are without entering them.
If you wait until the process is completed, a directory with the name play-java-seed
will be created directly under the directory, and the template project set will be downloaded into it.
Don't you think this is what you're doing so far?
"I want to download this template project with a different project name"
In such a case, you can create it with your favorite project name by using the --name = [project name]
option.
This option is described in the help of Giter8
used internally by the sbt new
command.
Giter8-How to use
By executing the following command, you can download the template project of 2.6.x
under the project name of play-sample
directly under any directory.
sbt new playframework/play-java-seed.g8 --branch 2.6.x --name=play-sample
Once you've successfully created your project, open it in your IDE.
I won't talk about IDEs this time, but ʻecipse or ʻIntelliJ IDEA
is often used in Java development other than Android.
If you open the project and check it, you will find a file called build.gradle
directly under the project.
This file is a configuration file for building projects in Gradle.
At the time of downloading, it is a Gradle 5 based setting, so we will modify it to support the Gradle Play Plugin.
The reference for changing the settings is "1.3. Applying the plugin", "3.1. Adding dependencies", "4.1. Targeting a" in Gradle Play Plugin Official. There are 4 items, "Play version" and "4.2. Configuring routes style".
1.3. Applying the plugin It is a setting of the plug-in to be loaded into Gradle. At the time of downloading, the description is as follows.
plugins {
id 'play'
id 'idea'
}
ʻId: The item marked'play'` is the setting to load the plug-in used up to Gradle 5. Change this to load the Gradle Play Plugin as follows.
plugins {
// id 'play'
id 'org.gradle.playframework' version '0.9'
id 'idea'
}
3.1. Adding dependencies Dependent library settings used when building a project. At the time of downloading, the settings are as follows.
dependencies {
play dependencyFor("com.typesafe.play:play-guice", scalaVersion, playVersion)
play dependencyFor("com.typesafe.play:play-logback", scalaVersion, playVersion)
play dependencyFor("com.typesafe.play:filters-helpers", scalaVersion, playVersion)
}
Play support in Gradle has disappeared, and the setting method of dependent libraries has been changed by moving to Gradle Play Plugin.
Refer to the official explanation of Gradle Play Plugin and change it to load the library corresponding to 2.6.25
.
dependencies {
// play dependencyFor("com.typesafe.play:play-guice", scalaVersion, playVersion)
// play dependencyFor("com.typesafe.play:play-logback", scalaVersion, playVersion)
// play dependencyFor("com.typesafe.play:filters-helpers", scalaVersion, playVersion)
implementation "commons-lang:commons-lang:2.6"
implementation "com.typesafe.play:play-guice_2.12:2.6.25"
implementation "ch.qos.logback:logback-classic:1.2.3"
testImplementation "org.scalatestplus.play:scalatestplus-play_2.12:3.1.2"
}
This time it works with 2.6.25
, so change the library version of com.typesafe.play: play-guice
to 2.6.25
instead of 2.6.15
presented in the Gradle Play Plugin official. doing.
4.1. Targeting a Play version It is the setting of the version of Play Framework, Java, Scala to be targeted at build time. At the time of downloading, the settings are as follows.
def playVersion = "2.6.22"
def scalaVersion = System.getProperty("scala.binary.version", /* default = */ "2.12")
...
model {
components {
play {
platform play: playVersion, scala: scalaVersion, java: '1.8'
injectedRoutesGenerator = true
sources {
twirlTemplates {
defaultImports = TwirlImports.JAVA
}
}
}
}
}
Since the format has been changed by moving to Gradle Play Plugin, change it according to the official description of Gradle Play Plugin.
Also, since the version of Play Framework used this time is 2.6.25
, the description of playVersion will also be changed.
def playVersion = "2.6.25"
def scalaVersion = System.getProperty("scala.binary.version", /* default = */ "2.12")
...
play {
platform {
playVersion=playVersion
scalaVersion=scalaVersion
javaVersion=JavaVersion.VERSION_13
}
}
Change the setting of the javaVersion
parameter according to the Java version used for the build.
As of 12/12/2019, Java 13 will be installed if you do not specify the version when running brew cask install java
.
Therefore, specify the version information in JavaVersion.VERSION_13
defined in Gradle.
Definition information for other versions can be found on the following pages of the official Gradle reference.
JavaVersion (Gradle API 6.0.1)
4.2. Configuring routes style
Set whether the format of routes used to associate URL and Controller in Play Framework is StaticRoutesGenerator
or ʻInjectedRoutesGenerator. Since
2.4 or later of Play Framework, ʻInjectedRoutesGenerator
is the default setting, add the setting as follows.
play {
platform {
playVersion=playVersion
scalaVersion=scalaVersion
javaVersion=JavaVersion.VERSION_13
}
injectedRoutesGenerator = true
}
Go back to the terminal and run the following command:
gradle runPlay
When the build is successful and the development server is started, the following display will be displayed.
BUILD SUCCESSFUL in 1s
7 actionable tasks: 1 executed, 6 up-to-date
Waiting for changes to input files of tasks... (ctrl-d to exit)
<=============> 100% EXECUTING [1m 49s]
> IDLE
When this is displayed, access http://127.0.0.1:9000 with a browser. If such a screen is displayed, the startup is successful.
Recommended Posts