繁体   English   中英

编译错误:包 XXX 不存在

[英]COMPILATION ERROR : package XXX does not exist

我在我的 java 项目中使用open-api generator plugin

有关该插件的更多信息,请参阅主页和我使用的生成器 jaxrs-spec

文件在我指定的源文件夹下生成,但是,当我在我的项目中实现这些文件时(如生成的接口),maven 编译器插件在compile目标期间失败。

这是我使用mvn clean compile命令时的完整错误堆栈跟踪:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/****/nba-pistache/src/main/java/com/example/controller/BasicAuth.java:[3,23] package com.example.api does not exist
[ERROR] /C:/Users/****/nba-pistache/src/main/java/com/example/controller/BasicAuth.java:[13,26] cannot find symbol
  symbol:   class RestResourceRoot
  location: package com.example
[ERROR] /C:/Users/****/nba-pistache/src/main/java/com/example/controller/BasicAuth.java:[13,1] static import only from classes and interfaces
[ERROR] /C:/Users/****/nba-pistache/src/main/java/com/example/controller/BasicAuth.java:[16,35] cannot find symbol
  symbol: class UsersApi
[ERROR] /C:/Users/****/nba-pistache/src/main/java/com/example/controller/BasicAuth.java:[15,7] cannot find symbol
  symbol: variable APPLICATION_PATH

这些文件在我的/target文件夹中生成。 正如您在以下屏幕上看到的: 在此处输入图像描述

这是我的pom.xml的一部分,我在<build>部分注册了openapi-generator-maven-plugin

  <plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.2.1</version>
    <executions>
      <execution>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <inputSpec>${project.basedir}/src/main/resources/pistache-api.yaml</inputSpec>
          <generatorName>jaxrs-spec</generatorName>
          <apiPackage>com.example.api</apiPackage>
          <modelPackage>
            com.example.api.model
          </modelPackage>
          <configOptions>
            <useSwaggerAnnotations>false</useSwaggerAnnotations>
            <library>quarkus</library>
            <interfaceOnly>true</interfaceOnly>
            <dateLibrary>java8</dateLibrary>
            <useTags>true</useTags>
            <useBeanValidation>false</useBeanValidation>
            <returnResponse>true</returnResponse>
          </configOptions>
        </configuration>
      </execution>
    </executions>
  </plugin>

这是我的控制器src/main/java/com/example/controller/BasicAuth.java ,我在其中实现了生成的接口,错误也位于其中:

package com.example.controller;

import com.example.api.UsersApi;
import com.example.client.RestClientBasicAuth;
import com.example.config.BasicUserConfig;
import org.eclipse.microprofile.rest.client.inject.RestClient;


import javax.inject.Inject;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import static com.example.RestResourceRoot.APPLICATION_PATH;

@Path(APPLICATION_PATH)
public class BasicAuth implements UsersApi {

    @Override
    public Response retrievePwdGet() {
       // my custom implementation 
    }
}

这是接口target/generated-sources/openapi/src/gen/java/com/example/api/UsersApi.java

package com.example.api;


import javax.ws.rs.*;
import javax.ws.rs.core.Response;


import java.io.InputStream;
import java.util.Map;
import java.util.List;


@Path("/retrieve-pwd")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2022-12-10T18:01:45.461936200+01:00[Europe/Paris]")
public interface UsersApi {

    @GET
    @Produces({ "application/json" })
    Response retrievePwdGet();
}

我不知道这里出了什么问题。 我的 IDE 没有报告任何错误,尽管我知道 IDE 不能完全信任。

最后,我的应用程序可以通过 IDE 正确启动,但是我的GET方法的控制器端点不是UsersApi接口中定义的端点。 路径不是api/v1/retrieve-pwd ,而是api/v1

这种行为也很奇怪。 也许通过在我的控制器中重载方法可以修改路径,但是,路径不是在方法上定义的,而是在接口上定义的。

我等待您的意见或反馈,并在此先感谢您

问题是生成的文件:它们没有被编译。 这解释了错误消息。

解决方案是添加一个 Maven 助手插件:

 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/newsrc/</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

暂无
暂无

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

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