How to tell ant to build using a specific javac executable?

偶尔善良 提交于 2019-11-28 23:27:15
Kayser

If you are using Ant 1.6 or higher, you can set the javac attribute fork="yes". This gives you the ability to specify the path of your executable when using jikes, jvc, gcj, sj, or whatever version of javac you are using.

  <javac srcdir="${src}"
         destdir="${build}"
         fork="yes"
         executable="/opt/java/jdk1.1/bin/javac"
         compiler="javac1.1"
  />

The -D argument when calling ant will use a property from the command line inside of the Ant script. The form that it is used in is:

ant -Dmyvar=true

Where myvar is the name of the property, and true is the value you want to use in your script.

The easiest way then would be to use a property for your javac executable attributes.

 <target name="compile">  
   <javac srcdir="${src}"
     destdir="${build}"
     fork="${fork}"
     executable="${javac.executable}"
     compiler="${compiler}"/>  
 </target>

and then on the command line you could call:

ant compile -Djavac.executable=/usr/bin/local/jdk/javac -Dsrc=/home/src -Dbuild=/home/build -Dcompiler=javac1.6 -Dfork=true

From the javac task page:

It is possible to use different compilers. This can be specified by either setting the global build.compiler property, which will affect all tasks throughout the build, by setting the compiler attribute, specific to the current task or by using a nested element of any typedeffed or componentdeffed type that implements org.apache.tools.ant.taskdefs.compilers.CompilerAdapter. Valid values for either the build.compiler property or the compiler attribute are:

  • classic (the standard compiler of JDK 1.1/1.2) – javac1.1 and javac1.2 can be used as aliases.
  • modern (the standard compiler of JDK 1.3/1.4/1.5/1.6/1.7) – javac1.3 and javac1.4 and javac1.5 and javac1.6 and javac1.7 (since Ant 1.8.2) can be used as aliases.
  • jikes (the Jikes compiler).
  • jvc (the Command-Line Compiler from Microsoft's SDK for Java / Visual J++) – microsoft can be used as an alias.
  • kjc (the kopi compiler).
  • gcj (the gcj compiler from gcc).
  • sj (Symantec java compiler) – symantec can be used as an alias.
  • extJavac (run either modern or classic in a JVM of its own).

The way I read this, you need to write a class that implements CompilerAdapter and uses your compiler. Then typedef that task and use it in the javac compiler attribute.

I've done something similiar before, using the Ant Exec task. See http://ant.apache.org/manual/Tasks/exec.html

It allows you to call a specific system command. In our case, we needed to call Delphi (don't ask) to build some DLL's for a particular project. The exec command would also allow you to call gcj instead of javac.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!