我在Java项目中使用Gradle,并且在settings.gradle.kts个和build.gradle.kts个文件中使用相同的URL.

URL在两个文件中都是重复的,我想声明它一次,并以Gradle设计的方式在两个文件中使用它(没有黑客攻击).

我试过以下方法:

  1. 使用项目的extra,但可以找到一种方法在两个文件中使用它,但只能在构建文件中使用它.
  2. Declare the variable in the top of the settings.gradle.kts个 file and then try to access it from the build file - with no success.

我知道我可以使用gradle.properties文件,但它是每个开发人员的本地文件,我不希望每个人都将它添加到他们的文件中.

我正在努力寻找对球队透明的方式.

现在看起来是这样的:

settings.gradle.kts

pluginManagement {
    val nexusUsername: String by settings
    val nexusPassword: String by settings

    repositories {
        maven {
            credentials {
                username = nexusUsername
                password = nexusPassword
            }

            val nexusBaseUrl = System.getenv("NEXUS_BASE_URL") ?: "my-default-url"
            url = uri("$nexusBaseUrl/nexus/content/repositories/gradle-plugins/")
        }
    }
}

build.gradle.kts

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
    java
    checkstyle
    id("com.github.johnrengelman.shadow") version "7.1.2"
}


subprojects {

    val nexusUsername: String by project
    val nexusPassword: String by project

    repositories {
        maven {
            name = "NexusPublic"
            credentials {
                username = nexusUsername
                password = nexusPassword
            }
            val nexusBaseUrl = System.getenv("NEXUS_BASE_URL") ?: "my-default-url"
            url = uri("$nexusBaseUrl/nexus/content/groups/public/")
        }
    }

我希望nexusBaseUrl被声明一次,并在两个文件中使用.

推荐答案

对于每个开发者来说,Gradle.properties不应该是本地的,它不应该是gitignore的一部分.

我建议从环境变量中获取nexusUsernamenexusPassword,并在gradle.properties上设置nexusBaseUrl,这与您现在获取用户名和密码的方式相同.

要从环境变量中获取,请使用System.getenv("NEXUS_USERNAME"),如图here所示.

Java相关问答推荐

Selenium Java:无法访问IFRAME内部的元素

为什么Java的代码工作(if condition内部的实例)

有关手动创建的包的问题

路径映射未发生

如何以干净的方式访问深度嵌套的对象S属性?

JVM会优化这个数学运算吗?

通过合并Akka Streams中的多个慢源保持订购

将带有js文件的 bootstrap 程序导入maven项目时出错

按属性值从流中筛选出重复项

尽管通过中断请求线程死亡,但线程仍将继续存在

如何在JavaFX中制作鼠标透明stage

没有使用Lombok生成的参数

OAuth:登录后无法查看Google邮箱地址

Cucumber java-maven-示例表-未定义一步

JOOQ:批处理CRUD操作使用动态表定义,如何?

获取月份';s在java中非UTC时区的开始时间和结束时间

始终使用Spring Boot连接mongodb上的测试数据库

如何在java中从以百分比表示的经过时间和结束日期中找到开始日期

如何调查进程列表中不可见的活跃 MySQL 事务?

具有 DayOfWeek 列表的 JPA 实体