[Java] [Play Framework] Until the project is started with Gradle

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.

Goals in this article

--Install the tools needed to use the Play Framework --Start the Play Framework development server using Gradle

environment

Overview

  1. [Install required tools](#Install required tools)
  2. Java
  3. sbt
  4. Gradle
  5. [Create a project](#Create a project)
  6. [Create Template Project](#Create Template Project)
  7. [When you want to download the project to any directory](#When you want to download the project to any directory)
  8. Launch Play Framework project using Gradle (Launch play-framework project using #gradle)
  9. [build.gradle settings](#buildgradle settings)
  10. 1.3. Applying the plugin
  11. 3.1. Adding dependencies
  12. 4.1. Targeting a Play version
  13. 4.2. Configuring routes style
  14. [Start on development server](# Start on development server)

Install the required tools

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

Create a project

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.

Creating a template project

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.

When you want to download the project to any directory

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

Launch a Play Framework project using Gradle

build.gradle settings

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
}

Start on the development server

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.

スクリーンショット 2019-12-16 18.25.11.png

Recommended Posts

[Java] [Play Framework] Until the project is started with Gradle
Get started with serverless Java with the lightweight framework Micronaut!
Build a Java project with Gradle
[Gradle] Build a Java project with a configuration different from the convention
Understanding the MVC framework with server-side Java 1/4 View
Understanding the MVC framework with server-side Java 3/4 Controller
Understanding the MVC framework with server-side Java 2/4 Model
Play Framework2.5 (Java) Tips
Getting started with Gradle (until you create a Java project and combine external libraries into one executable JAR)
Get started with Gradle
Java multi-project creation with Gradle
Getting Started with Java Collection
Java to play with Function
Getting Started with Java Basics
Now is the time to get started with the Stream API
Until the code is executed
Java is the 5th day
Returning to the beginning, getting started with Java ② Control statements, loop statements
Project facet Java version 13 is not supported. How to deal with
Is the version of Elasticsearch you are using compatible with Java 11?
Double submit measures with Play Framework
Where is the Java LocalDateTime.now () timezone?
Consideration on the 2017 Java Persistence Framework (1)
I tried the Java framework "Quarkus"
What is thread safe (with Java)
Follow the link with Selenium (Java)
Getting started with Java lambda expressions
Play with Markdown in Java flexmark-java
Image processing: Let's play with the image
[Java: memorandum] Until the line feed code CRLF is changed to LF
Getting started with Maven (until you create a Java project and combine external libraries into a single executable JAR)
Guess about the 2017 Java Persistence Framework (3) Reladomo
[Java] Create an executable module with Gradle
Getting Started with Ruby for Java Engineers
How to use BootStrap with Play Framework
IntelliJ console is garbled during gradle project
CICS-Run Java applications-(3) Build management with Gradle
What is the best file reading (Java)
Getting Started with Java Starting from 0 Part 1
Play Framework 2.6 (Java) environment construction in Eclipse
Authentication function with Play Framework [Registration and authentication]
What is the main method in Java?
The Java EE Security API is here!
Getting started with the JVM's GC mechanism
Post to Slack from Play Framework 2.8 (Java)
Play with the Processing libraries "ControlP5", "Fisica"
Java automated test implementation with JUnit 5 + Gradle
Try using the Wii remote with Java
Explanation until the original application is completed
[Java] Get the date with the LocalDateTime class
Initialize Ruby array with 0 like Java, that is, set the default value to 0
Until you run a Java program with the AWS SDK local to Windows
Be careful with requests and responses when using the Serverless Framework in Java