Skip to content

Instantly share code, notes, and snippets.

@XFY9326
Created November 28, 2021 19:57
Show Gist options
  • Save XFY9326/a9924bc0ae122967fd97d06354c16729 to your computer and use it in GitHub Desktop.
Save XFY9326/a9924bc0ae122967fd97d06354c16729 to your computer and use it in GitHub Desktop.
Gradle download path digest calculation
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GradleDigestUtils {
private static final String MD5_DIGEST = "MD5";
private static final Pattern VERSION_PATTERN = Pattern.compile("^(.*?)-(src|bin|all)$");
private static final String GRADLE_DOWNLOAD_URL = "https://services.gradle.org/distributions/gradle-%s.zip";
public static void main(String[] args) throws Exception {
if (args.length > 0) {
String input = args[0].trim();
Matcher matcher = VERSION_PATTERN.matcher(input);
if (matcher.matches()) {
String url = String.format(GRADLE_DOWNLOAD_URL, input);
MessageDigest messageDigest = MessageDigest.getInstance(MD5_DIGEST);
messageDigest.update(url.getBytes(StandardCharsets.UTF_8));
String digest = new BigInteger(1, messageDigest.digest()).toString(36);
System.out.println(digest);
} else {
System.out.println("Error: Version pattern error!");
System.exit(-1);
}
} else {
System.out.println("Error: Needs gradle version!");
System.exit(-1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment