How do I export environment variables from the Ant 'exec' task? - ant

I am using Ant under WinXp to build MSVisual c++ projects.
To access "devenv.com", being computer-independent, I would like to use the vsvars32.bat script.
Unfortunately, it seems that environment variables are defined only in the "exec" scope.
Example:
<exec executable='"${env.VS90COMNTOOLS}vsvars32.bat/>
<echo message="${DevEnvDir}" />
<echo message="${env.DevEnvDir}" />
<property environment="env2"/>
<echo message="${env2.DevEnvDir}" />
I never get the expected result.
How can I use the "vsvars32.bat" script and access to its env. vars?
Is there better way to achieve this?

Instead of calling vsvars32.bat directly, call it from little helper script that writes the environment settings to a file using set.
Helper script vsenvwrap.bat:
#echo off
call "%VS90COMNTOOLS%\vsvars32.bat"
set > vsenv.txt
In your build.xml call the helper script, then read the settings file vsenv.bat:
<exec executable="vsenvwrap.bat" />
<property file="vsenv.txt" prefix="env2" />
You can then delete the vsenv.txt file during your build, or in your clean target.
This uses the fact that environment variable listings mostly conform to the format required by java property files.

You can create a small batch file that runs your ant script and in that batch file execute vsvars32.bat before calling ant.
#echo off
setlocal
set TEMP_HOME=%~dp0
call "%VS90COMNTOOLS%\vsvars32.bat"
call "%ANT_HOME%\bin\ant.bat" %*
endlocal

Related

Ant inline exec arguments

I have tasks running in Ant, which I'm quite new to, as part of a CI chain of build events. I used a tutorial to create the file for testing/linting/etc a PHP application.
The first important directives in the build.xml are:
<property name="phpmd" value="phpmd"/>
<property name="phpunit" value="phpunit"/>
This works fine as is, assuming that phpmd/phpunit are on the path, and using phpunit as a further example, is run under the following target:
<target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" resultproperty="result.phpunit" taskname="phpunit">
<arg value="--configuration"/>
<arg path="${basedir}/phpunit.xml"/>
</exec>
<property name="phpunit.done" value="true"/>
</target>
All this works well as is- but I want to use docker from now on, which I had hoped would simply mean changing <property name="phpunit" value="phpunit"/> to <property name="phpunit" value="docker-compose run php phpunit"/>, but this instead gives me the following error:
Execute failed: java.io.IOException: Cannot run program "docker-compose run -w /var/www/src php phpunit" (in directory "/var/lib/jenkins/jobs/Blah blah blah/workspace/src"): error=2, No such file or directory
I know that you would usually add additional <arg/> nodes to targets- but is it not possible at all to provide the full command with inline arguments on the initial <property>?
Ant is obviously complaining because, along with those inline arguments, that executable doesn't exist. Will I have to use arg nodes and update every single target?
Using docker-compose alone works fine, but I need the args for the correct container and working directory to be used- preferably inline, otherwise I have to insert many arg nodes.
In the end I just created a property for phpunit arguments and then added it by using the <args line="${phpunitArgs}">.
Definitely not ideal, but at least it does what it is supposed to. I certainly prefer using Gulp! XML feels like a bad choice for a build system.

Ant: How to call a directory with php files and save the result as overwritten files

How can I use Ant to execute each php file in a directory? The output for the php execution should overwrite the original file.
The directory is on a localhost webserver with php installed. I assume the solution would involve GET and have src point to the http equivalent of each file.
Some dynamic version of
c:/webroot/php/file1.php
c:/webroot/php/file2.php
translate into
get src="localhost/php/file1.php"
get src="localhost/php/file2.php"
and then have the output overwrite itself.
Why not use the php command line combined with the ANT apply task?
Something like:
<apply executable="C:\PHP5\php.exe">
<arg value="-f"/>
<fileset dir="c:/webroot/php" includes="*.php"/>
</apply>
It's not clear to me why you want to overwrite your source files....
Update
Alternatively use an embedded groovy script
<fileset id="phpfiles" dir="c:/webroot/php" includes="*.php"/>
<groovy>
project.references.phpfiles.each {
def file = new File(it.toString())
ant.get(src:"http://localhost/php/${file.name}", dest:"output/${file.name}")
}
</groovy>

Which Ant property contains the CWD when the ant script is run?

I don't want to get the basedir -- that appears to contain the build.xml script -- I want the CWD of the call to ant itself.
Basically, I want to do this:
$ cd /home/chrisr/projects/some_project
$ ant -f ../../tools/ant-build-rules/library.xml build-library
At this point, I need two things:
The path to ant-build-rules in absolute form; this is currently found in the basedir property, so I'm set there.
The path of some_project, in absolute form. This is what I don't know how to get.
Which property contains this information?
The java property user.dir contains the current directory
<project name="demo" default="printCWD">
<target name="printCWD">
<echo message="${user.dir}"/>
</target>
</project>
There is no such property, but you can run a script to get it.
${bsh:WorkDirPath.getPath()}
See urbancode.com.

Passing filepath to ANT from BAT script

I am trying to invoke an ANT target from Windows (right-click) file context menu.
I have setup the registry entries to invoke a batch script which invokes my ANT EXEC target.
I need to pass the path of the file (on which user right-clicked) to my ANT target. So I am using %~dp1 to set an ANT properties in my bat script:
Set tobeusedfilepath=%~dp1
Set tobeusedfile=%~n1
resulting in:
tobeusedfilepath=D:\Project\Rel L\
tobeusedfile=file
The problem is %~dp1 returns a string with "\" as file separator. But ANT EXEC task wants "/"
[exec] '-source'
[exec] 'D:ProjectRel L/file'
[exec] ......
[exec] The file, 'D:ProjectRel L/file', does not exist.
Any suggestions how to get around this path separators?
set AntPath="D:\Project\Rel L\"
set AntPath=%AntPath:\=/%
set AntPath=%AntPath::/=:%
gives
set AntPath="D:\Project\Rel L\"
set AntPath="D:/Project/Rel L/"
set AntPath="D:Project/Rel L/"
If you are running on Windows Ant will happily accept OS directory separator which is \.
Upon examination of the output of your program I see that the path separators are missing: you have D:ProjectRel not D:\Project\Rel. I may only guess that you are trying to exec a Cygwin program. Cygwin programs will use \ as an escape character. Therefore you need to use a <pathconvert> property to adjust the directory separators.
Code snippet below illustrates how to do this
<property name="tobeusedfilepath" location="D:\Project\Rel L\"/>
<property name="tobeusedfile" value="file"/>
<property name="system-path-filename"
location="${tobeusedfilepath}/${tobeusedfile}"
/>
<pathconvert property="unixized-filename" targetos="unix">
<path location="${system-path-filename}"/>
</pathconvert>
<echo message="system-path-filename=${system-path-filename}"/>
<echo message="unixized-filename=${unixized-filename}"/>
And here is the output of this run:
[echo] system-path-filename=D:\Project\Rel L\file
[echo] unixized-filename=D:/Project/Rel L/file

Ant build scripts, antcall, dependencies, etc

I have a build script and as part of that script it copies a jar file to a directory, for ease lets call it the utils jar. the utils jar is built by another build script sitting in another directory. What im trying to do have my build script run the utils build script so that I can ensure the utils jar is up to date.
So I know I need to import the utils build file.
<import file="../utils/build/build.xml" />
Which doesn't work because the import task, unlike almost every other ant taks, doesn't run from basedir, it runs from the pwd. So to get around that I have this little ditty, which does successfully import the build file
<property name="baseDirUpOne" location=".." />
<import file="${baseDirUpOne}/utils/build/build.xml" />
So now that ive solved my import problem I need to call the task, well that should be easy right:
<antcall target="utils.package" />
note that in the above, utils is the project name of ../utils/build/build.xml
the problem I'm now running into is that ant call doesn't execute in ../utils/build so what I need, and cant find, is a runat property or something similar, essentially:
<antcall target="utils.package" runat="../utils/build" />
The reason I need this is that in my utils build file the step to select which code to copy to the jar is based on relative paths so as to avoid hardcoding paths in my ant file. Any ideas?
I've got something similar set up: I have a main Ant build.xml which calls a separate build.xml that takes care of building my tests. This is how I do it:
<target name="build-tests">
<subant target="build">
<fileset dir="${test.home}" includes="build.xml"/>
</subant>
</target>
The trick is to use subant instead of antcall. You don't have to import the other build file.
Try using the "ant" task instead of the "antcall" task, which runs the imported build directly instead of importing it into the current build file. It has a "dir" parameter:
the directory to use as a basedir
for the new Ant project. Defaults to
the current project's basedir, unless
inheritall has been set to false, in
which case it doesn't have a default
value. This will override the basedir
setting of the called project.
So you could do:
<ant antfile="${baseDirUpOne}/utils/build/build.xml" dir="../utils/build" />
or something like that.
You can pass params down to antcall using nested in the antcall block. So, you can pass the properties down that way (probably even basedir since properties are immutable).

Resources