如何将由Jenkins中的Gradle项目生成的工件发布到Nexus

时间:2017-12-14 09:14:07

标签: maven jenkins gradle nexus

我有一个Gradle项目,其中包含以下uploadArchives配置,可将其工件发布到Nexus:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'http://releasesrepo')
            snapshotRepository(url: 'http://snapshotsrepo')
        }
    }
}

如您所见,此配置中没有凭据。

这个版本将作为Freestyle项目从Jenkins运行,所以我只需要在Build - Invoke Gradle Script - Tasks - >中指定。 build upload

从Jenkins配置Nexus凭据的正确方法是什么,并将它们传递给Gradle taks(我不希望它们在带有源代码的Git仓库中)?

1 个答案:

答案 0 :(得分:1)

有三种不同的方式:

1.使用环境变量

def nexusUser = hasProperty('nexusUsername') ? nexusUsername : System.getenv('nexusUsername')
def nexusPassword = hasProperty('nexusPassword') ? nexusPassword : System.getenv('nexusPassword')
def nexusReleaseUrl = hasProperty('nexusReleaseUrl') ? nexusReleaseUrl : System.getenv('nexusReleaseUrl')
def nexusSnapshotUrl = hasProperty('nexusSnapshotUrl') ? nexusSnapshotUrl : System.getenv('nexusSnapshotUrl')

repositories {
    mavenDeployer {
        repository(url: nexusReleaseUrl) {
            authentication(userName: nexusUser, password: nexusPassword);
        }
        snapshotRepository(url: nexusSnapshotUrl) {
            authentication(userName: nexusUser, password: nexusPassword);
        }
    }

}

2.使用〜/ .gradle / gradle.properties(记得在用户jenkins下)

nexusUser=user
nexusPass=password

uploadArchives {
  def nexusUser = "${nexusUsername}"
  def nexusPassword = "${nexusPassword}"

  repositories {
    mavenDeployer {
        repository(url: 'your nexus releases') {
            authentication(userName: nexusUser, password: nexusPassword);
        }
        snapshotRepository(url: 'your nexus snapshot') {
            authentication(userName: nexusUser, password: nexusPassword);
        }
    }
  }
}

3.使用全局凭据

如果你想看看,你有一个例子here(适用于特拉维斯)