How to change Ant compiler to JDK 1.6

I need to compile the source code for compatibility with jre 1.6. However, when I try to configure the javac task compiler attribute as javac1.6, ant will compile my code using javac1.7. I also tried installing the β€œmodern” version of the compiler, and that didn't 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 installed in JDK 1.6:

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

My ant version: Apache Ant (TM) version 1.8.2

According to this post , ant uses its own compiler. How to override the default ant value? Also, according to this post and the ant documentation, I can set the build.compiler global property. How should I set this property and how to do it?

+6
source share
4 answers

I think the Ant compiler attribute should know which properties are supported by the compiler. In javac an attribute or option is generally passed to the target compiler.

You do not even need to install the Java 6 compiler.

+5
source

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> 
+24
source

Use the "target" attribute of the Ant javac task to determine the version of Java to which you want to combine class files, so set the value to "1.6" in this case). See the documentation for more details.

+1
source

Speaking of the basics of Ant, since a JVM is required to run, just recognizing the JRE is enough. However, if the build process must compile classes, then it will depend on the Java compiler, and if it does not find it, then the assembly will fail.

You can configure Ant to use one of several Java compilers, however I will talk about Oracle javac for an explanation below. Here are the alternatives ...

1) If the JRE recognized by Ant for its execution is a subdirectory in the JDK, it will raise the JDK level and determine the necessary resources.

2) If the JAVA_HOME environment variable is set to JVM (and not JRE), then Ant will be able to compile Java artifacts

3) Alternatively, according to the Ant documentation, JAVACMD can be configured in an OS-specific batch file (antrc_pre.bat for Windows) located under your home directory (and not with Ant), then it will recognize the JVM. Please note that JAVACMD must reference the full path of Java.exe in the bin folder, and not the JVM home.

4) As an alternative, the Ant command line can accept a list of libraries using the -l options. Pass the tools.jar parameter to -l and that will be enough.

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

0
source

Source: https://habr.com/ru/post/920844/


All Articles