9

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.

2
  • Possible duplicate of How do you compile Java+Kotlin project using Maven? Commented May 20, 2017 at 3:02
  • @Aryan, this is a different question: the very idea of Maven is convention over configuration, so things like folder src/main/kotlin should be picked up by default. Commented Apr 18, 2019 at 18:34

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed my issue. However, strangly, when I run the kotlin complier from the plugin itself, it still only runs for src/main/java. However, When I run a mvn clean install, it looks in src/main/kotlin as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.