我有一个 kotlin 项目,其中一些代码是使用注释处理 (AbstractProcessor) 生成的。生成的代码在java中,生成成
./target/generated-sources/annotations
生成的代码访问一些 kotlin 类,并在 java 中使用以下构建配置,一切都编译得很好
<build>
<finalName>xxx</finalName>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.build.directory}/generated-sources/annotations</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.build.directory}/generated-sources/annotations</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--
Take care of synchronizing java dependencies and imports in
package.json and main.js files.
It also creates webpack.config.js if not exists yet.
-->
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-frontend</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
但是,当我想在我的 Kotlin 项目中使用生成的类时,maven 不再编译并出现以下错误:
mvn clean package spring-boot:repackage -Pproduction -DskipTests=true
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.7.22:compile (compile) on project xxx: Compilation failure: Compilation failure:
[ERROR] /path/to/project/src/main/kotlin/com/x/y/z/View.kt:[12,44] Unresolved reference: CRUD
[ERROR] /path/to/project/src/main/kotlin/com/x/y/z/View.kt:[35,38] Unresolved reference: XxxCRUDController
关于如何在我的 Kotlin 项目中使用我生成的 Java 类有什么想法吗?
请(尝试)更改:
<execution>
<id>java-compile</id>
<phase>compile</phase> ...
致:
<execution>
<id>java-srcgen</id> <!-- optionally, but makes things clearer/is a better id;) -->
<phase>generate-sources</phase> <!-- important!! -->
...
看来,你编译到早期/生成(java 源代码)太晚了..
我们可以维护(我们的 maven)插件执行的顺序:
phase
(书签/研究链接!)。