Xen*_*dar 5 java maven swagger swagger-codegen
我在一个项目中使用 swagger-codegen ,最后我问自己一个问题:swagger 文件的位置是否有任何约定?
这是我的情况:
我在我的项目中使用 Maven,所以我有标准的 Maven 结构:
pom.xml
src
|--main
|--java
|--resources
|--webapp
|--test
|--java
|--resources
Run Code Online (Sandbox Code Playgroud)
为了使用 Swagger 生成我的 REST API,我必须将 swagger.json、一些 mustache 模板和 .swagger-codegen-ignore 放在某处。我已经把它们(自然地?)放在 src/main/resources 中:
resources
|--api
|--v2
|--swagger.json
|--swaggerconfig
|--api.mustache
|--[...]
|--.swagger-codegen-ignore
Run Code Online (Sandbox Code Playgroud)
我已经使用适当的 swagger-codegen-maven-plugin 配置了我的 pom.xml 并将打包设置为 war,以便生成我的战争(以便将其部署在 Tomcat 中)。摘录:
[...]
<packaging>war</packaging>
[...]
<build>
<finalName>my-project</finalName>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/api/v2/swagger.json</inputSpec>
<language>jaxrs</language>
<templateDirectory>${basedir}/src/main/resources/swaggerconfig</templateDirectory>
<addCompileSourceRoot>true</addCompileSourceRoot>
<configOptions>
<library>jersey2</library>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<!-- .swagger-codegen-ignore must be located at the root as per documentation -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/generated-sources/swagger</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>.swagger-codegen-ignore</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
在mvn install我的 swagger 生成的文件生成并成功编译后,war 生成并且工作正常。
但是,在生成的战争中,在 WEB-INF/classes 文件夹中,我找到了我的 api 和 swaggerconfig 文件夹,以及 .swagger-codegen-ignore。嗯,这是正常的,因为它们被打包在 src/main/resources 中。
长话短说:我应该把这些文件放在其他地方(比如 src/main/swagger 这样 Maven 默认会找到它们)?我应该在 maven-war-plugin 中编写一些包含/排除规则(但我觉得这不是一个好习惯)?我在 swagger Github 上没有找到明确的文档。
谢谢
| 归档时间: |
|
| 查看次数: |
5518 次 |
| 最近记录: |