Search code examples
javagradleintellij-ideacode-generationgradle-kotlin-dsl

Make custom Gradle task, that generates code, run on IDE import


Since there is no Gradle plugin for axis2 (a wsdl code generator), I called an Ant task in a custom Gradle task.

As of now ./gradlew build generates the code, and ./gradlew clean deletes it. Also, the code is only generated if changes in the input file(s) or in the output directory are detected.

The only problem I'm having is that the code is not generated automatically when the project is imported into an IDE.

How do I need to change the build.gradle.kts below in order to have the IDEs (currently IntelliJ, but I would also like support for Eclipse) generate the code on import?


plugins {
    id("base") // needed for delete
}

val axis2 by configurations.creating

dependencies {
    axis2("org.apache.axis2:axis2-ant-plugin:$axis2Version")
    axis2("org.apache.axis2:axis2-xmlbeans:$axis2Version")
}


val wsdl2Java by tasks.registering {
    group = "build"
    description = "Creates Java classes and resources from WSDL schema."

    inputs.files(fileTree("$projectDir/src/main/resources/wsdl"))
    outputs.dir("$projectDir/generated/")

    doLast {
        ant.withGroovyBuilder {
            "echo"("message" to "Generating Classes from WSDL!")
            "taskdef"("name" to "codegen", "classname" to "org.apache.axis2.tool.ant.AntCodegenTask", "classpath" to axis2.asPath)
            "codegen"(
                    "wsdlfilename" to "$projectDir/src/main/resources/wsdl/MP12N-H-HOST-WEB-SOAP.wsdl",
                    "output" to "$projectDir/generated/",
                    "targetSourceFolderLocation" to "src/main/java",
                    "targetResourcesFolderLocation" to "src/main/resources",
                    "packageName" to "de.hanel.com.jws.main",
                    "databindingName" to "xmlbeans")
        }
    }
}


val deleteGenerated by tasks.registering(Delete::class) {
    delete("$projectDir/generated/")
}

tasks {
    compileJava {
        dependsOn(wsdl2Java)
    }

    clean {
        dependsOn(deleteGenerated)
    }
}

java {
    sourceSets["main"].java {
        srcDir("generated/src/main/java")
    }

    sourceSets["main"].resources {
        srcDir("generated/src/main/resources")
    }
}

Solution

  • I have a working solution now. Both Eclipse and IntelliJ generate the source code on import.

    First we add the IDE-specific plugins.

    apply {
        plugin("idea")
        plugin("eclipse")
    }
    

    Then we get the corresponding IDE tasks and add our own task, that was defined in val wsdl2Java, as dependency

    // find by name (in tasks container), since a module is also called 'idea'
    project.tasks.findByName("idea")?.dependsOn(wsdl2Java)
    project.tasks.findByName("eclipse")?.dependsOn(wsdl2Java)
    

    The only problem is that apparently Eclipse can't handle

    java {
        sourceSets["main"].java {
            srcDir("generated/src/main/java")
        }
    
        sourceSets["main"].resources {
            srcDir("generated/src/main/resources")
        }
    }
    

    But that's a different question.

    UPDATE

    The code block below tells Eclipse to include the generated sources

    eclipse {
        classpath {
            plusConfigurations.add(configurations.findByName("compile"))
        }
    }
    

    and this tells IntelliJ to mark the generated, and already included, sources as generated

    idea {
        module {
            generatedSourceDirs.add(file("generated/src/main/java"))
        }
    }