【发布时间】:2018-07-21 09:11:14
【问题描述】:
我有一系列 Java 项目。它是一系列 API。我正在使用 swagger codegen 从 swagger 定义中生成大部分服务器代码。我正在使用带有委托模式的 spring-boot,所以我生成的代码都转到src/gen/java/main,我可以在src/main/java 中编写我的实现代码。生成的代码不受版本控制,而是由 maven swagger codegen 插件根据需要重新生成。这一切都很好:)
但是,当我第一次将项目导入 Eclipse 时(使用父项目上的“导入现有 maven 项目”将它们全部导入),我从生成的代码中收到一堆“未使用的函数”类型警告。 (我使用 build-helper-maven-plugin 将 src/gen/java/main 文件夹添加为源文件夹。)如果我在每个项目中选择 src/gen/java/main 文件夹,右键单击,选择属性并说忽略可选的编译问题然后这个就消失了(我也将它标记为派生资源)
问题:有没有办法在 pom 中标记这个文件夹,这样当我(或同事)将项目导入 Eclipse 时,这些设置已经在那个文件夹上设置了?或者,有什么方法可以告诉 eclipse 始终以这种方式处理具有名称(相对于项目路径)的文件夹?
其他信息
有人在评论中要求我提供 pom 文件。我做了一个相当简单的例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.api</groupId>
<artifactId>com.example.api</artifactId>
<packaging>jar</packaging>
<name>Example</name>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<springfox-version>2.7.0</springfox-version>
<swagger.codegen.version>2.4.0-SNAPSHOT</swagger.codegen.version>
<jetty-version>9.2.15.v20160210</jetty-version>
<slf4j-version>1.7.21</slf4j-version>
<junit-version>4.12</junit-version>
<servlet-api-version>2.5</servlet-api-version>
<springfox-version>2.7.0</springfox-version>
<jackson-version>2.8.9</jackson-version>
<jackson-threetenbp-version>2.6.4</jackson-threetenbp-version>
<spring-version>4.3.9.RELEASE</spring-version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<versionRange>${swagger.codegen.version}</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.api.Swagger2SpringBoot</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Needed to create swagger bits in asynch manner -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger.codegen.version}</version>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/spec/foo.yaml</inputSpec>
<modelPackage>com.example.api.models</modelPackage>
<apiPackage>com.example.api</apiPackage>
<language>spring</language>
<invokerPackage>com.example.api</invokerPackage>
<basePackage>com.example.api</basePackage>
<withXml>true</withXml>
<configOptions>
<artifactId>bookings</artifactId>
<artifactDescription>Bookings API</artifactDescription>
<title>Bookings API</title>
<artifactUrl>https://api.example.com/foo</artifactUrl>
<groupId>com.example.api</groupId>
<artifactVersion>1.0</artifactVersion>
<configPackage>com.example.api.config</configPackage>
<serializableModel>true</serializableModel>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<async>true</async>
<library>spring-boot</library>
<delegatePattern>true</delegatePattern>
<useBeanValidation>true</useBeanValidation>
<useOptional>true</useOptional>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webAppConfig>
<contextPath>/v2</contextPath>
</webAppConfig>
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
<stopPort>8079</stopPort>
<stopKey>stopit</stopKey>
<httpConnector>
<port>8002</port>
<idleTimeout>60000</idleTimeout>
</httpConnector>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--SpringFox dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
这使用了一个最小的 foo.yaml:
swagger: '2.0'
info:
title: Foo API
description: Test case
version: 1.0
host: api.example.com
basePath: /
schemes:
- https
consumes:
- application/json
produces:
- application/json
tags:
- name: foo
parameters:
message:
name: message
in: body
description: Foo
schema:
$ref: '#/definitions/Message'
required: true
definitions:
Message:
type: object
description: Foo
properties:
heading:
type: string
description: heading
body:
type: string
description: body
paths:
/foo:
post:
summary: foo
operationId: postFoo
tags:
- foo
parameters:
- $ref: '#/parameters/message'
responses:
'202':
description: Messages will be sent
default:
description: An unexpected error occurred
如果我只是 mvn clean compile 然后导入它就可以了。但是,如果我添加任何使用生成代码的实现代码,那么它不是。
例如,我添加了一个包com.example.api.implementation 到src/main/java,其中包含一个文件FooApi.java,它是:
package com.example.api.implementation;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import com.example.api.FooApiDelegate;
import com.example.api.models.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class FooApi implements FooApiDelegate {
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
public Optional<ObjectMapper> getObjectMapper() {
return Optional.ofNullable(objectMapper);
}
public Optional<HttpServletRequest> getRequest() {
return Optional.ofNullable(request);
}
@org.springframework.beans.factory.annotation.Autowired
public FooApi(ObjectMapper objectMapper, HttpServletRequest request) {
this.objectMapper = objectMapper;
this.request = request;
}
@Override
public CompletableFuture<ResponseEntity<Void>> postFoo( Message message) {
return new CompletableFuture<ResponseEntity<Void>>();
}
}
如果我现在导入,我会从我的非.生成的文件。
【问题讨论】:
-
如果插件正常工作,它应该默认生成到不受版本控制的``位于
target目录中,应该被Eclipse 等正确拾取。但是您已经写信将位置更改为src/gen/java/main? ...在简要查看插件的源代码后,您应该删除output的配置并保留默认值...这应该可以解决问题...否则请在此处添加完整的 pom... -
@khmarbaise:我的 pom 中确实有
<output>${project.basedir}</output>(和一个sourceFolder)。删除它们意味着生成的代码以target/generated-srouces/swagger/src/main/java结束,而不仅仅是src/gen/java/main但除非我将其设为源文件夹,否则我现在在 Eclipse 中会出现无法找到这些类的错误。如果我将其设为源文件夹,我会收到相同的警告 -
@khmarbaise:添加了示例 pom(以及重复问题所需的 yaml 和实现代码)
标签: java eclipse maven code-generation