在 Gradle 中从 Kotlin 调用 PCEnhancerTask

Calling PCEnhancerTask from Kotlin in Gradle

我需要从 Kotlin 而非 Groovy 调用 OpenJPA PCEnhancerTask class。以下代码工作正常(基于先前记录的解决方案 here):

def openJPAClosure = {
    def entityFiles = sourceSets.main.output.classesDirs.asFileTree.matching {
        include 'com/company/persist/*Entity.class'
    }
    println "Enhancing with OpenJPA:"
    entityFiles.getFiles().each {
        println it
    }
    ant.taskdef(
            name : 'openjpac',
            classpath : sourceSets.main.runtimeClasspath.asPath,
            classname : 'org.apache.openjpa.ant.PCEnhancerTask'
    )
    ant.openjpac(
            classpath: sourceSets.main.runtimeClasspath.asPath,
            addDefaultConstructor: false,
            enforcePropertyRestrictions: true) {
        entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
    }
}

我直接在看文档on how to call Ant tasks from Gradle but I could not translate all the necessary steps using the GroovyBuilder. So instead I tough of calling the PCEnhancer

fun openJPAEnrich() {
    val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
        include("com/company/persist/*Entity.class")
    }
    println("Enhancing with OpenJPA, the following files...")
    entityFiles.getFiles().forEach() {
        println(it)
    }
    org.apache.openjpa.ant.PCEnhancerTask.main(asList(entityFiles))
}

但它抱怨无法在class路径中找到org.apache.openjpa(但它是否被列为编译依赖项)

我的问题是:

所以我结束了让它与自定义 JavaExec Gradle 任务一起工作:

tasks.create<JavaExec>("openJPAEnrich") {
    val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
        include("com/company/persist/*Entity.class")
    }
    println("Enhancing with OpenJPA, the following files...")
    entityFiles.files.forEach() {
        println(it)
    }
    classpath = sourceSets.main.get().runtimeClasspath
    main = "org.apache.openjpa.enhance.PCEnhancer"
    args(listOf("-enforcePropertyRestrictions", "true", "-addDefaultConstructor", "false"))
    entityFiles.forEach { classFile -> args?.add(classFile.toString())}

}

I was tempted to build my own custom Gradle task but for this felt overkill.

Thanks.

--Jose