title: keytool with -dname containing spaces in Gradle: illegal option
body: <p>I am trying to create a task that would involve the Java keytool binary, but apparently I am unable to pass the dname argument properly. Spaces seem to be the problem, but only when run as a Gradle script. When I copy-paste the same command into a terminal it works. (I believe in both cases the same keytool binary is being used.)</p>
  
  <p>This is my script:</p>
  
  <pre><code>task generateTomcatKeystore() {
    doLast {
        def serverAlias = "server_debug"
        def serverStorePass = "changeit_server"
        def serverKeyPass = "changeit_server"
  
        def ourDomain = "domain.net"
        def ourName = "Company"
  
        def dName = /CN=$ourDomain, OU=Backend, O=$ourName, L=Prague, S=Czech Republic, C=CZ/
        def keytoolCommand = /keytool -genkeypair -alias $serverAlias -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -dname "$dName" -validity 365 -keypass $serverKeyPass -keystore tomcat.jks -storepass $serverStorePass/;
  
        // what exactly am I executing?
        println keytoolCommand
  
        def outputErr = keytoolCommand.execute().err.text
        // errors?
        println outputErr
  
        // am I calling the right binary? ...apparently yes
        println 'which keytool'.execute().text
    }
  }
  </code></pre>
  
  <p>This is the complaint I am getting:</p>
  
  <pre><code>Illegal option:  OU=Backend,
  </code></pre>
  
  <p>When I eliminate all the spaces in between the dname components like this:</p>
  
  <pre><code>def dName = /CN=$ourDomain,OU=Backend,O=$ourName,L=Prague,S=Czech Republic,C=CZ/
  </code></pre>
  
  <p>the error changes to:</p>
  
  <pre><code>Illegal option:  Republic,C=CZ"
  </code></pre>
  
  <p>...so clearly the spaces are the issue. But only when run from within Gradle.</p>
  
  <p>Note that my -dname is enclosed by double quotes:</p>
  
  <pre><code>keytool -genkeypair -alias server_debug -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -dname "CN=domain.net,OU=Backend,O=Company,L=Prague,S=Czech Republic,C=CZ" -validity 365 -keypass changeit_server -keystore tomcat.jks -storepass changeit_server
  </code></pre>
  
  <p>According to Oracle only commas have to be escaped within the dname argument, <a href="https://docs.oracle.com/javase/7/docs/technotes/tools/solaris/keytool.html#DName" rel="nofollow noreferrer">see for yourself</a>.</p>