You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法引用openapi-generator-maven-plugin生成的Kotlin JAR类

问题描述

我在开发Kotlin项目时,使用openapi-generator-maven-plugin的kotlin-spring生成器根据OpenAPI规范生成类,将生成的类打包为JAR上传至Azure制品库后,在另一个项目中添加该JAR作为依赖。目前遇到的问题是:虽然能在IntelliJ的外部库中看到JAR内的Kotlin文件,但无法在代码中引用这些类,IntelliJ和Maven都未识别它们,引用时提示Unresolved reference: Cat。

生成JAR项目的插件配置

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>6.6.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>
              ${project.basedir}/src/main/resources/api/project-api.yaml
        </inputSpec>
        <generatorName>kotlin-spring</generatorName>
        <output>${project.build.directory}/classes</output>      <!-- *1* -->
        <configOptions>
          <basePackage>ch.organization.project</basePackage>
          <apiPackage>ch.organization.project.api</apiPackage>
          <modelPackage>ch.organization.project.api.model</modelPackage>
          <configPackage>ch.organization.project.api.config</configPackage>
          <sourceFolder>.</sourceFolder>
          <interfaceOnly>true</interfaceOnly>
          <modelNameSuffix>Dto</modelNameSuffix>
          <documentationProvider>none</documentationProvider>
          <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
          <useSpringBoot3>true</useSpringBoot3>
          <gradleBuildFile>false</gradleBuildFile>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

1: 该配置将生成类放在target/classes而非默认target/generated-sources,确保类被打包进JAR,否则外部库中无法看到生成类。

依赖项目的外部库结构

- External Libraries
  - Maven: ch.organisation.project:api:1.0.8-SNAPSHOT
    - api-1.0.8-SNAPSHOT.jar
        .openapi-generator-ignore
        pom.xml
        README.md
      - .openapi-generator
          ...
      - api
          project-api.yaml
      - ch.organisation.project.api
          CatsApi                      // Interface
          ApiUtil                      // Object class
        - model
            Cat                        // Data class
            Colour                     // Enum
      - META-INF
        MANIFEST.MF
        - maven.ch.organisation.project.api
            pom.properties
            pom.xml

奇怪的是,IntelliJ能识别ch.organisation.project.api.model目录,但无法引用其中内容。已尝试清理缓存、重启IDE、设置gradleBuildFile=false、使用默认output配置、忽略报错构建等操作,仍未解决。


解决方案

1. 修正源码生成与编译配置

当前将生成的Kotlin源码直接放到target/classes,但Maven默认不会编译该目录下的Kotlin文件(仅编译src/main/kotlin)。JAR中仅包含.kt源码而非编译后的.class字节码,导致依赖项目无法识别。

具体修改:

  • 将生成目录改回默认的target/generated-sources/kotlin,避免混淆Maven的编译目录
  • 添加kotlin-maven-plugin,配置它编译生成的Kotlin源码:
<!-- 添加Kotlin编译插件,包含生成的源码目录 -->
<plugin>
  <groupId>org.jetbrains.kotlin</groupId>
  <artifactId>kotlin-maven-plugin</artifactId>
  <version>${kotlin.version}</version>
  <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/kotlin</sourceDir>
        </sourceDirs>
      </configuration>
    </execution>
  </executions>
</plugin>

<!-- 修改openapi-generator配置,指定正确的生成路径 -->
<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>6.6.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${project.basedir}/src/main/resources/api/project-api.yaml</inputSpec>
        <generatorName>kotlin-spring</generatorName>
        <output>${project.build.directory}/generated-sources</output>
        <configOptions>
          <basePackage>ch.organization.project</basePackage>
          <apiPackage>ch.organization.project.api</apiPackage>
          <modelPackage>ch.organization.project.api.model</modelPackage>
          <configPackage>ch.organization.project.api.config</configPackage>
          <!-- 指定源码生成到kotlin目录,方便编译插件识别 -->
          <sourceFolder>kotlin</sourceFolder>
          <interfaceOnly>true</interfaceOnly>
          <modelNameSuffix>Dto</modelNameSuffix>
          <documentationProvider>none</documentationProvider>
          <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
          <useSpringBoot3>true</useSpringBoot3>
          <gradleBuildFile>false</gradleBuildFile>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

2. 验证JAR内容

重新打包并上传JAR后,检查JAR内是否存在ch/organisation/project/api/model/Cat.class等字节码文件。如果仅存在.kt源码,说明编译步骤未生效。

3. 检查包名一致性

注意到配置中的包名是ch.organization.project,但外部库显示为ch.organisation.project(拼写差异:organization/organisation)。Kotlin对包名大小写敏感,确保代码中引用的包名与JAR内实际包名完全一致。

4. 强制更新依赖

在依赖项目中执行以下命令,清理本地缓存并重新拉取最新JAR:

mvn clean install -U

5. 确认依赖配置正确性

确保依赖项目的pom.xml中正确引入该JAR,且Azure制品库的镜像配置在settings.xml或pom.xml中已正确设置:

<dependency>
  <groupId>ch.organisation.project</groupId>
  <artifactId>api</artifactId>
  <version>1.0.8-SNAPSHOT</version>
</dependency>

内容的提问来源于stack exchange,提问作者pkadavil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:35:57