Skip to content

Commit 009634e

Browse files
authored
Merge pull request #6 from hauner/#5
resolves #5
2 parents 308d64c + 4f09c0e commit 009634e

File tree

9 files changed

+499
-108
lines changed

9 files changed

+499
-108
lines changed

README.md

Lines changed: 177 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,184 @@
66
a gradle plugin based on the [openapi-generatr-api][generatr-api] to handle all configured openapi-generatrs
77
without explicit dependency on a generatr. Requires Gradle 5.2 or better.
88

9-
# Usage
9+
# gradle dsl
1010

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].
1213

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
14187

15188
See [`openapi-generatr-spring-mvc-sample`][generatr-spring-mvc] for a complete spring boot sample project.
16189

@@ -27,5 +200,6 @@ The plugin at the [plugin portal][generatr-plugin].
27200

28201
[generatr-api]: https://github.com/hauner/openapi-generatr-api
29202
[generatr-spring]: https://github.com/hauner/openapi-generatr-spring
203+
[generatr-json]: https://github.com/hauner/openapi-generatr-json
30204
[generatr-spring-mvc]: https://github.com/hauner/openapi-generatr-spring-mvc-sample
31205
[generatr-spring-gradle]: https://hauner.github.io/openapi-generatr-spring/gradle.html

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'com.github.hauner.openapi'
9-
version '1.0.0.M4'
9+
version '1.0.0.M5'
1010

1111
targetCompatibility = JavaVersion.VERSION_1_8
1212

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.gradle
18+
19+
import org.gradle.api.file.FileCollection
20+
21+
/**
22+
* represents a generatr configured in {@link OpenApiGeneratrExtension}
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class Generatr {
27+
public static final String API_PATH = 'apiPath'
28+
public static final String TARGET_DIR = 'targetDir'
29+
30+
String name
31+
def generatrLib // String | FileCollection...
32+
33+
Map<String, Object> other = [:]
34+
35+
Generatr (String name) {
36+
this.name = name
37+
}
38+
39+
void generatr (FileCollection fc) {
40+
generatrLib = fc
41+
}
42+
43+
void generatr (String dep) {
44+
generatrLib = dep
45+
}
46+
47+
void targetDir (String targetDir) {
48+
other.put (TARGET_DIR, targetDir)
49+
}
50+
51+
void targetDir (GString targetDir) {
52+
other.put (TARGET_DIR, targetDir.toString ())
53+
}
54+
55+
String getTargetDir () {
56+
other.get (TARGET_DIR)
57+
}
58+
59+
void apiPath (String apiPath) {
60+
other.put (API_PATH, apiPath)
61+
}
62+
63+
void apiPath (GString apiPath) {
64+
other.put (API_PATH, apiPath.toString ())
65+
}
66+
67+
boolean hasApiPath () {
68+
other.containsKey (API_PATH)
69+
}
70+
71+
String getApiPath () {
72+
other.get (API_PATH)
73+
}
74+
75+
void setApiPath (String path) {
76+
other.put (API_PATH, path)
77+
}
78+
79+
def methodMissing (String name, def args) {
80+
if (args[0] instanceof Closure) {
81+
def builder = new MapBuilder()
82+
builder.with (args[0] as Closure)
83+
other.put (name, builder.get ())
84+
} else {
85+
other.put (name, args[0])
86+
}
87+
}
88+
89+
}

src/main/groovy/com/github/hauner/openapi/gradle/MapBuilder.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class MapBuilder {
8585
@Override
8686
void setProperty(String propertyName, Object value) {
8787
if (props.containsKey (propertyName)) {
88-
log.warn ("replacing property {} with value {} to value {}!", propertyName, props.get (propertyName), value)
88+
log.warn ("replacing property {} with value {} to value {}!",
89+
propertyName, props.get (propertyName), value)
8990
}
9091

9192
props.put (propertyName, value)

0 commit comments

Comments
 (0)