使用创建子项目的 Ant 任务,例如 <antcall>
并且<ant>
可能由于以下错误之一而在重复调用时导致构建失败:
- java.lang.OutOfMemoryError: PermGen 空间
- java.lang.OutOfMemoryError:Java 堆空间
仅当正在调用的任务之一使用<typedef>
or定义时才会发生错误<taskdef>
,并且在使用与 Ant 捆绑在一起的任务(例如<javadoc>
.
有没有办法在OutOfMemoryError
不增加最大 Java 堆大小的情况下避免这种情况?虽然增加堆大小暂时有效,但如果添加更多内存密集型任务,问题仍然会重新出现。
以下示例任务和关联build.xml
文件
OutOfMemoryError
在我的 Linux 机器上导致 Java 堆设置为 10 MB(用于测试)。Ant 任务构造了一个需要大量内存的对象(在本例中为闭包模板大豆模块的 Guice 注入器),然后使用<antcall>
.
CreateGuiceInjectorTask.java
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.template.soy.SoyModule;
import org.apache.tools.ant.Task;
/** Custom Ant task that constructs a Guice injector. */
public final class CreateGuiceInjectorTask extends Task {
private Injector injector;
public CreateGuiceInjectorTask() {
injector = Guice.createInjector(new SoyModule());
}
public void execute() {
System.out.println("Constructed Guice injector...");
}
}
构建.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="out-of-memory-test" basedir=".">
<property name="build.dir" location="${basedir}/build" />
<property name="CreateGuiceInjectorTask.jar"
location="${build.dir}/CreateGuiceInjectorTask.jar" />
<taskdef name="create-injector"
classname="CreateGuiceInjectorTask"
classpath="${CreateGuiceInjectorTask.jar}" />
<target name="call-create-injector">
<create-injector />
</target>
<target name="test"
description="Create multiple injectors until out of memory">
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
<antcall target="call-create-injector" />
</target>
</project>
测试输出:
$ ant test
test:
call-create-injector:
[create-injector] Constructed Guice injector...
call-create-injector:
[create-injector] Constructed Guice injector...
...
call-create-injector:
BUILD FAILED
Could not create type create-injector due to java.lang.OutOfMemoryError: Java heap space