Gradle compileJava in Jenkins getting: OutOfMemoryError: GC overhead limit exceeded

When I build my gradle project with JDK 7 in my dev environment it builds fine. When I build in in the Jenkins CI environment compileJava task fails.

Here is the stack trace:

14:27:34.875 [ERROR] [system.err] The system is out of resources.
14:27:34.876 [ERROR] [system.err] Consult the following stack trace for details.
14:27:34.876 [ERROR] [system.err] java.lang.OutOfMemoryError: GC overhead limit exceeded
14:27:34.876 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.signature(MemberEnter.java:372)
14:27:34.876 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.visitMethodDef(MemberEnter.java:576)
14:27:34.876 [ERROR] [system.err] at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:669)
14:27:34.876 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:401)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:413)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.finishClass(MemberEnter.java:423)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.finish(MemberEnter.java:1066)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.complete(MemberEnter.java:1026)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.code.Symbol.complete(Symbol.java:421)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:821)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.code.Symbol$ClassSymbol.flags(Symbol.java:764)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:163)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:158)
14:27:34.877 [ERROR] [system.err] at com.sun.tools.javac.comp.Resolve.loadClass(Resolve.java:1063)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Resolve.findIdentInPackage(Resolve.java:1227)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.selectSym(Attr.java:2392)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:2283)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1677)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:2247)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1677)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418)
14:27:34.878 [ERROR] [system.err] at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:449)
14:27:34.879 [ERROR] [system.err] at com.sun.tools.javac.comp.Annotate.enterAttributeValue(Annotate.java:214)
14:27:34.879 [ERROR] [system.err] at com.sun.tools.javac.comp.Annotate.enterAnnotation(Annotate.java:181)
14:27:34.879 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.enterAnnotations(MemberEnter.java:783)
14:27:34.885 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter.access$300(MemberEnter.java:57)
14:27:34.885 [ERROR] [system.err] at com.sun.tools.javac.comp.MemberEnter$5.enterAnnotation(MemberEnter.java:751)
14:27:34.885 [ERROR] [system.err] at com.sun.tools.javac.comp.Annotate.flush(Annotate.java:109)
14:27:34.886 [ERROR] [system.err] at com.sun.tools.javac.comp.Annotate.enterDone(Annotate.java:101)
14:27:34.886 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ‘:my-platform:compileMySrcJava’

I have tried setting environment variables in Jenkins using envInject plugin as follows:

[EnvInject] - Injecting as environment variables the properties content
JAVA_OPTIONS=-XX:-UseGCOverheadLimit -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -Xms128m -Xmx2048m
GRADLE_OPTS=-XX:-UseGCOverheadLimit -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -Xms128m -Xmx2048m

This seems to have no effect.

Any advice on what to try?