This message was deleted.
# community-support
s
This message was deleted.
s
Also, I was considering setting things up like @Vampire suggests here, i.e. that the
srcDir
is not explicitly sets but taken from the output of the generate step, but can’t get it to work. Here is my attempt, but I get stuck in how to get the output dir from the
TaskProvider<GenerateTask>
v
Actually with the OpenAPI Generator Gradle plugin it is a bit compilcated. Because the
java
generator does by default not just produce the Java sources, but a full Gradle build. It is probably more targeted to being one-time generated and then checked in which I greatly dislike. But due to that, the whole generated Gradle build is the output of the task and not just the sources you are interested in. If the
GenerateTask
would have the proper outputs, i.e. the root directory of the sources files, it would work much better. It then just is
Copy code
diff --git a/build.gradle b/build.gradle
index 1e8bb35..3c55eb7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -65,13 +65,13 @@ jacocoTestReport {
 }

 openApiValidate {
-       inputSpec = 'openapi.yaml'
+       inputSpec = file('openapi.yaml').absolutePath
 }

 check.dependsOn tasks.openApiValidate

 def generateHahabitApi = tasks.register('generateHahabitApi', GenerateTask) {
-       inputSpec = 'openapi.yaml'
+       inputSpec = file('openapi.yaml').absolutePath
        generatorName = 'java'
        outputDir = "${buildDir}/generated/sources/openapi"
        configOptions = [
@@ -80,13 +80,10 @@ def generateHahabitApi = tasks.register('generateHahabitApi', GenerateTask) {
        ]
 }

-compileTestJava.dependsOn generateHahabitApi
-
 sourceSets {
        test {
                java {
-                       // Wbat to put here? Can't do `generateHahabitApi.outputDir` since `generateHahabitApi` is a task provider.
-                       srcDir "${generateHahabitApi}/src/main/java"
+                       srcDir generateHahabitApi
                }
        }
 }
Actually you can even do that, but it is a bit unclean, as the the sources do not get properly set up in the IDE, and you also have the generated tests of the generated project in your classpath, namely all
.java
files in that generated project. So to have it a bit cleaner with the OpenAPI Generator plugin, it would more be like this to only generate the files you really want and have them cleanly integrated in the build and IDE:
Copy code
diff --git a/.openapi-generator-ignore b/.openapi-generator-ignore
new file mode 100644
index 0000000..a42e0e8
--- /dev/null
+++ b/.openapi-generator-ignore
@@ -0,0 +1,2 @@
+/build/generated/sources/openapi/main/java/**
+!build/generated/sources/openapi/main/java/org/**
diff --git a/build.gradle b/build.gradle
index 1e8bb35..67a4543 100644
--- a/build.gradle
+++ b/build.gradle
@@ -65,28 +65,31 @@ jacocoTestReport {
 }

 openApiValidate {
-       inputSpec = 'openapi.yaml'
+       inputSpec = file('openapi.yaml').absolutePath
 }

 check.dependsOn tasks.openApiValidate

-def generateHahabitApi = tasks.register('generateHahabitApi', GenerateTask) {
-       inputSpec = 'openapi.yaml'
+openApiGenerate {
+       inputSpec = file('openapi.yaml').absolutePath
        generatorName = 'java'
-       outputDir = "${buildDir}/generated/sources/openapi"
+       outputDir = "${buildDir}/generated/sources/openapi/main/java"
+       generateApiDocumentation = false
+       generateApiTests = false
+       generateModelDocumentation = false
+       generateModelTests = false
        configOptions = [
+               sourceFolder: '',
                library: 'native',
                useJakartaEe: 'true'
        ]
+       ignoreFileOverride = file(".openapi-generator-ignore").absolutePath
 }

-compileTestJava.dependsOn generateHahabitApi
-
 sourceSets {
        test {
                java {
-                       // Wbat to put here? Can't do `generateHahabitApi.outputDir` since `generateHahabitApi` is a task provider.
-                       srcDir "${generateHahabitApi}/src/main/java"
+                       srcDir tasks.openApiGenerate
                }
        }
 }
After you did these changes, you should once manually delete the
build/generated/sources/openapi
directory, or run
clean
task, as the directory is not wiped automatically with the new setup.
🙌 3
Lucky for you I did exactly this in a project not long ago, so could copy over some setup for you 🙂
s
Very cool, thanks! I’ll try this later tonight hopefully and report back. It does make sense in any case to not generate a bunch of stuff you don’t need or want…