Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin maven plugin order of compilation

Tags:

maven

kotlin

I have a question regarding the kotlin-maven plugin: How is the order of compilation determined? I mean: Must the kotlin compiler come first? Does it understand java code? The java compiler doesn't understand kotlin code, so I guess it checks against the byte code, but for this, the bytecode for kotlin must be present, so someone has to determine that the kotline compiler must compile its code before javac.

Example: I have java Class A that depends on Kotlin Class B and B also depends on A.

Do I have to do something manually in the maven plugin by declare it before the java maven compiler?

like image 304
morpheus05 Avatar asked Jun 06 '16 08:06

morpheus05


People also ask

Does Kotlin compile to Java or bytecode?

Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.

What does Kotlin Maven plugin do?

The kotlin-maven-plugin compiles Kotlin sources and modules.

How does Kotlin compile?

The Kotlin compiler for JVM compiles Kotlin source files into Java class files. The command-line tools for Kotlin to JVM compilation are kotlinc and kotlinc-jvm . You can also use them for executing Kotlin script files. In addition to the common options, Kotlin/JVM compiler has the options listed below.

Can you use Maven with Kotlin?

If you still prefer to stick with good old Maven, there is no problem. There is a plugin for it to support Kotlin as well. If you don't have Maven on your machine, you can follow the instructions at https://maven.apache.org/install.html to get it installed on your local machine.


2 Answers

Kotlin compiler can parse java sources, so you could reference java classes in your kotlin code, even if those classes have not been compiled yet.

After Kotlin compiler had compiled kotlin code, Java compiler could compile those java classes taking compiled kotlin classes as a dependency.

So, answering your question, yes — Kotlin compiler plugin must be executed before java compiler plugin in a project that mixes kotlin and java sources. This is achieved by scheduling the execution on the earlier phase of build, namely process-sources.

Here is the example of kotlin maven plugin configuration for mixed Kotlin-Java maven module:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>process-sources</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>
like image 80
Ilya Avatar answered Sep 24 '22 15:09

Ilya


In order to compile a project that contains both, Kotlin and Java code, you have to make sure that the Kotlin compiler runs before the Java compiler. I configured the compile plugins as described in the official Kotlin documentation and it worked like a charm:

        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <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>
like image 39
jjoller Avatar answered Sep 23 '22 15:09

jjoller