tags:

views:

2166

answers:

2

I'm using ant to build a mixture of Java and C++ (JNI) code that makes up a client project here. I've recently switched the C++ part of the build to using ant with cpptasks to build the C++ code instead of having ant invoke the various versions of Visual Studio that are necessary to build the code.

In order to get this to work, it is necessary to use ant's exec task to spawn off a shell in which a shell script or a batch file executes to set up the compiler environment before triggering another ant that executes the cpptasks-based C++ build. Essentially, the C++-related build tasks in the main ant build file look like this for Windows:

<target name="blah">
    <exec executable="cmd" failonerror="true">
      <arg value="/C"/>
      <arg line="&quot;${cpp.compiler.path}/vsvars32.bat&quot; &amp;&amp; %ANT_HOME%/bin/ant -f cpp-build.xml make-cpp-stuff" />
    </exec>
</target>

There is no way to get rid of the vsvars32.bat invocation as the code has to be built with multiple VS versions and on build machines where none of the Visual Studio setup can be part of the build user's environment.

The above works, but the issue I'm running into is that I would like to pass certain command line options (like -verbose, -quiet, -emacs) through to the child ant if they have been passed to the parent ant. Is it possible to get access at the command line options given to the parent ant at all? Please note I'm not talking about the usual property definitions, but the ant-internal options.

+1  A: 
<target name="blah">
    <property environment="env"/>
    <exec executable="cmd" failonerror="true">
      <arg value="/C"/>
      <arg value="${cpp.compiler.path}/vsvars32.bat"/>
      <arg value="&amp;&amp;"/>
      <arg value="${env.ANT_HOME}/bin/ant.bat"/>
      <arg value="-f" />
      <arg value="cpp-build.xml" />
      <arg value="make-cpp-stuff" />
    </exec>
</target>

Addition

You can create an external batch file that will run the vsvars and the ant, and then you will have only one process to create. I believe the && is not working as you expect it to:

run-ant-vs.bat:

....\vsvars32.bat
%ANT_HOME\bin\ant.bat -f cpp-build.xml make-cpp-stuff
David Rabinowitz
Thanks for pointing out how to split the command line into <arg value=""> - that finally helped me do this properly. That said, it doesn't solve the actual problem I'm having, namely passing -emacs/-verbose/-quit to the child ant if the ant that invoked the target 'blah' had these options or one of these options set on the command line. BTW, I also took the liberty to insert an important missing element in the arg value list.
Timo Geusch
A: 

I'm not sure if this could help you. Is Java client execution using a parameter you pass when you execute ant build, you can try to adapt this example (exec is more generic than java task, but is a similar concept)

Ant task example:

<target name="run">
  <java classname="my.package.Client" fork="true" failonerror="true">
    <arg line="-file ${specific.file}"/>
  </java>
</target>

Invocation example:

ant run -Dspecific.file=/tmp/foo.txt
JuanZe