How can I tell ant
to use a specific javac executable from the command line?
I have an installation of gcj, built as part of gcc, within a library we distribute, and I'd like to have a particular piece of Java software built against that. However, it just seems to use the system-gcc, and options such as "-Dbuild.compiler" seem to want me to specify some kind of Java class rather than a filepath.
I was hoping for something similar to CC in Makefiles.
I'm sure it's something really simple, and I'm just being stupid.
To be clear, I'd like to avoid editing the build file myself if possible. Is there not some standard way to simply specify the compiler on the command-line to ant? I don't mind the assumption that the buildfile is "well-behaved" in some sense.
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.