TestNG retry mechanism was broken if launching tests with gradle

I have written a code for retrying a failed test case using TestNG retry mechanism. If I ran the tests directly, the retry works well. But if launching the tests with gradle, the test will be executed unlimited without end. Could you please take a look.

UIRetryAnalyzer.java

package test.testng.retry;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class UIRetryAnalyzer implements IRetryAnalyzer {

private static int MAX_RETRY_COUNT = 7;

private int count = 0;

public boolean isRetryAvailable() {
	return count < MAX_RETRY_COUNT ? true : false;
}

@Override
public boolean retry(ITestResult result) {
	boolean retry = false;
	if (isRetryAvailable()) {
		System.out.println("Retrying test " + result.getName() + " with status " + getResultStatusName(result.getStatus()) + " for the " + count
				+ " time(s).");
		retry = true;
		count++;
	}
	return retry;
}

public String getResultStatusName(int status) {
	String resultName = null;
	if (status == 1)
		resultName = "SUCCESS";
	if (status == 2)
		resultName = "FAILURE";
	if (status == 3)
		resultName = "SKIP";
	return resultName;
}

}

Test case

@Test(groups = {“retrygroup”},retryAnalyzer = UIRetryAnalyzer.class)
public void testRetry() {
System.out.println("testRetry " + System.currentTimeMillis());
Assert.fail();
}

Gradle Task

task testRetry (type: Test) {
useTestNG() {
includeGroups ‘retrygroup’
}
}