ciscoo / cxf-codegen-gradle

Gradle plugin to generate Java artifacts from WSDL
Apache License 2.0
28 stars 6 forks source link

Incompatibility with Micronaut Application plugin #7

Closed jimmymain closed 2 years ago

jimmymain commented 3 years ago

Where: Build file 'D:\Development\Projects\Zilch\issuer-service\build.gradle' line: 121

* What went wrong:
A problem occurred evaluating root project 'issuer-service'.
> Could not create task ':wsdl2javaGps'.
   > 'wsdl' property is not present

build.gradle has:

plugins {
    id "io.micronaut.application" version "1.3.4"
    id "io.mateo.cxf-codegen" version "1.0.0-rc.3"
    id "com.github.johnrengelman.shadow" version "6.1.0"
}

and later on:

cxfCodegen {
    wsdl2java {
        gps {
            wsdl = file("./src/main/resources/wsdl/service.wsdl")
        }
    }
}

The property seems present and correct.

ciscoo commented 3 years ago

I was able to reproduce on my Windows 10 machine with the following:

plugins {
    id("io.mateo.cxf-codegen") version "1.0.0-rc.3"
    id("io.micronaut.application") version "1.3.4"
}

repositories {
    mavenCentral()
    // https://github.com/grails/grails-core/issues/11825
    maven {
        url = uri("https://repo.grails.org/artifactory/core")
    }
}

dependencies {
    cxfCodegen("jakarta.xml.ws:jakarta.xml.ws-api:2.3.3")
    cxfCodegen("jakarta.annotation:jakarta.annotation-api:1.3.5")
}

application {
    mainClass.set("com.example.Example")
}

cxfCodegen {
    wsdl2java {
        register("gps") {
            wsdl.set(file("src/main/resources/wsdl/service.wsdl"))
        }
    }
}

If you remove the Micronaut plugin, it works:

plugins {
    id("io.mateo.cxf-codegen") version "1.0.0-rc.3"
-   id("io.micronaut.application") version "1.3.4"
}

I have not worked with Micronaut so I will need to investigate what the various Micronaut plugins are doing to cause the wsdl property to be unset.

From my brief testing, you do not need to execute any wsdl2java tasks for the error to show which is not clear to me why.

jimmymain commented 3 years ago

I will try this again later. I wonder if it works if the cxf plugin is after the micronaut plugin? It's worth a try, if this is a problem in micronaut, I will happily log the issue there.

ciscoo commented 3 years ago

I think I narrowed it down to these lines from io.micronaut.application:

https://github.com/micronaut-projects/micronaut-gradle-plugin/blob/v1.5.1/src/main/java/io/micronaut/gradle/MicronautApplicationPlugin.java#L109...L146

More specifically this line: https://github.com/micronaut-projects/micronaut-gradle-plugin/blob/v1.5.1/src/main/java/io/micronaut/gradle/MicronautApplicationPlugin.java#L109

tasks.withType(JavaExec.class, javaExec -> { ... })

That is equivalent to:

tasks.withType(JavaExec.class).all(...)

What I think is happening is that the Micronaut Application plugin is causing the wsdl2java tasks to be realized. Per the Gradle docs (point 7):

Eagerly realizing some tasks may cause a cascade of other tasks to be realized.

Since the Micronaut Application plugin is targeting all JavaExec types, Wsdl2JavaTask types are unfortunately being targeted/configured as well since they are a subclass of JavaExec.

With all of that said, my testing was as follows:

  1. Hack together a test that fails: https://github.com/ciscoo/cxf-codegen-gradle/commit/c9c05cb4ea52049ec53422846440194d9e576f07
  2. Clone the Micronaut Application plugin repo and publish 9.9.9-SNAPSHOT locally
  3. Observe test from (1) fail (passes assertion)
  4. Modify the aforementioned line above and republish locally:
- tasks.withType(JavaExec.class, javaExec -> { ... })
+ tasks.withType(JavaExec.class).configureEach(javaExec -> { ... })
  1. Rerun failing test and it no longer fails for 'wsdl' property is not present

IMO, the Micronaut Application plugin should be more selective as to which JavaExec task it needs to customize. It adds additional dependencies to wsdl2java's classpath which really are not needed and configures other items that are not needed for the wsdl2java tool.

However, I think it is unfair to place the issue solely on Micronaut here. There may be an issue within the cxf-codegen plugin that I am not seeing. I'll need to put together a build scan to get deeper insights as noted in the troubleshooting docs

ciscoo commented 3 years ago

I think I may have a fix:

https://github.com/ciscoo/cxf-codegen-gradle/blob/v1.0.0-rc.3/cxf-codegen-gradle/src/main/java/io/mateo/cxf/codegen/CxfCodegenPlugin.java#L104

@@ -101,7 +101,10 @@ public class CxfCodegenPlugin implements Plugin<Project> {
                                task.setClasspath(configuration.get());
                                task.setGroup(WSDL2JAVA_GROUP);
                                task.setDescription("Generates Java sources for '" + option.getName() + "'");
-                               task.setArgs(option.generateArgs());
+                               // Generate arguments at the very last moment to avoid realization issues.
+                               task.doFirst("generateArgsFor" + name, (exec) -> {
+                                       ((Wsdl2JavaTask) exec).setArgs(option.generateArgs());
+                               });
                        });
                });
        }

My brief testing shows that this works with the Micronaut Application plugin. I'll need to do more testing to make sure.

I am also not sure if this should be considered a bug or an enhancement

jimmymain commented 3 years ago

Thanks, that looks awesome, especially as I was thinking the issue might actually be with the micronaut plugin

ciscoo commented 3 years ago

Unfortunately the fix for this will need to be reverted since it had undesired consequences. Trying to resolve #13 leads to the same issue as this one, so the fix isn't really a fix.

ciscoo commented 2 years ago

This has been fixed with https://github.com/ciscoo/cxf-codegen-gradle/issues/25 and tests show that it now works with the Micronaut plugin. Please give the latest snapshot a try; migration guide here.