从此
📄文章 #️⃣专题 🌐酷站 👨‍💻技术 📺 📱

Gradle(Kotlin)、Maven - 构建工具

综述

Gradle 构建工具Maven 仓库知名项目库

Gradle

    Gradle兼容Java版本列表 - https://docs.gradle.org/current/userguide/compatibility.html
    说明 - 兼容表的“Support for running Gradle”列是指Gradle自身JDK支持最高版本。
    Gradle顶级对象可用成员 - https://docs.gradle.org/current/dsl/org.gradle.api.Project.html
    NetBeans支持设置Gradle执行环境与IDE不同的Java Runtime了,安装JDK21,添加zip版JDK20。
    目录 gradle/wrapper/ 为可选文件夹,对Gradle不敏感者无需配置。

NetBeans的Gradle库通常不支持最新JDK,故应指定兼容的Gradle版本:
  Options->Java->Gradle->Gradle Distribution:Custom...
  不兼容则报 - The Java version: 22, that is selected for the project is not supported by Gradle 8.7.

Gradle首选kotlin,语法特例:
  列表初始化要用: options.compilerArgs = listOf("-Xdoclint:none", "-Xlint:none", "-nowarn")
  Kotlin不支持三元表达式,可换用 if(org.gradle.internal.os.OperatingSystem.current().isLinux()) "linux" else "windows";

Gradle Kotlin语法:
  val x = "string"
  var xx= "${x}"
  tasks.register("x") {
    from("other-dir", "build/outputs/aar/${x}-release.aar")
    into("build/${x}/")
  }

Gradle语法:
  获取-P传参值 providers.gradleProperty("mySecureRepositoryUsername").getOrNull()
  -D传参值用 providers.systemProperty(... 取环境变量用 providers.environmentVariable​(...

解决withJavadocJar()上传至远程仓库超时:systemProp.org.gradle.internal.http.socketTimeout=300000

指定JAVA_HOME:
  org.gradle.java.home=E:\\java\\jdk-19.0.2_windows-x64_bin\\jdk-19.0.2

排除依赖:
    implementation("eu.agno3.jcifs:jcifs-ng:2.1.10") {
        exclude(group = "org.bouncycastle", module = "bcprov-jdk15on")
    }

管理插件:
  buildscript { // build.gradle
    configurations.all {
        resolutionStrategy {
            //  Jib插件ASM未支持JDK22报Unsupported class file major version 61 - https://github.com/GoogleContainerTools/jib/pull/4252
            force 'org.ow2.asm:asm:9.7.1'
        }
    }
  }

  pluginManagement { // settings.gradle
    resolutionStrategy {
        eachPlugin {
            println(requested.id.id)
            if (requested.id.id == 'com.google.cloud.tools.jib') {
                useVersion("3.4.4") // 指定插件版本或插件全名:
                useModule("com.google.cloud.tools:jib-gradle-plugin:3.4.4")
            }
        }
    }
    repositories {
        mavenLocal()
        gradlePluginPortal()
    }
  }

Java Library 发布:
  publishing {
    publications {
        create("anyName") {
            from(components["java"]) // Android 则 components["release"] 且必须定义 android.publishing.singleVariant("release")
            groupId = "com.openle.our"
            artifactId = "java-lib-core"
            version = "1.0.0-SNAPSHOT"
        }
    }
    repositories {
        maven {
            url = uri("https://asia-northeast1-maven.pkg.dev/project-123456/maven-private-snapshot/")
            credentials { // 若配了credentials.username则可去掉artifactregistry插件,将协议改为“https://...”即可。
                //  编码key.json内容为base64后username改用_json_key_base64
                username = "_json_key"
                password =
                    file("../../../../docs/cloud/project-123456-any.json").readText()
            }
            authentication { create("basic") }
        }
    }
  }

Maven

命名规范:
  groupId使用Java Package命名格式,每个点号均对应一个文件夹目录;
  artifactId首选横杠分割,不使用点号和其他特殊符号,故整个算一个文件夹目录;
  version如果是快照版本则写死SNAPSHOT,否则用点号分隔的实际版本号。

Maven/mvn之POM用法