Creating the Kotlin-Gradle project

IntelliJ IDEA offers an option to create a Kotlin project with Gradle as the build system. In the New Project window, just select Gradle on the left and tick the Kotlin checkbox on the right:

If you are using the downloaded code files, you can use the New Project from existing sources option to create an IntelliJ project (in the file selection dialog, just select the build.gradle file from the root of the source code folder).

With the project sorted out, next we need to set up our build.gradle file.

JavaFx apps are usually packaged with a tool called javapackagerhttps://docs.oracle.com/javase/9/tools/javapackager.htm#JSWOR719. There is a Gradle plugin for JavaFx ( https://github.com/FibreFoX/javafx-gradle-plugin) that is a wrapper for this packager and enables assembling and distributing JavaFx apps with Gradle. This also enables running the project from IntelliJ with the click of a button.

IntelliJ created a build.gradle file for us. It should be located in the root of the project. Let's add the JavaFx plugin to the dependencies. Kotlin Gradle was added by IntelliJ, and below that we add the JavaFx plugin:

buildscript {
ext.kotlin_version = '1.2.60'

repositories {
mavenCentral()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
}
}

group 'com.packt.kotlinquickstart'
version '1.0'

We also have to tell the Gradle to use it, and we apply it below the line to apply Kotlin:

apply plugin: 'kotlin'
apply plugin: 'javafx-gradle-plugin'

With the plugin added, we can configure the run configuration inside IntelliJ so we can run our app from the IDE. In the Run menu, click on the Edit Configurations menu option. Then, click on the + button in the upper-right corner and the Add New Configuration popup will appear. There, click on the Gradle option. Select our English Dictionary under the project and under Tasks, enter jfxRun:

There is one more thing needed for the JavaFx plugin to work: we need to declare the main class of our JavaFx app. This class extends the JavaFx application class and it serves as the main entry point into our app.

Our package structure is com.packt.kotlinquickstart.dictionary, and this package structure is in the src/main/java folder. In that package, we'll now add the MainApp class:

class MainApp : Application() 

The Application class has one abstract method, onStart, which we have to override:

public abstract void start(Stage primaryStage) throws Exception;

To make our code compile, we override it, but leave it empty for now:


override fun start(primaryStage: Stage) {
}

Back in the build.gradle file, we tell the JavaFx plugin to use this class as the entry point to our app:

jfx {
mainClass = 'com.packt.kotlinquickstart.dictionary.MainApp'
vendor = 'Packt Publishing'
}

Finally, in the dependencies section, we add the libraries we're going to use. We need the Kotlin Standard Library (compatible with Java 8), the JDBC driver for SQLite, and the Jackson JSON deserializer:

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.21.0.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.3.43