Introduce Kotlin to your existing Java Maven Project

Introduction

Introducing Kotlin into a Maven project written in Java is easy. Simply rewrite pom.xml to add the Kotlin source code and build with the Java code. As expected * 100% interoperable with Java ™ *! (\ *'E` \ *)

If I modify pom.xml according to the official document, it works somehow, but I didn't understand why Maven works in the first place, so I investigated it.

It's almost Maven rather than Kotlin

Before / After of pom.xml

Let's take a simple pom.xml as an example. By rewriting pom.xml, you can add the code of Kotlin to the Maven Project that originally worked only with * .java.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>megmogmog1965</groupId>
    <artifactId>JavaJarToKotlin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
+       <!-- Kotlin -->
+       <kotlin.version>1.1.2-5</kotlin.version>
    </properties>

    <dependencies>
+       <!-- Kotlin -->
+       <dependency>
+           <groupId>org.jetbrains.kotlin</groupId>
+           <artifactId>kotlin-stdlib-jre8</artifactId>
+           <version>${kotlin.version}</version>
+       </dependency>
+       <dependency>
+           <groupId>org.jetbrains.kotlin</groupId>
+           <artifactId>kotlin-test</artifactId>
+           <version>${kotlin.version}</version>
+           <scope>test</scope>
+       </dependency>
    </dependencies>

    <build>
        <plugins>
+           <!-- Kotlin -->
+           <plugin>
+               <groupId>org.jetbrains.kotlin</groupId>
+               <artifactId>kotlin-maven-plugin</artifactId>
+               <version>${kotlin.version}</version>
+               <executions>
+                   <execution>
+                       <id>compile</id>
+                       <phase>compile</phase>
+                       <goals>
+                           <goal>compile</goal>
+                       </goals>
+                   </execution>
+                   <execution>
+                       <id>test-compile</id>
+                       <phase>test-compile</phase>
+                       <goals>
+                           <goal>test-compile</goal>
+                       </goals>
+                   </execution>
+               </executions>
+           </plugin>

            <!-- for .java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.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>compile</id>
+                       <phase>compile</phase>
+                       <goals>
+                           <goal>compile</goal>
+                       </goals>
+                   </execution>
+                   <execution>
+                       <id>testCompile</id>
+                       <phase>test-compile</phase>
+                       <goals>
+                           <goal>testCompile</goal>
+                       </goals>
+                   </execution>
+               </executions>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Commentary

First of all, I don't understand the meaning of Phase or Goal, so I'll try to make a diagram. Below is the Build Lifecycle diagram of the above pom.xml.

kotlin-build-lifecycle.png

In short, I want to build the Kotlin code first and * .java later. Otherwise, the reference from * .java to Kotlin cannot be resolved.

properties

<properties>
    <!-- Kotlin -->
    <kotlin.version>1.1.2-5</kotlin.version>
</properties>

dependencies

<dependencies>
    <!-- Kotlin -->
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jre8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

plugins --> kotlin-maven-plugin

<plugins>
    <!-- Kotlin -->
    <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <version>${kotlin.version}</version>
        <executions>
            <execution>
                <id>compile</id>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>test-compile</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    ...

Same as test-compile

plugins --> maven-compiler-plugin

<plugins>
    ...
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.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>compile</id>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>testCompile</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
        ...

In other words, in order to execute kotlin-maven-plugin: compile first, I deleted it and added it again.

Same as test-compile

Recommended Posts

Introduce Kotlin to your existing Java Maven Project
Introduce Maven to Tomcat project
Use Maven to add your favorite Java library to your environment.
Kotlin Class to send to Java developers
How to make a Maven project
[Android] Convert Android Java code to Kotlin
Kotlin Class part.2 to send to Java developers
Try to solve Project Euler in Java
Introduce two-factor authentication to your Rails application
Convert all Android apps (Java) to Kotlin
Added slf4J + logback to Eclipse Maven project
Kotlin scope functions to send to Java developers
[Java] [Maven3] Summary of how to use Maven3
Kotlin vs. Java: Which Programming Language to Choose for Your Android App
Memo for migration from java to kotlin
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
Deploy Java web app to Azure with maven
Migrate from Java to Server Side Kotlin + Spring-boot
Initial settings for rewriting Java projects to Kotlin
Getting started with Kotlin to send to Java developers
Automate Java (Maven) project build with CircleCI + Orbs
What I did when I converted java to Kotlin
Java adds page numbers to existing PDF documents
Where Java programmers tend to trip over Kotlin
Introduction to kotlin for iOS developers ②-Project creation
How to write Java String # getBytes in Kotlin?
What Java inexperienced people did to study Kotlin
Add packages to your project with Swift PM
java, maven memo
[Java] Introduction to Java
Introduction to java
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Introduce RMagick to convert existing existing image files to another format
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
Rails6 I tried to introduce Docker to an existing application
I want to transition screens with kotlin and java!
[Swift 5] Note: Add Core Data to an existing project