Search code examples
antjavac

How to pass command line options to the java compiler through the <javac> Ant task using <compilerarg value="">?


To compile code using preview features, javac requires options --enable-preview and --release.

If I pass these options to the <javac> Ant task (Ant 1.10.5) using the line attribute, as shown below, compilation succeeds.

<compilerarg line="--enable-preview --release 15"/>

But if I pass them as individual arguments using the value attribute, as shown below, Ant throws error: invalid flag: --release 15.

<compilerarg value="--enable-preview"/>
<compilerarg value="--release 15"/>

Since the Ant manual says "It is highly recommended to avoid the line version when possible", I'd like to know how to make the code work with the value attribute as well. What's the trick?


Solution

  • It needs to be three arguments, like:

    <compilerarg value="--enable-preview"/>
    <compilerarg value="--release"/>
    <compilerarg value="15"/>
    

    ... otherwise, as you saw, "--release 15" is treated as a single argument with an embedded space.