2

Demonstration project

It has parent root module and two submodules: core and http(depends on core)
For kotlin I use kotlin-maven-plugin
Problem is http tests can't see kotlin classes from core tests.
In this example I want to use abstract integration test class from core in http tests.

My maven config:

parent pom.xml: https://github.com/vatrubin/kotlin-submodules-demo/blob/master/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <artifactId>parent</artifactId>
    <groupId>ru.vatrubin</groupId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>parent</name>

    <modules>
        <module>core</module>
        <module>http</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.code.style>official</kotlin.code.style>
        <kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
        <java.version>11</java.version>
        <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
    </properties>

    <repositories>
        <repository>
            <id>mavenCentral</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>ru.vatrubin</groupId>
                <artifactId>core</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>ru.vatrubin</groupId>
                <artifactId>http</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-test-junit</artifactId>
                <version>1.4.10</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib</artifactId>
                <version>1.4.10</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>1.4.10</version>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <configuration>
                                <sourceDirs>
                                    <source>src/main/java</source>
                                    <source>src/main/kotlin</source>
                                </sourceDirs>
                            </configuration>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>test-compile</phase>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                            <configuration>
                                <sourceDirs>
                                    <source>src/test/java</source>
                                    <source>src/test/kotlin</source>
                                </sourceDirs>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

core pom.xml: https://github.com/vatrubin/kotlin-submodules-demo/blob/master/core/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <parent>
        <artifactId>parent</artifactId>
        <groupId>ru.vatrubin</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>

    <artifactId>core</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>core</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

http pom.xml: https://github.com/vatrubin/kotlin-submodules-demo/blob/master/http/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <parent>
        <artifactId>parent</artifactId>
        <groupId>ru.vatrubin</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>

    <artifactId>http</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>http</name>

    <repositories>
        <repository>
            <id>mavenCentral</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>ru.vatrubin</groupId>
            <artifactId>core</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-stdlib</artifactId>
        </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>
            </plugin>
        </plugins>
    </build>
</project>

Classes

IntegrationTestAbstract

package ru.vatrubin.kotlinMavenDemo

import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

abstract class IntegrationTestAbstract {
    companion object {
        //prepare db
        const val SOME_CONSTANT = "test"
    }
}

@Configuration
@ComponentScan("ru.vatrubin.**")
@EnableAutoConfiguration
open class IntegrationTestConfig

SimpleHttpTest

package ru.vatrubin.kotlinMavenDemo

import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner

@SpringBootTest(classes = [IntegrationTestConfig::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner::class)
class SimpleHttpTest : IntegrationTestAbstract() {

    @Test
    fun `some http test`() {
        Assert.assertEquals(SOME_CONSTANT, SOME_CONSTANT)
    }
}

Errors during mvn verify

...
Error:  Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.10:test-compile (test-compile) on project http: Compilation failure: Compilation failure: 
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[9,27] Only 'const val' can be used in constant expressions
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[9,28] Unresolved reference: IntegrationTestConfig
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[9,28] An annotation argument must be a compile-time constant
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[11,24] Unresolved reference: IntegrationTestAbstract
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[15,29] Unresolved reference: SOME_CONSTANT
Error:  /home/runner/work/kotlin-submodules-demo/kotlin-submodules-demo/http/src/test/kotlin/ru/vatrubin/kotlinMavenDemo/SimpleHttpTest.kt:[15,44] Unresolved reference: SOME_CONSTANT
...

What's wrong?!


Found similar issue: Creating a multi module project with maven spring and kotlin get unresolved reference

vatrubin
  • 21
  • 4
  • What about your IntegrationTestConfig? – Daniel Jacob Feb 11 '21 at 21:29
  • It is in `IntegrationTestAbstract` file. Just open class with spring annotations. Tests in `http` project can't see it too. – vatrubin Feb 11 '21 at 22:46
  • It is probably the argument in your @componentScan. Change it to an actual package name instead of using wildcards. This is not necessary anyway since spring will scan all the child packages too. – Daniel Jacob Feb 12 '21 at 07:08

0 Answers0