简体   繁体   English

编译错误:包 XXX 不存在

[英]COMPILATION ERROR : package XXX does not exist

I am using the open-api generator plugin in my java project.我在我的 java 项目中使用open-api generator plugin

For more information about the plugin, see homepage and the generator jaxrs-spec i use.有关该插件的更多信息,请参阅主页和我使用的生成器 jaxrs-spec

Files are generated under the source folder i specified, but, when i'm implementing thoses files in my project (like a generated interface), maven compiler plugin fail during the compile goal.文件在我指定的源文件夹下生成,但是,当我在我的项目中实现这些文件时(如生成的接口),maven 编译器插件在compile目标期间失败。

Here is the complete error stack trace when i'm using mvn clean compile command:这是我使用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

these files are generated in my /target folder.这些文件在我的/target文件夹中生成。 As you can see on the following screen:正如您在以下屏幕上看到的: 在此处输入图像描述

Here is a part of my pom.xml where i registered the openapi-generator-maven-plugin in the <build> section:这是我的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>

Here is my controller src/main/java/com/example/controller/BasicAuth.java in which I implement the generated interface and in which the errors are located:这是我的控制器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 
    }
}

Here is the interface target/generated-sources/openapi/src/gen/java/com/example/api/UsersApi.java :这是接口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();
}

I don't know what's going wrong here.我不知道这里出了什么问题。 My IDE does not report any errors, although I know that the IDE cannot be fully trusted.我的 IDE 没有报告任何错误,尽管我知道 IDE 不能完全信任。

Finally, my application can be started correctly via the IDE, but the controller endpoint for my GET method is not the one defined in the UsersApi interface.最后,我的应用程序可以通过 IDE 正确启动,但是我的GET方法的控制器端点不是UsersApi接口中定义的端点。 The path is not api/v1/retrieve-pwd , but api/v1 .路径不是api/v1/retrieve-pwd ,而是api/v1

This behavior is also strange.这种行为也很奇怪。 Maybe by overloading the method in my controller the path can be modified, but, the path is not defined on the method but on the interface.也许通过在我的控制器中重载方法可以修改路径,但是,路径不是在方法上定义的,而是在接口上定义的。

I await your comments or feedback and thank you in advance我等待您的意见或反馈,并在此先感谢您

Problem was generated files: they were not compiled.问题是生成的文件:它们没有被编译。 This explain errors messages.这解释了错误消息。

Solution was adding a maven helper plugin:解决方案是添加一个 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