001/* 002 * Copyright 2017-2021 original authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.micronaut.maven.openapi; 017 018import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder; 019import org.apache.maven.plugins.annotations.LifecyclePhase; 020import org.apache.maven.plugins.annotations.Mojo; 021import org.apache.maven.plugins.annotations.Parameter; 022 023/** 024 * Generates an OpenAPI server. 025 * The sources are generated in the target directory. 026 */ 027@Mojo(name = OpenApiServerMojo.MOJO_NAME, defaultPhase = LifecyclePhase.GENERATE_SOURCES) 028public class OpenApiServerMojo extends AbstractOpenApiMojo { 029 030 public static final String MOJO_NAME = "generate-openapi-server"; 031 032 private static final String SERVER_PREFIX = MICRONAUT_OPENAPI_PREFIX + ".server."; 033 034 /** 035 * The package name of the controller if controller implementation files are generated. 036 */ 037 @Parameter(property = SERVER_PREFIX + "controller.package.name", defaultValue = IO_MICRONAUT_OPENAPI_PREFIX + ".controller.package.name", required = true) 038 protected String controllerPackageName; 039 040 /** 041 * Whether to generate authentication annotations for APIs. 042 */ 043 @Parameter(property = SERVER_PREFIX + "use.auth", defaultValue = "false") 044 protected boolean useAuth; 045 046 /** 047 * Determines if the server should use lombok. 048 * 049 * @since 4.2.2 050 */ 051 @Parameter(property = SERVER_PREFIX + "lombok") 052 protected boolean lombok; 053 054 /** 055 * Determines if the server should use flux for arrays. 056 * 057 * @since 4.2.2 058 */ 059 @Parameter(property = SERVER_PREFIX + "flux.for.arrays") 060 protected boolean fluxForArrays; 061 062 /** 063 * If set to true, the `javax.annotation.Generated` annotation will be added to all generated classes. 064 * 065 * @since 4.2.2 066 */ 067 @Parameter(property = SERVER_PREFIX + "generated.annotation", defaultValue = "true") 068 protected boolean generatedAnnotation; 069 070 /** 071 * If set to true, the generated code should be made compatible with Micronaut AOT. 072 * 073 * @since 4.2.2 074 */ 075 @Parameter(property = SERVER_PREFIX + "aot.compatible") 076 protected boolean aotCompatible; 077 078 /** 079 * The property that defines if this mojo should be used in configuration. 080 */ 081 @Parameter(property = MICRONAUT_OPENAPI_PREFIX + ".generate.server") 082 protected boolean enabled; 083 084 @Override 085 protected boolean isEnabled() { 086 return enabled; 087 } 088 089 @Override 090 protected void configureBuilder(MicronautCodeGeneratorBuilder builder) { 091 if ("kotlin".equalsIgnoreCase(lang)) { 092 builder.forKotlinServer(spec -> spec 093 .withControllerPackage(controllerPackageName) 094 .withAuthentication(useAuth) 095 .withAot(aotCompatible) 096 // we don't want these to be configurable in the plugin for now 097 .withGenerateImplementationFiles(false) 098 .withGenerateControllerFromExamples(false) 099 .withGenerateOperationsToReturnNotImplemented(false) 100 .withGeneratedAnnotation(generatedAnnotation) 101 .withFluxForArrays(fluxForArrays) 102 .withKsp(ksp) 103 ); 104 } else if ("java".equalsIgnoreCase(lang)) { 105 builder.forJavaServer(spec -> spec 106 .withControllerPackage(controllerPackageName) 107 .withAuthentication(useAuth) 108 .withAot(aotCompatible) 109 // we don't want these to be configurable in the plugin for now 110 .withGenerateImplementationFiles(false) 111 .withGenerateControllerFromExamples(false) 112 .withGenerateOperationsToReturnNotImplemented(false) 113 .withGeneratedAnnotation(generatedAnnotation) 114 .withFluxForArrays(fluxForArrays) 115 .withLombok(lombok) 116 ); 117 } else { 118 throw new UnsupportedOperationException("Unsupported language: " + lang); 119 } 120 } 121}