6
6
a gradle plugin based on the [ openapi-generatr-api] [ generatr-api ] to handle all configured openapi-generatrs
7
7
without explicit dependency on a generatr. Requires Gradle 5.2 or better.
8
8
9
- # Usage
9
+ # gradle dsl
10
10
11
- See [ ` Using Gradle ` ] [ generatr-spring-gradle ] in the documentation of [ openapi-generatr-spring] [ generatr-spring ] .
11
+ For a more detailed description see [ ` Using Gradle ` ] [ generatr-spring-gradle ] in the documentation of
12
+ [ openapi-generatr-spring] [ generatr-spring ] .
12
13
13
- # Sample project
14
+ The plugin adds a new configuration block ` openapiGeneratr ` to the gradle project. Each generatr is
15
+ configured by a nested configuration block.
16
+
17
+ Apart from that there is only a single option that is recognized inside the configuration block:
18
+
19
+ * ` apiPath ` , which defines the path to the openapi yaml file. This is usually the same for all
20
+ generatrs and placing it directly into the the ` openapiGeneratr ` block sets it for all generatrs.
21
+
22
+ To configure a generatr, for example the [ openapi spring generatr] [ generatr-spring ] , a ` spring `
23
+ configuration is placed into the the ` openapiGeneratr ` block. The name of the configuration is
24
+ used to create a gradle task ` generate<Name> ` to run the corresponding generatr.
25
+
26
+
27
+ openapiGeneratr {
28
+
29
+ // the path to the open api yaml file.
30
+ apiPath "${projectDir}/src/api/openapi.yaml"
31
+
32
+ spring {
33
+ ... options of the spring generatr
34
+ }
35
+
36
+ }
37
+
38
+
39
+ In case the json generatr is needed it is added in the same way:
40
+
41
+
42
+ openapiGeneratr {
43
+
44
+ // the path to the open api yaml file.
45
+ apiPath "${projectDir}/src/api/openapi.yaml"
46
+
47
+ spring {
48
+ ... options of the spring generatr
49
+ }
50
+
51
+ json {
52
+ ... options of the json generatr
53
+ }
54
+
55
+ }
56
+
57
+
58
+ The configuration of a single generatr has a few pre-defined properties and it can have any number of
59
+ additional parameters defined by the generatr (all options are passed in a map to the generatr with
60
+ the option name as the key).
61
+
62
+ * ` generatr ` (mandatory): the ` generatr ` dependency. Uses the same dependency notations allowed in the
63
+ gradle ` dependencies ` block.
64
+
65
+ The generatr library is configured here to avoid any side effect on the build dependencies of the
66
+ project.
67
+
68
+ Example using the preferred shortcut nation:
69
+
70
+ spring {
71
+ generatr 'com.github.hauner.openapi:openapi-generatr-spring:1.0.0.M7'
72
+ }
73
+
74
+ or like this to use an un-published generatr:
75
+
76
+ spring {
77
+ generatr files('... path to generatr jar')
78
+ }
79
+
80
+
81
+ * ` apiPath ` (optional): the path to the open api yaml file. If set inside a generatr configuration it
82
+ overrides the parent ` apiPath ` .
83
+
84
+ * ` targetDir ` (mandatory): the target folder for the generatr. The generatr will write its output to
85
+ this directory.
86
+
87
+ # gradle tasks
88
+
89
+ The plugin creates a single gradle task for each generatr configuration that will run the corresponding
90
+ generatr. The name is derived from the generatr name: ` generate<Name> ` .
91
+
92
+
93
+ The plugin does not add the ` generate<Name> ` task to the build lifecycle. To run it automatically
94
+ add a task dependency in the ` build.gradle ` file. For example to run generatr-spring before compiling
95
+
96
+ // generate api before compiling
97
+ compileJava.dependsOn ('generateSpring')
98
+
99
+ and to run generatr-json when processing the resources:
100
+
101
+ processResources.dependsOn ('generateJson')
102
+
103
+
104
+ # using the generatr output
105
+
106
+ In case the generatr creates java sources it is necessary to compile them as part of the build process.
107
+
108
+ For example to compile the java source files created by generatr-spring add the ` targetDir ` of the
109
+ generatr to the java ` sourceSets ` :
110
+
111
+ // add the targetDir of the generatr as additional source folder to java.
112
+ sourceSets {
113
+ main {
114
+ java {
115
+ // add generated files
116
+ srcDir 'build/openapi'
117
+ }
118
+ }
119
+ }
120
+
121
+ To add the json file created by the generatr-json to the final artifact jar as resource add the
122
+ ` targetDir ` of the generatr to the java ` resources ` source set:
123
+
124
+
125
+ // add the targetDir of the generatr as additional resource folder.
126
+ sourceSets {
127
+ main {
128
+ resources {
129
+ srcDir "$buildDir/json"
130
+ }
131
+ }
132
+ }
133
+
134
+
135
+ # configuration example
136
+
137
+ Here is a full example using the generatrs [ spring] [ generatr-spring ] & [ json] [ generatr-json ] :
138
+
139
+ openapiGeneratr {
140
+
141
+ // the path to the open api yaml file. Usually the same for all generatrs.
142
+ //
143
+ apiPath "${projectDir}/src/api/openapi.yaml"
144
+
145
+ // based on the name of a generatr configuration the plugin creates a gradle task with name
146
+ // "generate${name of generator}" (in this case "generateSpring") to run the generatr.
147
+ //
148
+ spring {
149
+ // the spring generatr dependency (mandatory)
150
+ //
151
+ generatr 'com.github.hauner.openapi:openapi-generatr-spring:1.0.0.M7'
152
+
153
+ // setting api path inside a generatr configuration override the one at the top.
154
+ //
155
+ // apiPath "${projectDir}/src/api/openapi.yaml"
156
+
157
+ // the destination folder for generating interfaces & models. This is the parent of the
158
+ // {package-name} folder tree configured in the mapping file. (mandatory)
159
+ //
160
+ targetDir "$projectDir/build/openapi"
161
+
162
+ //// generatr-spring specific options
163
+
164
+ // file name of the mapping yaml configuration file. Note that the yaml file name must end
165
+ // with either {@code .yaml} or {@code .yml}.
166
+ //
167
+ mapping "$projectDir/openapi-generatr-spring.yaml"
168
+
169
+ // show warnings from the open api parser.
170
+ showWarnings true
171
+ }
172
+
173
+ // applying the rule described above the task to run this one is "generateJson".
174
+ //
175
+ json {
176
+ // the json generatr dependency (mandatory)
177
+ //
178
+ generatr 'com.github.hauner.openapi:openapi-generatr-json:1.0.0.M2'
179
+
180
+ // the destination folder for the json file. (mandatory)
181
+ targetDir "$buildDir/json"
182
+ }
183
+
184
+ }
185
+
186
+ # sample project
14
187
15
188
See [ ` openapi-generatr-spring-mvc-sample ` ] [ generatr-spring-mvc ] for a complete spring boot sample project.
16
189
@@ -27,5 +200,6 @@ The plugin at the [plugin portal][generatr-plugin].
27
200
28
201
[ generatr-api ] : https://github.com/hauner/openapi-generatr-api
29
202
[ generatr-spring ] : https://github.com/hauner/openapi-generatr-spring
203
+ [ generatr-json ] : https://github.com/hauner/openapi-generatr-json
30
204
[ generatr-spring-mvc ] : https://github.com/hauner/openapi-generatr-spring-mvc-sample
31
205
[ generatr-spring-gradle ] : https://hauner.github.io/openapi-generatr-spring/gradle.html
0 commit comments