Search code examples
javaant

How to build using ANT preview features of Java 17 in Eclipse


<target name="compile" depends="init" description="Compile the source.">
    <javac srcdir="${src}" classpathref="classpath" destdir="${build.bin}" compiler="modern" debug="true" debuglevel="lines,vars,source" includeantruntime="false" source="17" target="17" />
</target>

Eclipse won't build cause my switch contains preview code. How can I enable preview and allow compilation using ANT (build.xml)?

error: patterns in switch statements are a preview feature and are disabled by default.


Solution

  • You can use the Ant <compilerarg> element, which can be nested inside the <javac> element.

    Specifically, you need the javac --enable-preview argument.

    Example:

    <target name="compile" depends="init" description="Compile the source.">
        <javac srcdir="${src}" classpathref="classpath" destdir="${build.bin}" compiler="modern" debug="true" debuglevel="lines,vars,source" includeantruntime="false" source="17" target="17">
            <compilerarg value="--enable-preview" />
        </javac>
    </target>
    

    More information and examples for compilerarg can be found here: Ant: passing compilerarg into javac