Maven 使用生成的源代码编译 kotlin 和 java

问题描述 投票:0回答:1

我有一个 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 类有什么想法吗?

java maven kotlin code-generation annotation-processing
1个回答
1
投票

请(尝试)更改:

<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)插件执行的顺序:

  1. 通过
    phase
    (书签/研究链接!)。
  2. Via(有效)“pom-order”(pom 中出现的顺序)。 (我(也)用 maven 3.9.0 验证了这个,但不会依赖那个“永远”:)(这解释/也能够解决你的问题:kotlin 插件执行出现在 pom 中(在同一阶段) before maven-compiler-plugin.)
© www.soinside.com 2019 - 2024. All rights reserved.