制作自定义Gradle任务,生成代码,在IDE导入上运行

问题描述 投票:0回答:2

由于axis2(wsdl代码生成器)没有Gradle插件,因此我在自定义Gradle任务中调用了Ant任务。

截至目前./gradlew build生成代码,./gradlew clean删除它。此外,仅在检测到输入文件或输出目录中的更改时才生成代码。

我遇到的唯一问题是,当项目导入IDE时,代码不会自动生成。

我如何更改下面的build.gradle.kts以获得IDE(目前是IntelliJ,但我还想支持Eclipse)在导入时生成代码?


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")
    }
}
java gradle intellij-idea code-generation gradle-kotlin-dsl
2个回答
1
投票

您可以在Gradle导入或IDE制作之前/之后使用mark any task or run configuration to be activated

enter image description here


0
投票

我现在有一个有效的解决方案。 Eclipse和IntelliJ都会在导入时生成源代码。

首先,我们添加特定于IDE的插件。

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

然后我们获得相应的IDE任务,并将我们自己的任务(在val wsdl2Java中定义)添加为依赖项

// 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)

唯一的问题是显然Eclipse无法处理

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

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

但这是一个不同的问题。

UPDATE

下面的代码块告诉Eclipse包含生成的源代码

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

这告诉IntelliJ将生成的,已包含的源标记为已生成

idea {
    module {
        generatedSourceDirs.add(file("generated/src/main/java"))
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.