Executing a unix command in cygwin launched from ant in eclipse - java

I have an ant build for a java project that I would like to trigger from within eclipse, that in turn launches a cygwin window that executes a deployment script. Is it possible to implement this without calling a new command window from ant?

Use bash.exe as the executable (replacing or parameterizing cygwin directory as needed):
<project default="default">
<target name="default">
<exec executable="c:/cygwin/bin/bash.exe">
<arg value="-c"/>
<arg value="ls c:/"/>
</exec>
</target>
</project>

Related

CreateProcess error=2, Can not run program "python" on Ant

I have a C:\Users\Sony\Desktop\Project\Workspace\testpython\verify_fam_extended.xml:52: Execute failed: java.io.IOException: Cannot run program "python" (in directory "C:\Users\Sony\Desktop\Project\Workspace\testpython\backend\generated"): CreateProcess error=2, The system cannot find the file specified error when I run my xml code through Ant.
<exec dir="backend/generated" executable="python">
<arg line="toplevel.py"/>
<env key="PYTHONPATH" value="${dsltrans.install.path}:./backend/generated"/>
</exec>
edit python.exe must be in a directory defined in the executable path variable PATH.
Following working example assumes the presence of C:\Python33\python.exe
<project name="demo" default="main" basedir=".">
<property environment="env" />
<echo>PATH is set as: ${env.PATH}</echo>
<target name="main">
<exec dir="backend/generated" executable="python">
<arg line="--version"/>
</exec>
</target>
</project>
execute in a command session
set PATH=C:\Python33;%PATH%
ant
output
Buildfile: X:\temp\build.xml
[echo] PATH is set as: C:\Python33;...
...
main:
[exec] Python 3.3.0
edit Just checked. The executable can be executable="python" or executable="python.exe". So #cdarke is right the issue is that python.exe is not in PATH.

Ant target to run Windows BAT including changing directory

I have a Windows BAT file that looks like this:
Z:
tool.exe c:\mypath\to\input c:\mypath\to\output \flag1 flag2
That is, tool.exe resides on a mapped network drive. The tool requires that the EXE is run from its own directory (Z: in this case).
The BAT works fine when run via double-click or in a CMD window.
I'd like to automate it through Ant. My attempt is here:
<target name="go">
<exec executable="cmd">
<arg value="/c"/>
<arg value="runtool.bat"/>
</exec>
</target>
It doesn't work. From this link and other research, I understand the following:
"Z:" is likely the equivalent of "cd Z:\"
there is no "cd.exe"... "cd" is interpreted by the Windows CMD shell, and not available via "cmd.exe"
That said, how to achieve the goal in Ant? in Java?
I'm open to writing my own Ant task, doing further network configuration of the directories, etc. (At the risk of making the question too broad, we are using Jenkins and open to ideas there as well.)
You have to specify the dir attribute on the exectask:
<exec executable="tool.exe" dir="z:">
<arg value="c:\mypath\to\input"/>
<arg value="c:\mypath\to\output"/>
<arg value="flag1"/>
<arg value="flag2"/>
</exec>
This worked for me:
<target name="go">
<property name="batFileDir" location="${basedir}/resources" />
<exec executable="cmd" dir="." spawn="false">
<arg line="/C start ${batFileDir}/runTool.bat"/>
</exec>
</target>
where the runTool.bat is as described in the question, and located in ~/resources.

Trouble Executing Bash.exe From an Ant Buildfile in Eclipse

I've created an Ant buildfile and an associated Ant builder in my Eclipse project. The builder is executing correctly but I can't seem to pass the correct information to bash. I'm running cygwin on an XP Professional SP3 machine. I know the command works and have verified it from a cygwin terminal. I created a custom builder earlier with this command so I also know that it works from Eclipse.
Here is my build xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="BlazeLibrary.makejar" default="makejar" basedir=".">
<property name="bash" location="e:\cygwin\bin\bash.exe" />
<property name="workingdir" location="e:\cygwin\bin" />
<property name="cmdline" value="--login -c \"cd /cygdrive/c/dev/projects/droid/NDKTestApp && /cygdrive/c/dev/tools/droid/android-ndk-r4b/ndk-build"" />
<target name="nativeBuild" description="Build the native binaries using the Android NDK">
<exec dir="${workingdir}" executable="${bash}">
<arg value="${cmdline}" />
</exec>
</target>
</project>
The task runs fine but the output I indicates that the command line parameters are incorrect. Even though they are listed exactly as they should be (and verified by running from cmd prompt as well as the custom builder mentioned previously).
Here is the relevant part of the error message (the rest just vomits the help and is of no relevancy for this question):
nativeBuild:
[exec] /usr/bin/bash: --login -c "cd /cygdrive/c/dev/projects/droid/NDKTestApp && /cygdrive/c/dev/tools/droid/android-ndk-r4b/ndk-build": invalid option
[exec] Usage: /usr/bin/bash [GNU long option] [option] ... blah blah blah
I'll be the first to admit that I am an Ant noob so I'm probably missing something very obvious. I've searched but nothing really jumps out at me and the task seems to run correctly, just something funky about the command line. Thanks for any help in advance.
The immediate problem is that the ${cmdline} property is being passed to bash as a single argument - hence the very long 'invalid option'.
You could pass the command as an arg line instead:
<exec dir="${workingdir}" executable="${bash}">
<arg line="${cmdline}" />
</exec>
Or perhaps break it up into separate values. Note that you don't need the quots around the -c arg in that case:
<property name="cmdline" value="cd /cygdrive/ ..etc.. ndk-build" />
<exec dir="${workingdir}" executable="${bash}">
<arg value="--login" />
<arg value="-c" />
<arg value="${cmdline}" />
</exec>
More here.
You can still use the format in your initial post, just change your quotes around a bit, like this:
<!-- These properties hold the location of the android-ndk, for us to build our antive libs -->
<property name="bash" location="c:\cygwin\bin\bash.exe" />
<property name="workingdir" location="c:\cygwin\bin" />
<property name="cmdline" value="--login -c 'cd /path/to/project/files;/path/to/ndk_build/ndk-build;exit'" />
<target name="NDKBUILD" description="Build the native binaries using the Android NDK">
<exec dir="${workingdir}" executable="${bash}">
<arg line="${cmdline}" />
</exec>
</target>
Note the usage of " " to denote the XML string, but ' ' to denote command lines passed to cygwin/bash.
Also, when using this script in my android builder (heavily customised ant script with extensive source preprocessing) I ran into issues where the ant build was "Already In Progress".
To this end, I added an exit command to the end of the bash script, so the cygin bash process is always closed after being called.

Use Ant for running program with command line arguments

My program getting command line arguments. How can I pass it when I use Ant?
Extending Richard Cook's answer.
Here's the ant task to run any program (including, but not limited to Java programs):
<target name="run">
<exec executable="name-of-executable">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</exec>
</target>
Here's the task to run a Java program from a .jar file:
<target name="run-java">
<java jar="path for jar">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</java>
</target>
You can invoke either from the command line like this:
ant -Darg0=Hello -Darg1=World run
Make sure to use the -Darg syntax; if you ran this:
ant run arg0 arg1
then ant would try to run targets arg0 and arg1.
If you do not want to handle separate properties for each possible argument, I suggest you'd use:
<arg line="${args}"/>
You can check if the property is not set using a specific target with an unless attribute and inside do:
<input message="Type the desired command line arguments:" addProperty="args"/>
Putting it all together gives:
<target name="run" depends="compile, input-runargs" description="run the project">
<!-- You can use exec here, depending on your needs -->
<java classname="Main">
<arg line="${args}"/>
</java>
</target>
<target name="input-runargs" unless="args" description="prompts for command line arguments if necessary">
<input addProperty="args" message="Type the desired command line arguments:"/>
</target>
You can use it as follows:
ant
ant run
ant run -Dargs='--help'
The first two commands will prompt for the command-line arguments, whereas the latter won't.
The only effective mechanism for passing parameters into a build is to use Java properties:
ant -Done=1 -Dtwo=2
The following example demonstrates how you can check and ensure the expected parameters have been passed into the script
<project name="check" default="build">
<condition property="params.set">
<and>
<isset property="one"/>
<isset property="two"/>
</and>
</condition>
<target name="check">
<fail unless="params.set">
Must specify the parameters: one, two
</fail>
</target>
<target name="build" depends="check">
<echo>
one = ${one}
two = ${two}
</echo>
</target>
</project>
Can you be a bit more specific about what you're trying to do and how you're trying to do it?
If you're attempting to invoke the program using the <exec> task you might do the following:
<exec executable="name-of-executable">
<arg value="arg0"/>
<arg value="arg1"/>
</exec>
What I did in the end is make a batch file to extract the CLASSPATH from the ant file, then run java directly using this:
In my build.xml:
<target name="printclasspath">
<pathconvert property="classpathProp" refid="project.class.path"/>
<echo>${classpathProp}</echo>
</target>
In another script called 'run.sh':
export CLASSPATH=$(ant -q printclasspath | grep echo | cut -d \ -f 7):build
java "$#"
It's no longer cross-platform, but at least it's relatively easy to use, and one could provide a .bat file that does the same as the run.sh. It's a very short batch script. It's not like migrating the entire build to platform-specific batch files.
I think it's a shame there's not some option in ant whereby you could do something like:
ant -- arg1 arg2 arg3
mpirun uses this type of syntax; ssh also can use this syntax I think.

NetBeans Post Build Script

I'm using NetBeans 6.8, and I want to run a script to construct an SQLite database with a predefined schema after each build. I don't see any options under Build Configuration. Is this feature not yet supported?
Figured it out. Just edited the build.xml file that associates itself with the project. Details on how to do it are self-contained.
<target name="-post-compile">
<exec dir="." executable="cmd">
<arg line="/c DatabaseInitializer.bat"/>
</exec>
</target>

Categories

Resources