Gradle-来自build.gradle的“替代”行为/属性

时间:2019-06-21 08:01:53

标签: jenkins gradle

无论如何我都不是Gradle专家,所以请保持温柔...

我有一个Gradle构建,试图在Jenkins上运行。 build.gradle包含以下内容:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.lbl1.text = self.dataList[indexPath.row].0
    cell.lbl2.text = self.dataList[indexPath.row].1
    return cell
}

运行作业的Jenkins服务器无法访问“ some_internal_corporate_repo”。

由于我无法修改,我想知道是否有某种方法可以扩展或覆盖Jenkins服务器上的build.gradle,使其指向mavenCentral(或类似的内容),例如通过初始化文件或设置属性等?

预先感谢

编辑:最后,因为我使用的是Jenkins,所以我使用了Groovy支持(执行Groovy构建步骤)来解决我的问题:

repositories {
    maven {
        url "http://some_internal_corporate_repo"
    }
}

1 个答案:

答案 0 :(得分:1)

您可以定义 multiple repositories

  

声明的顺序决定了Gradle在运行时如何检查依赖项

repositories {
    maven {
        url "http://some_internal_corporate_repo"
    }
    mavenCentral() 
}

您可以使用属性来定义Maven存储库URL:

repositories {
    maven {
        url "${repositories_maven_url}"
    }
}

gradle.properties文件中

repositories_maven_url=maven_url

根据gradle documentationgradle.properties文件按以下顺序应用:

  • gradle.properties在项目根目录中。
  • gradle.properties在GRADLE_USER_HOME目录中。
  • 系统属性,例如在命令行上设置-Dgradle.user.home时。

或者您可以使用类似的

repositories {
        maven {
            url getMavenUrl()
        }
}

/**
 * Returns the url of the maven repo.
 * Set this value in your ~/.gradle/gradle.properties with repositories_maven_url key
 * If the property is not defined returns a default value
 * @return
 */
def getMavenUrl() {
    return hasProperty('repositories_maven_url') ? repositories_maven_url : "YOUR_DEFAULT_VALUE"
}
相关问题