【问题标题】:How to conditionally specify a compiler argument in the javac task如何在 javac 任务中有条件地指定编译器参数
【发布时间】:2022-07-30 02:53:44
【问题描述】:

我正在尝试编写这样的目标

<!-- Properties that must be set when invoking via antcall:
        DestinationDir  Directory to compile classes to.
        ReleaseVer      Java version this is being compiled for (i.e. 8, 9, etc). Must be at least 8.
        Debug           Whether to compile with debug information. Either 'on' or 'off'.
    -->
    <target name="java-support-compile" description="compile JavaSupport source">
        <mkdir dir="${DestinationDir}"/>
        <condition property="ModulesSupported">
            <not>
                <equals arg1="${ReleaseVer}" arg2="8" />
            </not>
        </condition>
        <!-- Compile GSSUtilWrapper separately, since we can't use the 'release' option when referencing the sun.security.jgss.GSSUtil package,
             and we need to add an &#45;&#45;add-exports option to access the java.security.jgss module for ${ReleaseVer} > 9 -->
        <javac srcdir="${javasupport.src.dir}" source="1.${ReleaseVer}" target="1.${ReleaseVer}" debug="${Debug}" destdir="${DestinationDir}">
            <include name="${GSSUtilWrapperFile}"/>
            <compilerarg if="${ModulesSupported}" line="--add-exports java.security.jgss/sun.security.jgss=ALL-UNNAMED" />
        </javac>
        <javac srcdir="${javasupport.src.dir}" release="${ReleaseVer}" debug="${Debug}" destdir="${DestinationDir}">
            <exclude name="${GSSUtilWrapperFile}"/>
        </javac>
    </target>

我得到的错误是compilerarg doesn't support the "if" attribute。我需要它是有条件的,就好像我传入 ReleaseVer=8,我收到错误 error: option --add-exports not allowed with target 8

我从 http://ant-contrib.sourceforge.net/compilerarg.html 获得了该语法,但我没有意识到这不在核心 ant 中(如果可能的话,我不想安装任何其他东西)。

如何在标准蚂蚁中做到这一点?

【问题讨论】:

    标签: ant


    【解决方案1】:

    一个可能不是最干净的选项是修改您的&lt;condition&gt; 任务以设置编译器arg 的文本,而不是布尔值,然后使用它。像这样的:

    <condition property="javac_arg"
               value="--add-exports java.security.jgss/sun.security.jgss=ALL-UNNAMED"
               else="">
      <not>
        <equals arg1="${ReleaseVer}" arg2="8" />
      </not>
    </condition>
    
    <javac compiler="modern" srcdir="." includeantruntime="no">
      <compilerarg line="${javac_arg}" />
    </javac>
    

    注意else参数,它确保条件为假时传递给javac的arg为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2011-12-10
      • 2011-05-07
      • 2017-10-06
      • 2013-07-03
      • 2023-03-23
      • 2011-12-06
      相关资源
      最近更新 更多