How to change Ant compiler to JDK 1.6

前端 未结 5 551
天涯浪人
天涯浪人 2020-12-16 03:52

I need to compile my source code to be compatible with jre 1.6. However, when I attempt to set the compiler attribute of the javac task to be javac1.6, ant will still compil

相关标签:
5条回答
  • 2020-12-16 04:12

    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). See the task documentation for more details.

    0 讨论(0)
  • 2020-12-16 04:14

    I think Ant compiler attribute is just to know which properties are supported by the compiler. In javac in general the attribute or option to pass to the compiler is target.

    You should not even need to have a Java 6 compiler installed.

    0 讨论(0)
  • 2020-12-16 04:18

    In the javac task, try specifying the Java compiler by setting the executable attribute as follows:

    <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>
    
    0 讨论(0)
  • 2020-12-16 04:22

    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. The trick for me was to use the following attributes in combination (referencing the documentation):

    • srcdir: Location of the java files.
    • destdir: Location to store the class files.
    • includeantruntime: Whether to include the Ant run-time libraries in the classpath. 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.
    • source: Java language features accepted by compiler
    • target: Generate class files for specific JVM version (cross-compile).
    • fork: Whether to execute javac using the JDK compiler externally.
    • compiler: The compiler implementation to use.

    You don't have to specify the executable file unless you have really specific dependencies (e.g. 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>
    
    0 讨论(0)
  • 2020-12-16 04:34

    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. 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.

    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. 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.

    2) If JAVA_HOME environment variable is set pointing to a JVM (not JRE) then ant will be able to compile Java artifacts

    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. Please note JAVACMD should refer to the full path of Java.exe under a bin folder and not of JVM home.

    4) Alternatively the Ant command line can take the list of libraries using -l options. Pass a tools.jar to -l option and it would suffice.

    5) Finally fork attribute of JavaC task can be set as explained above. Keep in mind this is resource intensive and not recommended.

    0 讨论(0)
提交回复
热议问题