Ant:将compilerarg传递给javac
我有可以编译的 ant 脚本:
<javac srcdir="${test.src.dir}" destdir="${test.dist.dir}">
...
<compilerarg value="-Xlint:unchecked" />
</javac>
我需要增加编译器的堆内存,因此我将以下参数放入 compileargs
中:
<compilerarg value="-Xlint:unchecked -Xms128m -Xmx512m" />
但我在控制台中收到错误:
[javac] javac: invalid flag: -Xms128m
[javac] Usage: javac <options> <source files>
为什么会发生这种情况?如何增加 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
与 Ant 一起在进程内运行。 Java 的一个普遍限制是,一旦 JVM 进程启动,您就无法调整 JVM 进程的Xms
和Xmx
。因此,您看到的错误消息是软件拒绝您违反此原则的尝试(使用无用的、不友好的错误消息)。但是,如果您指定属性
fork="true"
您将能够通过
标记指定新的Xms
和Xms
。这是因为fork
指示 Ant 启动一个新的 JVM 子进程,在其中运行javac
。由于 JVM 进程是新的,因此它为 Ant 提供了可接受的机会为其指定Xms
和Xmx
。您可以尝试这样的操作:(
注意我使用的是
compilerarg line=""
而不是compilerarg value=""
。line
属性让您指定多个以空格分隔的参数。value
属性用于传递单个参数。)Ant 将等待分叉的
退出,这发生在javac
进程完成其工作(即编译)。然后,Ant 继续在其自己的原始 JVM 进程中运行构建脚本。 Ant 将检查分叉的 javac 是否失败或成功,并根据此信息采取通常的操作。性能
通常,不 fork
javac
会获得更高的性能,而只需整体调整初始 Ant JVM 的相关内存设置。这通常(但并非总是)最好的选择,因为启动单独的 JVM 通常比简单地允许javac
在进程内运行更慢并且需要更多内存。如果您使用 Ant 提供的
ant.bat
或ant.sh
来启动 Ant,这是调整 Ant 的Xms
和的简单方法>Xmx
就是定义环境变量ANT_OPTS来包含你想要的参数。设置环境变量的方法有很多种,但你可以直接编辑ant.bat
:By default,
<javac>
runs in-process with Ant. It is a general limitation of Java that you can't adjust a JVM process'Xms
andXmx
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 newXms
andXms
. This is becausefork
instructs Ant to launch a new JVM subprocess in which to runjavac
. Because the JVM process is new, it gives Ant an acceptable opportunity to specifyXms
andXmx
for it.You might try something like this:
(Notice I am using
compilerarg line=""
rather thancompilerarg value=""
. Theline
attribute lets you specify multiple space-separated arguments. Thevalue
attribute is for passing a single argument.)Ant will wait for the forked
<javac>
to exit, which happens after thejavac
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 forkedjavac
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 allowingjavac
to run in-process.If you are using the Ant-provided
ant.bat
orant.sh
to launch Ant, an easy way to tune Ant'sXms
andXmx
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 editant.bat
:您是否在 Java 任务下尝试过
?对于默认的,您可以使用 ANT_OPTS 环境变量。我发现这个 示例,不是很很有用,但有一个 build.xml。为了增加 Javac 堆空间,我在谷歌搜索时发现了这一点。
它是从此链接复制的。将 fork 设置为 true 很重要。
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.
It's copied from this link. Setting fork to true is important.
我不认为这个问题与ant有真正的关系。如果您直接尝试
javac -Xms128m -Xmx512m
,您会看到相同的消息。您需要使用
-J
选项将标志直接传递到运行时系统。例如,compilerarg
中的-J-Xms128m -J-Xmx512m
而不是-Xms128m -Xmx512m
。javac -X
用于将非标准选项传递给编译器。如果您运行命令javac -X
,它将显示合法选项列表,其中包括您使用的-Xlint
。内存选项是底层 JVM 的设置,因此需要使用-J
。替代
还有
memoryInitialSize
(相当于-Xms
)和memoryMaximumSiz
e(相当于-Xmx< /code>) javac 任务的选项,因此请尝试使用这些选项而不是使用compilerargs 例如
I don't think this problem is really related to ant. You would see the same message if you tried
javac -Xms128m -Xmx512m
directly.You need to use the
-J
option for passing flags directly to the runtime system. e.g.-J-Xms128m -J-Xmx512m
instead of just-Xms128m -Xmx512m
in yourcompilerarg
.javac -X
is for passing nonstandard options to the compiler. If you run the commandjavac -X
it will display a list of legal options which include the-Xlint
that you've used. The memory options are settings for the underlying JVM, hence the need to use-J
.Alternative
There are
memoryInitialSize
(equivalent to-Xms
) andmemoryMaximumSiz
e (equivalent to-Xmx
) options for thejavac
task so try those instead of usingcompilerargs
e.g.