Since v7.0 you can easily develop OpenXava projects with Kotlin using OpenXava Studio, Eclipse, IntelliJ IDEA and others.
Kotlin is the most used language in Android applications and can be developed on JVM or JS. One of the Kotlin's features is that it's designed to fully interoperate with Java syntax, it means that you can compile both codes and have them interact with each other without any problems.
After created an OpenXava project (
how to), you must define the Kotlin version in properties in the
pom.xml file located in the root folder of the project:
<properties>
<kotlin.version>1.8.0</kotlin.version>
</properties>
Add the following dependency in
dependencies to use the Kotlin library:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
To build projects that include Kotlin and Java code, add the following in
build:
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Create the
kotlin folder in
src/main to store Kotlin codes, in the same folder create
com.yourcompany.invoicing.model package for OpenXava entities. You may need to do a
Reload project in Maven so that you can create packages in the
kotlin folder.
Create a Kotlin class in the
model package with the name
Customer (or
Customer.kt file in Eclipse/OpenXava Studio):
package com.yourcompany.invoicing.model
import org.openxava.annotations.Required
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
@Entity
class Customer {
@Id
@Column(length = 6)
var number : Int ?= null
@Required
@Column(length = 50)
var name : String ?= null
}
Before running your project for the first time, you must make a
mvn package, in the next ones mvn compile is enough. Then find the
invoicing class inside the package
com.yourcompany.invoicing.run in
src/main/java, and run it. In the browser go to
http://localhost:8080/invoicing and log in with
admin/admin as username/password to see the results.
To create a simple button (action) where it displays a message, put the Kotlin
ShowMessageAction (
ShowMessageAction.kt) class in the
com.yourcompany.invoicing.action package of the
kotlin folder:
package com.yourcompany.invoicing.action
import org.openxava.actions.ViewBaseAction
class ShowMessageAction : ViewBaseAction() {
@Throws(Exception::class)
override fun execute() {
addMessage("Hello world!")
}
}
Remember that you have to declare the action in
controllers.xml in
src/main/resources/xava:
<controller name ="Customer">
<extends controller ="Typical"/>
<action name ="showMessage" class ="com.yourcompany.invoicing.action.ShowMessageAction" mode="detail"/>
</controller>
An entity with more features:
package com.yourcompany.invoicing.model
import org.openxava.annotations.*
import org.openxava.calculators.CurrentLocalDateCalculator
import org.openxava.calculators.CurrentYearCalculator
import org.openxava.model.Identifiable
import java.time.LocalDate
import javax.persistence.*
@Entity
@View(members="""
year, number, date,
data {
customer;
details;
remarks
}
"""
)
class Invoice : Identifiable() {
@Column(length = 4)
@DefaultValueCalculator(CurrentYearCalculator::class)
var year : Int? = null
@Column(length = 6)
var number : Int? = null
@Required
@DefaultValueCalculator(CurrentLocalDateCalculator::class)
var date: LocalDate? = null
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@ReferenceView("Simple")
var customer: Customer? = null
@ElementCollection
@ListProperties("product.number, product.description, quantity")
var details: Collection<Detail>? = null
@TextArea
var remarks: String? = null
}
If you decide to develop your OpenXava project with Kotlin, we recommend use IntelliJ IDEA, because it comes with Kotlin included which offers autocompletion, code coloring, automatic compilation, etc.
You can find more details about Kotlin in its
documentation and how to integrate Kotlin into
maven projects.
You can also see more about how to use OpenXava in our
course developed in Java.