由于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")
}
}
您可以在Gradle导入或IDE制作之前/之后使用mark any task or run configuration to be activated:
我现在有一个有效的解决方案。 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"))
}
}