how to exec command in new prompt using ant - selenium

I am trying to run a command in ant to start the selenium server but it opens up in the same command prompt, is there anyway i can open it up in a new prompt?
<project>
<target name="startGRID">
<exec dir="." executable="cmd">
<arg value="/c"/>
<arg value="java -jar selenium-server-standalone-2.43.1.jar -role hub"/>
</exec>
</target>
</project>

To see a separate command prompt in which your server is run, use the dos start command, which does exactly that:
<exec dir="." executable="cmd">
<arg value="/c"/>
<arg value="start"/>
<arg value="java -jar selenium-server-standalone-2.43.1.jar -role hub"/>
</exec>
The only issue I find with that is that, when the server is terminated, the new command prompt window will remain open. This can be worked around by wrapping the java command in a batch script, let's call it start-selenium.bat, with an exit statement at the end:
java -jar selenium-server-standalone-2.43.1.jar -role hub
exit
then the Ant task becomes:
<exec dir="." executable="cmd">
<arg value="/c"/>
<arg value="start"/>
<arg value="start-selenium.bat"/>
</exec>

Related

dotnet publish -o ./dist does not set $OutDir or $OutPath in msbuild

I Have a file move event which I want to trigger after a publish
<Target Name="CopyEmailTemplates" AfterTargets="AfterPublish">
<ItemGroup>
<TemplatesFolder Include="Views\EmailTemplates\*.cshtml" />
</ItemGroup>
<Copy SourceFiles="#(TemplatesFolder)" DestinationFolder="$(OutDir)Views\EmailTemplates\" />
</Target>
I've confirmed that the command does not return the publish directory with this target:
<Target Name="OutputTest" AfterTargets="AfterPublish">
<Exec Command="echo OutPath: $(OutputPath)" />
<Exec Command="echo OutDir: $(OutDir)" />
</Target>
Expected:
OutDir is set to dist/
Actual behavior:
OutDir is set to bin/Release/netcoreapp2.0/
I am using: .NET Command Line Tools (2.1.4) on osx.10.12-x64
Publish is a two-step process. The project is built using normal build settings and then published to $(PublishDir). Use this property wherever you need to know the path of the publish output.
Self answering in hopes to prevent future headaches for people.
The dotnet publish -o ./dist command will set the $(PublishDir) variable in msbuild.
dotnet build -o ./dist does however set $(OutDir)
To be more explicit with our build I now use the msbuild command
dotnet publish -o ./dist -c Release
Becomes:
dotnet msbuild /t:publish /p:PublishDir=dist/ /p:Configuration=Release

Execute npm with ant

I want to automate the build of an application with frontend and backend. To do this I want to use maven with ant for platform independent copy and cli tasks. With some cli's like docker ... this works. But this does not work for cli's provided by npm or npm itself.
<exec executable="docker">
<arg line="version"/>
</exec>
<!--Works-->
<exec executable="C:\Program Files\nodejs\npm.cmd">
<arg line="version"/>
</exec>
<!--Doesn't work-->
<exec executable="npm">
<arg line="version"/>
</exec>
As seen in the second example, the script works if I specify the full path to the npm.cmd. But this should work at least on windows and unix. So specifying the full path is not an option.
Is there any way to run npm and its modules from ant?
Late edit:
The real problem was, that the windows node installer also placed a file named npm into the bin folder which is a bash script meant for cygwin. The npm bin folder was added to the 'global' PATH env var and the windows cmd did pick up the correct binary because it uses the PATHEXT env var to determine what is executable and what not. The ant exec plugin does not use PATHEXT and just executes the first file named npm which fails. The Solution was to rename the plain npm file in the path. This way ant sees the npm.cmd file first and everything runs smoothly.
I know this is old, but this is what I have, for others in the future. Works in our mac, unix, and windows boxes.
<macrodef name="exec-node">
<attribute name="module" description="The name of the NodeJS module to execute" />
<attribute name="failonerror" default="true" description="Fail if the exit code is not 0" />
<attribute name="dir" description="Directory to execute task" />
<element name="args" implicit="yes" description="Argument to pass to the exec task" />
<sequential>
<exec executable="cmd.exe" dir="#{dir}" failonerror="#{failonerror}" osfamily="winnt">
<arg line="/c #{module}" />
<args />
</exec>
<exec executable="#{module}" dir="#{dir}" failonerror="#{failonerror}" osfamily="unix" logError="true">
<args />
</exec>
</sequential>
</macrodef>
Then things like this can work
<exec-node dir="${node.project.dir}" module="npm" failonerror="true" >
<arg value="run" />
<arg value="lint" />
</exec-node>
You can of course hardcode module as npm, or default it, but I use this with npm and npx.
From what I can gather, its not possible to call npm directly through the antrun plugin.
I did manage to get it tu run by calling cmd (on windows) with the /c argument.
Example:
<exec executable="cmd">
<arg line="/c npm run babel -- src/main/webapp/js/es6/ --presets babel-preset-es2015 --out-dir src/main/webapp/js/"/>
</exec>
This works for me on Windows:
<exec executable="npm.cmd">
<arg value="version"/>
</exec>
If you like to use npm you should take a look at the frontend-maven-plugin.

msbuild fails with successful robocopy exit code

My msbuild script fails even if copying files is successful. If robocopy command exitcode < 8, it means that files copied. So how can I say to msbuild script IgnoreExitCode if exit code < 8?
I set IgnoreExitCode to true, but what if it's real error?
<Exec Command="robocopy $(SourceDir) $(DestinationDir) /mir /mt /xd $(ExcludeDir)" IgnoreExitCode="true" />
Use ExitCode output parameter of Exec task and ContinueOnError parameter instead of IgnoreExitCode:
<Exec ContinueOnError="True" Command="robocopy $(SourceDir) $(DestinationDir) /mir /mt /xd $(ExcludeDir)">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
</Exec>
<Error Condition="$(ErrorCode) > 8" Message="Robocopy failed"/>
Try this workaround:
(robocopy $(SourceDir) $(DestinationDir) /mir /mt /xd $(ExcludeDir)) ^& IF %ERRORLEVEL% LEQ 1 exit 0

Open ssh tunnel in Ant in and keep it open (but continue on to other tasks)

I tried opening an ssh tunnel in ant, but it appears to close immediately and requires a task to run under it. How can I open a tunnel on a port and keep it open but not hang up the rest of the tasks while it sits open?
I tried:
<target name="tunnel">
<sshsession host="${ssh.host}" username="${username}" keyfile="${keyfile}" localtunnels="${port}:localhost:${port}">
<sequential />
</sshsession>
</target>
Found the answer. You can't use sshsession but instead have to use exec.
<exec spawn="true" executable="ssh">
<arg value="-t" />
<arg value="-t" />
<arg value="-L" />
<arg value="4003:localhost:4003" />
<arg value="username#server" />
</exec>
Note: The double -t is not an error. You need two of those or it won't work
This starts it in the background. To shut it down, you need to find the pid, which you can do with a grep: ps aux | grep 4003 | grep -v grep | awl '{print $2}' and then kill it

Exec Task in MSBuild for execution of command on remote machine

I am using following command to install a service via MSBuild file. This works great
<Exec Command= 'c:\test\myService.Appservices.exe install' ContinueOnError='false' />
But the above command install the service on local machine. I want to install the service on a remote machine. How can I specify the machine name using this command?
As per Mike Vine's comment, MSBuild doesn't include tools for remote execution. You could however use something like psexec. e.g.
<Exec Command='psexec -accepteula -s \\RemoteServer "C:\Path To EXE on Remote Machine\my.EXE"' IgnoreExitCode="false" ContinueOnError="false" Timeout="600000" >
<Output TaskParameter="ExitCode" PropertyName="exitCode1"/>
</Exec>