简体   繁体   English

如何将Ant编译器更改为JDK 1.6

[英]How to change Ant compiler to JDK 1.6

I need to compile my source code to be compatible with jre 1.6. 我需要编译我的源代码以与jre 1.6兼容。 However, when I attempt to set the compiler attribute of the javac task to be javac1.6, ant will still compile my code with javac1.7. 但是,当我尝试将javac任务的编译器属性设置为javac1.6时,ant仍将使用javac1.7编译我的代码。 I have also tried setting the compiler version to be "modern" and that did not help. 我也尝试将编译器版本设置为“现代”,但没有帮助。

<target name="compile-tests">
    <javac compiler="javac1.6" includeantruntime="false" srcdir="${test.dir}"
     destdir="${build.dir}" >
        <classpath refid="class.path" />
    </javac>
</target>

My JAVA_HOME is set to JDK 1.6: 我的JAVA_HOME设置为JDK 1.6:

echo $JAVA_HOME </code> gives: <code>
/usr/lib/jvm/java-6-openjdk-amd64/

My ant version is: Apache Ant(TM) version 1.8.2 我的ant版本是:Apache Ant(TM)版本1.8.2

According to this post , ant uses its own compiler. 根据这篇文章 ,ant使用自己的编译器。 How do I override the ant default? 如何覆盖ant默认值? Also, according to this post and the ant documentation, I can set the global build.compiler property. 另外,根据这篇文章和ant文档,我可以设置全局build.compiler属性。 What do I set that property to be and how might I do that? 我该怎么设置该属性,我该怎么做?

In the javac task, try specifying the Java compiler by setting the executable attribute as follows: javac任务中,尝试通过设置executable属性来指定Java编译器,如下所示:

<property name="JDK1.6.dir" location="/usr/lib/jvm/java-6-openjdk-amd64" />
<property name="javac1.6" location="${JDK1.6.dir}/bin/javac" />

<target name="compile-tests">
  <javac executable="${javac1.6}" 
      fork="yes"
      includeantruntime="false" 
      srcdir="${test.dir}"
      destdir="${build.dir}" >
    <classpath refid="class.path" />
  </javac>
</target>

I think Ant compiler attribute is just to know which properties are supported by the compiler. 我认为Ant compiler属性只是为了知道编译器支持哪些属性。 In javac in general the attribute or option to pass to the compiler is target . javac中,传递给编译器的属性或选项是target

You should not even need to have a Java 6 compiler installed. 您甚至不需要安装Java 6编译器。

I thought it would be nice to add an updated answer to this question, since I recently dealt with a lot of headaches surrounding this same issue. 我认为为这个问题添加一个更新的答案会很好,因为我最近处理了围绕同一问题的许多令人头疼的问题。 My specific use case: My computer uses JRE1.8, but I needed ant to target JRE1.7 running on the server. 我的具体用例:我的计算机使用JRE1.8,但我需要ant来定位服务器上运行的JRE1.7。 The trick for me was to use the following attributes in combination (referencing the documentation ): 我的诀窍是组合使用以下属性(引用文档 ):

  • srcdir : Location of the java files. srcdir :java文件的位置。
  • destdir : Location to store the class files. destdir :存储类文件的位置。
  • includeantruntime : Whether to include the Ant run-time libraries in the classpath. includeantruntime :是否在类路径中包含Ant运行时库。 It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run. 通常最好将其设置为false,以便脚本的行为对运行它的环境不敏感。
  • source : Java language features accepted by compiler source :编译器接受的Java语言特性
  • target : Generate class files for specific JVM version (cross-compile). target :为特定JVM版本生成类文件(交叉编译)。
  • fork : Whether to execute javac using the JDK compiler externally. fork :是否在外部使用JDK编译器执行javac。
  • compiler : The compiler implementation to use. 编译器 :要使用的编译器实现。

You don't have to specify the executable file unless you have really specific dependencies (eg more recent enterprise version as opposed to latest free version). 没有 ,除非你真的有特定的依赖(而不是最新的免费版如最近的企业版本)来指定可执行文件。

Example: 例:

<javac srcdir="${source}"
       destdir="${java.output.dir}"
       includeantruntime="false"
       source="1.7"
       target="1.7"
       fork="yes"
       compiler="javac1.7">
</javac>

Use the "target" attribute of the Ant javac task to define the version of Java that you want your class files to be compatible with (so set it to "1.6" in your case). 使用Ant javac任务的“target”属性来定义您希望类文件兼容的Java版本(在您的情况下将其设置为“1.6”)。 See the task documentation for more details. 有关详细信息,请参阅任务文档

Talking about the basics of Ant, since it requires a JVM to run, it is suffice for it to recognize a JRE for this purpose. 谈论Ant的基础知识,因为它需要运行JVM,它就足以为此目的识别JRE。 However if the build process needs to compile classes then it will be dependent on a Java compiler and if it doesn't find one then the build will fail. 但是,如果构建过程需要编译类,那么它将依赖于Java编译器,如果它没有找到,那么构建将失败。

It is possible to set up Ant to use one of several Java compilers, however I will focus on Oracle's javac for the explanation below. 可以将Ant设置为使用多个Java编译器中的一个,但是我将重点关注Oracle的javac以获得下面的解释。 Here are the alternatives ... 以下是替代方案......

1) If the JRE recognized by Ant for its execution is a sub directory under the JDK, it will raise upto the JDK level and identify the required resources. 1)如果Ant识别其执行的JRE是JDK下的子目录,它将提升到JDK级别并识别所需的资源。

2) If JAVA_HOME environment variable is set pointing to a JVM (not JRE) then ant will be able to compile Java artifacts 2)如果JAVA_HOME环境变量设置为指向JVM(而不是JRE),那么ant将能够编译Java工件

3) Alternatively, as per the Ant documentation, JAVACMD can be set in a batch file specific to the OS (antrc_pre.bat for Windows) placed under your home directory (and not that of Ant) then it will recognize the JVM. 3)或者,根据Ant文档,JAVACMD可以设置在特定于操作系统(Windows的antrc_pre.bat)的批处理文件中,放在您的主目录下(而不是Ant的那个),然后它将识别JVM。 Please note JAVACMD should refer to the full path of Java.exe under a bin folder and not of JVM home. 请注意JAVACMD应该引用bin文件夹下的Java.exe的完整路径而不是JVM主目录。

4) Alternatively the Ant command line can take the list of libraries using -l options. 4)或者,Ant命令行可以使用-l选项获取库列表。 Pass a tools.jar to -l option and it would suffice. 将tools.jar传递给-l选项就足够了。

5) Finally fork attribute of JavaC task can be set as explained above. 5)最后可以如上所述设置JavaC任务的fork属性。 Keep in mind this is resource intensive and not recommended. 请记住,这是资源密集型,不推荐使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM