Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: passing compilerarg into javac

I have ant script that compiles:

            <javac srcdir="${test.src.dir}" destdir="${test.dist.dir}">                ...                  <compilerarg value="-Xlint:unchecked" />             </javac> 

I need to increase heap memory of compiler, so I've put the following arguments into compileargs:

<compilerarg value="-Xlint:unchecked -Xms128m -Xmx512m" /> 

But I get an error in console:

[javac] javac: invalid flag: -Xms128m [javac] Usage: javac <options> <source files> 

Why does it happen? How do I increase memory used by javac?

like image 823
Denis Kniazhev Avatar asked Nov 09 '10 14:11

Denis Kniazhev


People also ask

What is includeAntRuntime in ant?

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. No; defaults to yes , unless build.sysclasspath property is set.

Which Ant task does the Java compiling?

Ant Javac task is used to compile Java source file. It scans source and destination directory to compile the source file. It only compiles if either . class is not present or .

What is the output of javac?

The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)


2 Answers

By default, <javac> runs in-process with Ant. It is a general limitation of Java that you can't adjust a JVM process' Xms and Xmx once that JVM process has launched. So, the error message that you are seeing is the software rejecting your attempt to violate this principle (using an unhelpful, unfriendly error message.)

If, however, you specify the attribute fork="true" on the <javac> tag you will be able to specify a new Xms and Xms. This is because fork instructs Ant to launch a new JVM subprocess in which to run javac. Because the JVM process is new, it gives Ant an acceptable opportunity to specify Xms and Xmx for it.


You might try something like this:

<project name="project" default="all" basedir="[yourvalue]">     <target name="all">         <javac srcdir="[yourvalue]" destdir="[yourvalue]" fork="true">             <!-- javac requires that -Xmx and -Xms be prefixed with -J -->             <compilerarg line="-J-Xms128m -J-Xmx512m" />         </javac>     </target> </project> 

(Notice I am using compilerarg line="" rather than compilerarg value="". The line attribute lets you specify multiple space-separated arguments. The value attribute is for passing a single argument.)


Ant will wait for the forked <javac> to exit, which happens after the javac process finishes its work (i.e. compiling). Ant then continues running the build script inside its own original JVM process. Ant will check if the forked javac failed or succeeded, and take the usual actions based on this information.


Performance

It's usually more performant to not fork javac, and instead simply tune the relevant memory settings for the initial Ant JVM overall. This is often (but not always) the best choice because launching a separate JVM is usually slower and takes more memory than simply allowing javac to run in-process.

If you are using the Ant-provided ant.bat or ant.sh to launch Ant, an easy way to tune Ant's Xms and Xmx is to define the environment variable ANT_OPTS to contain the arguments you want. There many ways to set environment variables, but you could just edit ant.bat:

set ANT_OPTS=-Xms128m -Xmx512m 
like image 107
11 revs Avatar answered Oct 04 '22 03:10

11 revs


Have you tried <jvmarg value="-Xmx512m" /> under Java task? For default ones you can use ANT_OPTS environment variable. I found this example, not very useful but has a build.xml.

To increase Javac heap space I found this while googling.

<javac fork="true"        srcdir="${basedir}/src"        destdir="${basedir}/build/classes"        classpath="${project.classpath}"        memoryinitialsize="256m"        memorymaximumsize="256m"> </javac> 

It's copied from this link. Setting fork to true is important.

like image 24
CoolBeans Avatar answered Oct 04 '22 03:10

CoolBeans