Unresolved reference with maven modules, Spring Boot and Kotlin


#1

Hello everyone,

I am currently building a micro-service using Kotlin, Spring Boot, and Maven.

So far, so good, the thing is that I would like to maven submodules like the following:

customer (simple aggregator)
|— customer-core (Application)
|— customer-library (Library - the name is temporary)

The customer-core module requires customer-library.

The issue is that if a reference a class from customer-library in customer-core, it won’t build and I will get a beautiful Unresolved reference.

ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.1.3-2:compile (compile) on project customer-core: Compilation failure
[ERROR] /Users/user/workspace/customer/customer-core/src/main/kotlin/com/company/project/api/CustomerResource.kt:[28,41] Unresolved reference: CustomerCommonService
  • CustomerResource is in customer-core and references CustomerCommonService.
  • CustomerCommonService is in customer-library
  • Both parent and customer-library are successfully building
  • No issue in IntelliJ to: CustomerResource finds CustomerCommonService

I believe the issue is in the configuration of maven:

pom.xml (parent)

<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.1.3-2</kotlin.version>
        <junit.version>4.12</junit.version>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>customer-library</module>
        <module>customer-core</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.4.RELEASE</version>
                <configuration>
                    <mainClass>com.company.project.ApplicationKt</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml (customer-core)

<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.company.project</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.company.project</groupId>
    <artifactId>customer-core</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.1.3-2</kotlin.version>
        <junit.version>4.12</junit.version>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.company.project</groupId>
            <artifactId>customer-library</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-runtime</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <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-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <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>
        </plugins>
    </build>
</project>

pom.xml (customer-library)

<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.company.project</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.company.project</groupId>
    <artifactId>customer-library</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.1.3-2</kotlin.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-runtime</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <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-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <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>
        </plugins>
    </build>
</project>

I am pretty sure I am missing something obvious, but I just can’t put the finger on it. I obviously did some research.

An idea anyone?

Thanks :slightly_smiling_face:


#2

What if you first do mvn install for the customer-library project, and then for customer-core? Does that work?

If not, check the contents of the JAR file generated from mvn install for customer-library – it is likely missing the Kotlin class file.

When investigating how to set up my own Kotlin project with Spring Boot, I decided rather quickly to go for Gradle as it resulted in rather much simpler build files.


#3

I had exact same problem and changing part of kotlin-maven-plugin to:

<execution>
    <id>compile</id>
    <phase>compile</phase>
    <goals>
        <goal>compile</goal>
    </goals>
</execution>

solved the issue


#4

Hah, not works :frowning: Change in every module include parent