Minimal example of a Maven pom for a mixed Kotlin and Java project
August 18, 2021 [Java, Kotlin, Maven, Programming, Tech]The Kotlin docs describe some things you need in your pom.xml to create a project that is a mix of Kotlin and Java code, but there is no complete example, so here is mine:
pom.xml:
<project> <modelVersion>4.0.0</modelVersion><groupId>com.example.kj</groupId> <artifactId>kotlin-and-java</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <kotlin.version>1.5.21</kotlin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <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> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies>
</project>
src/main/java/MyJava.java:
public class MyJava { public static void main(String[] args) { MyKotlin k = new MyKotlin(); // Use Kotlin from Java System.out.println(k.message()); } }
src/main/kotlin/MyKotlin.kt:
class MyKotlin : MyJava() { // Use Java from Kotlin fun message(): String { return "Hello from Kotlin!" } }
src/test/java/MadeInJavaTest.java:
class MadeInJavaTest { public void testCanUseJava() { MyJava j = new MyJava(); }public void testCanUseKotlin() { MyKotlin k = new MyKotlin(); assertEquals(k.message(), "Hello from Kotlin!"); } static void assertEquals(String left, String right) { if (!left.equals(right)) { throw new AssertionError(left + " != " + right); } }
}
src/test/kotlin/MadeInKotlinTest.kt:
class MadeInKotlinTest { fun testCanUseJava() { MyJava() }fun testCanUseKotlin() { val k = MyKotlin(); assertEquals(k.message(), "Hello from Kotlin!"); }
}
fun assertEquals(left: String, right: String) { if (left != right) { throw AssertionError("${left} != ${right}"); } }