Kotlin多模块项目依赖关系在测试生命周期中尚未解决

Ild*_*kov 7 testing maven multi-module kotlin

项目结构: 纯Kotlin作为多模块maven应用

kotlinspringboot(root-module)

  • API

  • 集成的测试

问题: 我跑的时候

$ cd ./kotlingspringboot/integration-tests/
$ mvn test
Run Code Online (Sandbox Code Playgroud)

要么

$ cd ./kotlingspringboot/integration-tests/
$ mvn kotlin:test-compile
Run Code Online (Sandbox Code Playgroud)

我从api模块得到关于类的未解析引用的编译错误:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.2.21:test-compile (test-compile) on project hello-integration-tests: Compilation failure: Compilation failure:
[ERROR] C:\workspace\kotlinspringboot\integration-tests\src\test\kotlin\org\ildar\hello\integration\HelloEndpointTests.kt:[6,18] Unresolved reference: SpringKotlinHelloWorldApplication
Run Code Online (Sandbox Code Playgroud)

注意:我之前运行过mvn clean install并验证.m2缓存包含有效的api模块jar.

api(子模块)pom.xml

    ....
    <parent>
        <artifactId>kotlin-spring-boot</artifactId>
        <groupId>org.ildar</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hello-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
  ....
Run Code Online (Sandbox Code Playgroud)

集成测试(子模块)pom.xml

<parent>
    <artifactId>kotlin-spring-boot</artifactId>
    <groupId>org.ildar</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>hello-integration-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    <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>
                </execution>

                <execution>
                    <id>test-compile</id>
                    <goals> <goal>test-compile</goal> </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

链接到github上的项目:https: //github.com/IldarGalikov/kotlinspringboot

Mar*_*aas 6

由于 Spring 在 Spring Boot 2.0 中删除了“MODULE”布局,maven 在尝试Christophs回答时抱怨不存在的 LayoutType ENUM 。

虽然查看文档帮助我解决了这个问题:https : //docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

专门将其添加到 spring-boot-maven-plugin 中:

<executions>
  <execution>
    <id>repackage</id>
    <configuration>
      <classifier>exec</classifier>
    </configuration>
  </execution>
</executions>
Run Code Online (Sandbox Code Playgroud)


Chr*_*hme 5

该问题是由于Spring Boot重新打包API jar导致的。它将应用程序的类文件从 jar 的根目录移动到 jar 中的 BOOT-INF/classes 文件夹中。编译集成测试时,kotlin 编译器仅在 jar 的根目录中搜索类文件,而不查看 BOOT-INF 文件夹。因此,它无法解析对 API jar 中的类的引用。

Damien这个回答描述了如何让 Spring Boot 将应用程序类保留在 jar 的根目录中。如果我将其中提到的配置添加到您的 api/pom.xml 中,项目中的集成测试将按预期编译:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>MODULE</layout>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

  • 对我来说不起作用。我收到此错误无法将“模块”转换为枚举 (3认同)