简体   繁体   English

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

[英]Kotlin Multi-module Project Dependency is Unresolved in test Lifecycle

Project structure: Pure Kotlin as multi module maven application 项目结构:纯Kotlin作为多模块maven应用

kotlinspringboot (root-module) kotlinspringboot(root-module)

  • api API

  • integration-tests 集成的测试

Problem: When I run 问题:我跑的时候

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

or 要么

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

I get following compilation error regarding unresolved reference for a class from api module : 我从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

Note: I've ran previously mvn clean install and verified that .m2 cache contains valid api module jar. 注意:我之前运行过mvn clean install并验证.m2缓存包含有效的api模块jar。

api (sub-module) pom.xml 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>
  ....

integration-tests (sub-module) pom.xml 集成测试(子模块)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>

Link to a project on github : https://github.com/IldarGalikov/kotlinspringboot 链接到github上的项目: https//github.com/IldarGalikov/kotlinspringboot

The problem is caused by Spring Boot's repackaging of the API jar. 问题是由Spring Boot重新打包API jar引起的。 It moves the class files of your application from the root of the jar into a BOOT-INF/classes folder in the jar. 它将应用程序的类文件从jar的根目录移动到jar中的BOOT-INF / classes文件夹中。 When compiling the integration tests the kotlin compiler only search the root of the jar for class files and does not look into the BOOT-INF folder. 在编译集成测试时,kotlin编译器只搜索jar的根目录中的类文件,而不是查看BOOT-INF文件夹。 As a result it cannot resolve references to the classes in the API jar. 因此,它无法解析API jar中类的引用。

This answer by Damien describes how to make Spring Boot keeping your application classes in the root of the jar. Damien的 回答描述了如何使Spring Boot将应用程序类保存在jar的根目录中。 If I add the configuration mentioned there to your api/pom.xml the integration tests in your project compile as expected: 如果我将那里提到的配置添加到api / pom.xml中,项目中的集成测试将按预期进行编译:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>MODULE</layout>
    </configuration>
</plugin>

Since Spring removed the "MODULE" Layout in Spring Boot 2.0, maven was complaining about a non existent LayoutType ENUM when trying Christophs answer. 由于Spring删除了Spring Boot 2.0中的“MODULE”布局,maven在尝试Christophs回答时抱怨不存在LayoutType ENUM。

Looking at the Docs though helped me resolve the issue: https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html 查看文档虽然帮助我解决了这个问题: https//docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

Specifically adding this to the spring-boot-maven-plugin: 特别是将它添加到spring-boot-maven-plugin:

<executions>
  <execution>
    <id>repackage</id>
    <configuration>
      <classifier>exec</classifier>
    </configuration>
  </execution>
</executions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM