ant command from powershell - dot used as delimiter for parameter? - powershell

I'm trying to run an ant task which I'm invoking from a powershell script:
Invoke-Expression "ant -f $antFilePath -Dtestsuite=$testsuite clean compile run"
The $testsuite variable is a string which includes a dot character, e.g. "systemTest.All", so:
Invoke-Expression "ant -f $antFilePath -Dtestsuite=systemTest.All clean compile run"
My problem is that the dot seems to be interpreted as a delimiter (by powershell? Invoking from cmd works just fine), hence the "All" part gets treated as a ant target (among with clean compile run).
(The use of a dot in the testsuite name is not one of mine doings so that part I can not affect)
Do I need to qoute the ant argument, escape the dot in some way?
Br,
Pete

Try this (but I can't test it), run this directly w/o invoke-Expression:
ant -f $antFilePath "-Dtestsuite=$testsuite" clean compile run

Related

Powershell how to pass System variables

I am trying to execute the below command on powershell, but the encryption password is not recognised.
This password is used in integration tests.
gradle publish -Djasypt.encrypt.password = $xyz!#
The below command also does not work
cmd /c gradle publish -Djasypt.encrypt.password = $xyz!#
The same command works well on CMD
Any suggestions on passing the arguments (with -D)?
$ is the sigil denoting a variable in PowerShell just like most other shells, so $xyz means the variable named xyz. You need to escape that symbol with a backtick
gradle publish -Djasypt.encrypt.password = `$xyz!#
Alternatively just quote the string with a single quote to prevent variable substitution
gradle publish -Djasypt.encrypt.password = '$xyz!#'

How to use command-line argument files for javac on windows?

Here's my working directory:
C:\GitHub\
And under this directory I have:
class\ src\ cmain
and cmain is my argument file.
All my .java source code files are undersrc\
main.java CalcHandler.java
And here's the content ofcmain
-cp .\class\
-sourcepath .\src\
-d .\class\
.\src\main.java
But when I tried compiling src\main.java in PowerShell usingcmain, it is not recognized:
PS C:\GitHub> javac #cmain
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
....
I tried this in Linux and it worked fine, contents were exactly the same.
What should I do to make it work?
Edit: Apparently this is PowerShell's fault, it worked in command-prompt. But still I'd like to hear from you about how to make it correct in PowerShell.
javac '#cmain'
PowerShell parses arguments to commands, and #cmain means taking the array $cmain and expanding it to one argument per item (splatting). By passing an explicit string you can bypass that automatism.
Other options:
javac --% #cmain
This will tell PowerShell to stop parsing arguments after the --% marker and just pass them verbatim to the other program.
javac `#cmain
This will escape the # so its special behavior will not apply.
Always remember that shell features will need workarounds if you want to pass that syntax to other programs. That's no different than % expanding environment variables in cmd, or most shells tokenizing arguments at spaces and other whitespace. A small utility program that just prints its arguments can come in handy for diagnosing such cases.

javac powershell classpath separator

So I'm aware that different operating systems require different classpath separators. I'm running a build of windows where CMD has been replaced with Powershell which is causing problems when using the semi-colon separator.
The command I'm trying to run begins with cmd /c to try and get it to run in command prompt instead but I think when PowerShell is parsing the whole command it sees the semi-colon and thinks that is the end!
My whole command is:
cmd /c javac -cp PATH1;PATH2 -d DESTINATION_PATH SOURCE_PATH
I have tried using a space, colon and period to no avail. Can anybody suggest a solution?
This is my first question on stackoverflow, hope the community can help and that it will eventually help others. :)
I suggest you start the process in the following way using Powershell
Start-Process cmd.exe -ArgumentList "/c javac -cp PATH1;PATH2 -d DESTINATION_PATH SOURCE_PATH" -NoNewWindow
Running javac in CMD shouldn't be required. Just put quotes around arguments that (may) contain whitespace or special characters. I'd also recommend using the call operator (&). It's optional in this case, but required if you put the executable in quotes (e.g. because the executable or path contains spaces or you want to put it in a variable).
& javac -cp "PATH1;PATH2" -d "DESTINATION_PATH" "SOURCE_PATH"
You could also use splatting for providing the arguments:
$javac = "$env:JAVA_HOME\bin\javac.exe"
$params = '-cp', "PATH1;PATH2",
'-d', "DESTINATION_PATH",
"SOURCE_PATH"
& $javac #params
javac -classpath "path1:path2:." main.java does the trick in powershell. the cmd doesn't need the doble quotes however while using powershell we need to put the quotes and it works smoothly.

Passing property values with spaces to Ant

I'm trying to pass a space-separated value $env:tt to Ant under PowerShell
$env:tt="val1 val2"
Here are the commands I've tried:
ant '-DTest="$env:tt"'
ant -DTest=$env:tt
With the above commands, Ant doesn't interpret $env:tt. The value of test becomes $env:tt.
ant -DTest="$env:tt"
I got the following response under PowerShell
PS C:\> ant -DTest="$env:tt"
>>
It seems that this command is not finished, and PowerShell expects me to enter some characters to terminate the command.
Any idea on how to do this?
variables are expanded inside double quotes but not when inside single quotes.
ant "-dest=$env:tt"

Disable powershell expansion of command's extension?

We have a lot of existing batch files that help with the running of different perl and ruby scripts.
A batch file (e.g. test.bat) would normally be invoked like:
$ test
and within the batch file, it will set some settings and finally try to run the corresponding script file (e.g. test.pl) like this:
perl -S "%0.pl" %*
All works with cmd.exe, but today, I decided to switch to PowerShell and found out that it expands the commands. So trying to run "test" will actually run "Full\path\test.bat" and my script would complain that there is no file test.bat.pl.
Is there a way to prevent this command expansion? Rewriting all batch files is not an option.
One way is to call cmd explicitly:
cmd /c test