您可以使用maven 插件中的源代码作为开发 Gradle 任务的指南。这是一个简单的起点
class SqljTask extends DefaultTask {
@Input
String encoding
@Input
List<String> additionalArgs = []
@InputDirectory
File sqljDir
@OutputDirectory
File generatedJava
@OutputDirectory
File generatedResources
@TaskAction
void generate() {
project.mkdir generatedJava
project.mkdir generatedResources
project.fileTree(sqljDir).visit { FileVisitDetails fvd ->
if (!fvd.directory) {
List<String> sqljArgs = []
sqljArgs << "-dir=$generatedJava"
sqljArgs << "-d=$generatedResources"
sqljArgs << "-encoding=$encoding"
sqljArgs << "-compile=false"
sqljArgs << fvd.file.absolutePath
sqljArgs.addAll(additionalArgs)
int result = sqlj.tools.Sql.statusMain(sqljArgs as String[])
if (result != 0) throw new RuntimeException("Can't translate $fvd.file ($returnCode)"
}
}
}
}
在 build.gradle 中的使用
apply plugin: 'java'
task sqlj(type: SqljTask) {
encoding = 'UTF-8'
additionalArgs = ['-status']
sqljDir = file('src/main/sqlj')
generatedJava = file("$buildDir/sqlj/java")
generatedResources = file("$buildDir/sqlj/resources")
}
compileJava.dependsOn sqlj
sourceSets {
main {
java {
srcDir sqlj.generatedJava
}
resources {
srcDir sqlj.generatedResources
}
}
}
注意:仅当自上次成功运行以来任务输入/输出发生更改时,此任务才会运行。UP-TO-DATE
因此,如果自上次构建以来没有任何变化,则会考虑