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

时间:2018-02-23 15:51:47

标签: maven testing kotlin multi-module

项目结构: Pure Kotlin作为多模块maven应用程序

kotlinspringboot(root-module)

  • api

  • 整合测试

问题: 我跑的时候

$ cd ./kotlingspringboot/integration-tests/
$ mvn test

$ cd ./kotlingspringboot/integration-tests/
$ mvn kotlin:test-compile

我从 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

注意:我之前运行过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>
  ....

集成测试(子模块)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>

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

2 个答案:

答案 0 :(得分:2)

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

This answer的{p> 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>

答案 1 :(得分:1)

由于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>