Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin is not compiling from src/main/kotlin while using maven

The kotlin compiler seems to only be trying to compile .kt files that are in src/main/java, and is ignoring src/main/kotlin. However, everything seems to be linked correctly in the IntelliJ IDE. No errors.

Below is my plugin configuration for kotlin:

<plugins>
        <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>
                    <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>

However, when I run mvn clean install, the kotlin compiler does not seem to run. So I try to run the kotlin compiler directly from the plugin.

    [INFO] --- kotlin-maven-plugin:1.1.2:compile (default-cli) @ eagle-client-core ---
[INFO] Kotlin Compiler version 1.1.2
[INFO] Compiling Kotlin sources from [C:\Users\me\workspace\Project\Clients\project-client\project-client-core\src\main\java]

As you can see, src/main/java is getting scanned, but not src/main/kotlin.

I don't see anything obviously wrong with my configuration. Any help is appriciated.

like image 427
Zach Melnick Avatar asked May 20 '17 02:05

Zach Melnick


People also ask

Can you use Kotlin with Maven?

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.

How is Kotlin code compiled?

Yes, when targeting the JVM, Kotlin is compiled to JVM *. class files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code.

What does Kotlin Maven plugin do?

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

Can Kotlin compile Java code?

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


1 Answers

You would likely need to turn off the default compile as noted in https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
        <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>
like image 134
Archimedes Trajano Avatar answered Oct 28 '22 23:10

Archimedes Trajano