在命令行中传递 gradle 任务参数

Passing a gradle task arguments in command line

我正在尝试找到从命令行传递 gradle 任务参数的最佳方法。 我有这个任务。我想从学生练习中解压缩解决方案并将它们复制到项目中的正确位置以评估它们。我这样称呼这个任务:

> gradle swapSolution -Pstudent=MyStudent -Pexercise=ex05

我在启用 Gradle 插件的情况下在 IntelliJ 中执行此操作时遇到的一个问题是我在构建项目时收到此错误消息。有什么解决方案?

A problem occurred evaluating root project 'kprog-2020-ws'.
> Could not get unknown property 'student' for root project 'kprog-2020-ws' of type org.gradle.api.Project.

这是 gradle 任务:

task swapSolution(type: Copy) {
    new File("${rootDir}/Abgaben").eachDir { file ->
        if (file.name.toString().matches("(.*)" + project.property("student") + "(.*)")) {
            def exDir = new File("/src/main/java/prog/" + project.property("exercise"))
            if (!exDir.exists()) {
                delete exDir
            }
            new File(file.path).eachFile { zipSolution ->
                //def zipFile = new File("./Abgaben/" + file.name.toString() + "/" + project.property("exercise") + "Solution.zip")
                from zipTree(zipSolution)
                into "/src/main/java/"
            }

        }
    }
}

您对优化这个流程有什么建议吗?

-P denotes the Gradle Project Property. If you need to use project properties you can specify it as a system property in gradle.properties 项目根目录下的文件。

如果您的任务是 JavaExec 类型,您可以使用 --args 开关并将其传递到 Gradle Task Run ConfigurationArguments 文本字段中任务名称如 swapSolution -args="-student=MyStudent -exercise=ex05"。也可以看看