项目结构:纯Kotlin作为多模块maven应用程序
kotlinspringboot (根模块)
问题:当我运行时
$ 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干净安装,并验证了.m2缓存包含有效的api模块jar。
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上的项目
发布于 2019-05-16 15:08:26
因为Spring删除了SpringBoot2.0中的“模块”布局,所以maven在尝试LayoutType答案时抱怨一个不存在的Christophs ENUM。
不过,看看这些文档帮助我解决了这个问题:https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html
特别是将其添加到spring maven-plugin中:
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>发布于 2018-03-04 20:40:42
这个问题是由Spring重新打包API jar引起的。它将应用程序的类文件从jar的根移动到jar中的BOOT-INF/ class文件夹中。在编译集成测试时,kotlin编译器只在jar的根目录中搜索类文件,而不查看BOOT-INF文件夹。因此,它无法解析对API中类的引用。
This answer by Damien描述了如何使Spring将应用程序类保持在jar的根中。如果我将上面提到的配置添加到您的api/头.xml中,项目中的集成测试将按预期进行编译:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>MODULE</layout>
</configuration>
</plugin>https://stackoverflow.com/questions/48951653
复制相似问题