Skip to content

Gradle

下载地址

bash
https://services.gradle.org/distributions/gradle-8.10-bin.zip
https://services.gradle.org/distributions/gradle-7.6.4-bin.zip
https://services.gradle.org/distributions/gradle-6.9.4-bin.zip

版本更新

bash
gradlew wrapper --gradle-version=8.10 --distribution-type bin
gradlew wrapper --gradle-version=7.6.4 --distribution-type bin
gradlew wrapper --gradle-version=6.9.4 --distribution-type=bin

仓库设置

阿里云 实际上偶尔会有部分库下载不了

bash
repositories {
    maven { url = uri("https://maven.aliyun.com/repository/public") }
    maven { url = uri("https://maven.aliyun.com/repository/spring") }
    maven { url = uri("https://maven.aliyun.com/repository/google") }
    maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
    maven { url = uri("https://maven.aliyun.com/repository/spring-plugin") }
    mavenLocal()
    mavenCentral()
    gradlePluginPortal()
}

备选

bash
repositories {
    maven { url = uri("https://mirrors.cloud.tencent.com/nexus/repository/maven-public") }
    maven { url = uri("https://mirrors.163.com/maven/repository/maven-public") }
    maven { url = uri("https://repo.huaweicloud.com/repository/maven") }
    maven { url = uri("https://maven.aliyun.com/repository/public") }
    maven { url = uri("https://maven.aliyun.com/repository/spring") }
    maven { url = uri("https://maven.aliyun.com/repository/google") }
    maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
    maven { url = uri("https://maven.aliyun.com/repository/spring-plugin") }
    mavenLocal()
    mavenCentral()
    gradlePluginPortal()
}

安装

RHEL

bash
# 解压缩
unzip gradle-8.7-bin.zip -d /data/app/tools
#
mv /data/app/tools/gradle-8.7 /data/app/tools/gradle
ln -s /data/app/tools/gradle-8.7 /data/app/tools/gradle

添加环境变量

bash
touch /etc/profile.d/gradle.sh
vi /etc/profile.d/gradle.sh

添加一下内容

bash
export GRADLE_HOME=/data/app/tools/gradle
export PATH=$PATH:$GRADLE_HOME/bin:.

启用环境变量

bash
source /etc/profile

拆分包

kotlin
var clearLibs = tasks.register<Delete>("clearLibs") {
    delete(layout.buildDirectory.dir("libs/libs-internal"));
    delete(layout.buildDirectory.dir("libs/libs-external"));
}

var copyLibs = tasks.register<Copy>("copyLibs") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from(configurations.runtimeClasspath)
    include("**/platform-*.jar")
    into(layout.buildDirectory.dir("libs/libs-internal"))
}

var copyExtLibs = tasks.register<Copy>("copyExtLibs") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from(configurations.runtimeClasspath)
    exclude("**/platform-*.jar")
    into(layout.buildDirectory.dir("libs/libs-external"))
}

tasks.named<BootJar>("bootJar") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    dependsOn(clearLibs)
    dependsOn(copyExtLibs)
    dependsOn(copyLibs)
    exclude("*.jar")
    manifest {
        attributes["Manifest-Version"] = 1.0
        attributes["Class-Path"] = configurations.runtimeClasspath.get().files.onEach {
            println(it.name)
        }.joinToString(" ") {
            if (it.name.startsWith("platform-")) "./libs-internal/" + it.name else "./libs-external/" + it.name
        }
    }
    archiveFileName.set("app.jar")
}