Search code examples
javagradlebuild.gradleminecraftminecraft-forge

Can I prevent the gradle from downloading jars by pre-downloading these jar files to local?


I am a new programmer to Gradle and Minecraft modding. I tried to set up the Minecraft forge build.gradle file using command

./gradlew genVSCodeRuns

in the command line of VSCode. Every time it failed in the process of downloading two required files with this kind of error message:

> Could not download icu4j-66.1.jar (com.ibm.icu:icu4j:66.1)
  > Could not get resource 'https://maven.minecraftforge.net/com/ibm/icu/icu4j/66.1/icu4j-66.1.jar'.
     > Premature end of Content-Length delimited message body (expected: 12,935,630; received: 6,397,856)

As I thought this was due to the internet problem, I tried to download the two required jar files to local and saved them in the libs folder. However, as I tried to implement them before Gradle started to download them automatically using the following script, Gradle kept on downloading them.

dependencies {
    implementation fileTree(dir:'libs',includes:['*jar'])
}

And I am now wondering what is the correct script to use these downloaded jar files as cache and prevent the build.gradle from downloading them again.

I viewed similar questions and do not know where to place my cache files. Only if I place the cache files correctly can I use the offline mode of Gradle to build the project.


Solution

  • I have figured out the solution by myself.

    The goal is to add the downloaded file to local cache to order Gradle to use this file cache instead of re-downloading the file from the internet.

    Before doing this, we need to make sure that the size of file is same as the required size. Because of my poor internet connection, I once downloaded only 6mb while the true size was 18mb.

    To add this to local cache, follow the steps below.

    Firstly, add an environmental variable GRADLE_USER_HOME.

    Then, the jar file caches will be stored at path:

    /%GRADLE_USER_HOME%/caches/modules-2/files-2.1/$package$/$project$/$version$/$sha1$/$proj-ver.jar$/
    

    For example, for icu4j-66.1.jar from URL 'https://maven.minecraftforge.net/com/ibm/icu/icu4j/66.1/icu4j-66.1.jar' will be stored at the following path:

    %GRADLE_USER_HOME%/caches/modules-2/files-2.1/com.ibm.icu/icu4j/66.1/$sha1$/icu4j-66.1.jar
    

    After that, we need to fetch the sha1 code. We can implement the Gradle code from https://gist.github.com/dagezi/9594839#file-calcsha1-gradle in build.gradle by creating a new task and print out the fetched sha1 code.

    Then, run build.gradle again, and the cached file will be implemented automatically. (There is no need to use offline mode)