1

I try to create a simple Gradle task to execute an sql query. The command I try to execute is:

UPDATE user_account SET user_password = 'xyz'

The complete command line looks like this:

"c:\Program Files\MariaDB 10.3\bin\mysql.exe" --user=dbUser --password=dbPass-D dbName -e "UPDATE user_account SET user_password = 'xyz'"

This command works fine from the command line. However it doesn't work from a Gradle Exec task:

task updatePasswords(type:Exec) {
    def sql = "UPDATE user_account SET user_password = 'xyz'"
    def myCmd = '"' + dbSqlExe + '" --user=' + dbUser + ' --password=' + dbPass + ' -D ' + dbDb + ' -e "' + sql + '"'
    println 'Update cmd: ' + myCmd
    commandLine 'cmd', '/c', myCmd

    ignoreExitValue = true
}

Where

def dbSqlExe = "c:\\Program Files\\MariaDB 10.3\\bin\\mysql.exe"

This task reports an error:

'c:\Program' is not recognized as an internal or external command, operable program or batch file.

If I remove the double quotes around the sql query then mysql.exe will be found, but the command is not valid any more as mysql requires the double quotes around the SQL query.

I have tried the solution in this thread: gradle: Execute task "type:Exec" with many arguments with spaces without success.

Any tip how should I format my task?

Vmxes
  • 2,329
  • 2
  • 19
  • 34

1 Answers1

0

Try this

task updatePasswords(type:Exec) {
    def sql = "UPDATE user_account SET user_password = 'xyz'"
    commandLine = ['cmd', '/c', '"' + dbSqlExe + '"', "--user=$dbUser", "--password=$dbPass", '-D', dbDb, '-e', sql] 
    ignoreExitValue = true
    doFirst {
        println "Executing $commandLine" 
    } 
}  
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • This code reports a silly error in the line of **commandLine ['cmd', '/c',...** that say: "Problem occurred evaluating root project 'demoProject'" > Cannot cast object 'cmd' with class 'java.lang.String' to class 'int' – Vmxes Jun 04 '19 at 14:11
  • I spend hours debugging things like this with groovy. Did you use my exact code? Try `commandLine = [...]` instead of `commandLine [...]`. I've updated my answer accordingly – lance-java Jun 04 '19 at 21:27
  • Still the same error. Maybe my variable declaration is wrong. `def dbSqlExe = 'c:\\Program Files\\MariaDB 10.3\\bin\\mysql.exe'` I have updated the original post. – Vmxes Jun 07 '19 at 06:36
  • I've updated my solution, wrapped the exe in double quotes. Pls try again – lance-java Jun 07 '19 at 07:32
  • 1
    Unfortunately, doesn't help. Still the same error. I gave it up and put the sql statement in separate file. – Vmxes Jun 07 '19 at 10:49
  • I suggest that you remove gradle from the picture. At a DOS prompt, type `cmd /c xxx` and get it working without gradle. You can then bring it into gradle once that's working – lance-java Jun 07 '19 at 14:13
  • Are you sure you tried `cmd /c xxx` from command line? Your question states "The complete command line looks like this: c:\Program Files\MariaDB..." – lance-java Jun 11 '19 at 08:56
  • Hoops, I just executed the `xxx` part alone and it works. But with the `cmd /c` prefix it really fails with the original error. – Vmxes Jun 11 '19 at 10:04
  • Exactly. Once you get that working from command line you should be able to bring it into gradle – lance-java Jun 11 '19 at 11:08