1

I'm trying to find the best way to pass a gradle task arguments from the command line. I have this task. I want to unpack solutions from student exercises and copy them into the right place in the project to evaulate them. I call this task like this:

> gradle swapSolution -Pstudent=MyStudent -Pexercise=ex05

One Problem i have with this while doing this in IntelliJ while having the Gradle plugin enabled is that i get this error message when build the project. What could be a solution to this?

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.

This is the gradle task:

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/"
            }

        }
    }
}

Do you have any suggestions to optimize this process?

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
Monogenesis
  • 35
  • 1
  • 5

1 Answers1

3

-P denotes the Gradle Project Property. If you need to use project properties you can specify it as a system property in gradle.properties file in project root directory.

If your task is of type JavaExec you can use --args switch and pass it in Arguments text field of the Gradle Task Run Configuration togenther with the task name like swapSolution -args="-student=MyStudent -exercise=ex05". See also https://stackoverflow.com/a/48370451/2000323

Andrey
  • 15,144
  • 25
  • 91
  • 187
  • Thank you very much for your reply, it helped me alot understanding the procedure. The fix for my Problem was that instead of taking the system property student like this system.properties("student"), i do this now, system.getProperties().get("student"). This way evaluating the project does not crash not knowing the parameters. Thank you! – Monogenesis Nov 26 '20 at 17:35