Jenkins中Gradle插件报错:DefaultGroovyMavenDeployer无法获取属性
解决Jenkins中Gradle uploadArchives任务无法读取命令行属性的问题
我之前碰到过一模一样的场景,问题根源出在Groovy闭包的作用域差异上:当你在mavenDeployer的authentication闭包里直接写nexusReleaseUser这类属性时,Gradle会默认从当前闭包的上下文(也就是DefaultGroovyMavenDeployer实例)里查找,而不是从项目根作用域读取——这就是为什么单独用println nexusReleaseUser能正常输出,但放到闭包里就报错的原因。
解决方案1:明确指定项目作用域属性
修改你的uploadArchives任务,在属性前加上project.前缀,强制Gradle从项目级作用域读取这些命令行传入的参数:
uploadArchives { repositories { mavenDeployer { repository(url: 'https://mynexus.com/repository/maven-releases/') { authentication(userName: project.nexusReleaseUser, password: project.nexusReleasePassword) } snapshotRepository(url: 'https://mynexus.com/repository/maven-snapshots/') { authentication(userName: project.nexusSnapshotsUser, password: project.nexusSnapshotsPassword) } } } }
解决方案2:提前将属性赋值为局部变量
如果觉得每次写project.太繁琐,也可以在闭包外部先把属性存到局部变量里,再在authentication闭包里使用:
uploadArchives { // 先把项目属性赋值给局部变量 def releaseUser = nexusReleaseUser def releasePwd = nexusReleasePassword def snapshotUser = nexusSnapshotsUser def snapshotPwd = nexusSnapshotsPassword repositories { mavenDeployer { repository(url: 'https://mynexus.com/repository/maven-releases/') { authentication(userName: releaseUser, password: releasePwd) } snapshotRepository(url: 'https://mynexus.com/repository/maven-snapshots/') { authentication(userName: snapshotUser, password: snapshotPwd) } } } }
额外验证小提示
- 检查Jenkins传递的参数拼写(包括大小写),Gradle的属性是严格大小写敏感的
- 如果是多模块项目,要确保这些属性能传递到目标子模块,子模块中可以用
rootProject.property('nexusReleaseUser')来引用根项目属性
按上面的方式修改后,Jenkins执行Gradle构建时就能正确读取到命令行传入的认证属性了。
内容的提问来源于stack exchange,提问作者4lberto
相关产品推荐
相关产品推荐

