Ant:将 compilerarg 传递给 javac
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ant: passing compilerarg into javac
提问by Denis Kniazhev
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
:
我需要增加编译器的堆内存,因此我将以下参数放入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
?
为什么会发生?如何增加使用的内存javac
?
采纳答案by Mike Clark
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.)
默认情况下,<javac>
与 Ant 一起在进程内运行。Java 的一般限制是您无法调整 JVM 进程,Xms
并且Xmx
一旦该 JVM 进程启动。因此,您看到的错误消息是软件拒绝您违反此原则的尝试(使用无益的、不友好的错误消息。)
If, however, you specify the attribute fork="true"
on the <javac>
tag you willbe 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.
但是,如果您fork="true"
在<javac>
标签上指定属性,您将能够指定一个新的Xms
和Xms
. 这是因为fork
指示 Ant 启动一个新的 JVM 子进程来运行javac
. 因为JVM进程是新的,它给了Ant来指定一个可接受的机会Xms
,并Xmx
为它。
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.)
(注意我使用的是compilerarg line=""
而不是compilerarg value=""
。该line
属性允许您指定多个以空格分隔的参数。该value
属性用于传递单个参数。)
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.
Ant 将等待分叉<javac>
退出,这发生在javac
进程完成其工作(即编译)之后。Ant 然后继续在它自己的原始 JVM 进程中运行构建脚本。Ant 将检查分叉是javac
失败还是成功,并根据这些信息采取通常的行动。
Performance
表现
It's usually more performant to notfork 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.
不fork通常性能更高javac
,而是简单地整体调整初始 Ant JVM 的相关内存设置。这通常(但并非总是)是最佳选择,因为启动单独的 JVM 通常比简单地允许javac
在进程内运行更慢并且占用更多内存。
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
:
如果您使用的是蚂蚁提供ant.bat
或ant.sh
推出蚂蚁,一个简单的方法来调整Ant的Xms
并且Xmx
是定义环境变量ANT_OPTS来包含你需要的参数。有很多方法可以设置环境变量,但您只需编辑ant.bat
:
set ANT_OPTS=-Xms128m -Xmx512m
回答by CoolBeans
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.
你<jvmarg value="-Xmx512m" />
在Java任务下试过吗?对于默认的,您可以使用 ANT_OPTS 环境变量。我发现这个例子,不是很有用,但有一个 build.xml。
To increase Javac heap space I found this while googling.
为了增加 Javac 堆空间,我在谷歌搜索时发现了这一点。
<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.
它是从这个链接复制的。将 fork 设置为 true 很重要。
回答by mikej
I don't think this problem is really related to ant. You would see the same message if you tried javac -Xms128m -Xmx512m
directly.
我不认为这个问题真的与蚂蚁有关。如果您javac -Xms128m -Xmx512m
直接尝试,您会看到相同的消息。
You need to use the -J
option for passing flags directly to the runtime system. e.g.
您需要使用-J
将标志直接传递给运行时系统的选项。例如
-J-Xms128m -J-Xmx512m
instead of just -Xms128m -Xmx512m
in your compilerarg
.
-J-Xms128m -J-Xmx512m
而不仅仅是-Xms128m -Xmx512m
在您的compilerarg
.
javac -X
is for passing nonstandard options to the compiler. If you run the command javac -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
.
javac -X
用于将非标准选项传递给编译器。如果您运行该命令javac -X
,它将显示一个合法选项列表,其中包括-Xlint
您使用过的选项。内存选项是底层 JVM 的设置,因此需要使用-J
.
Alternative
选择
There are memoryInitialSize
(equivalent to -Xms
) and memoryMaximumSiz
e (equivalent to -Xmx
) options for the javac
task so try those instead of using compilerargs
e.g.
该任务有memoryInitialSize
(相当于-Xms
)和memoryMaximumSiz
e(相当于-Xmx
)选项,javac
因此请尝试使用这些选项而不是使用compilerargs
eg
<javac srcdir="${test.src.dir}" destdir="${test.dist.dir}"
memoryInitialSize="128m"
memoryMaximumSize="512m">