有一个Github上的开源Java项目,使用Gradle构建。现在需要发布到Maven中央仓库,其中需要配置一些中央仓库的用户、密码、签名等信息。这些都是私密信息,直接贴build脚本里面放在Github上肯定是不合适的。
Google了很久找到一个解决办法,在项目目录下创建gradle.properties
文件。存放用户名密码信息,但实际值为空:
version=0.4.5-SNAPSHOT signing.keyId= signing.password= signing.secretKeyRingFile= ossrhUsername= ossrhPassword=
现在只有一个问题,在使用travis-ci
自动构建的时候,提示错误:
:signArchives FAILED FAILURE: Build failed with an exception. * What went wrong: Could not evaluate onlyIf predicate for task ':signArchives'. > Neither path nor baseDir may be null or empty string. path='' basedir='/home/travis/build/xyalan/marathon-cli' * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED
我的build脚本部分内容:
artifacts { archives javadocJar, sourcesJar } ext.isReleaseVersion = !version.endsWith("SNAPSHOT") signing { required { boolean signed = isReleaseVersion && gradle.taskGraph.hasTask("uploadArchives") print signed signed } sign configurations.archives } uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { authentication(userName: ossrhUsername, password: ossrhPassword) } snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { authentication(userName: ossrhUsername, password: ossrhPassword) } pom.project { name 'marathon-cli' packaging 'jar' // optionally artifactId can be defined here description 'A application used as an example on how to set up pushing its components to the Central Repository.' url 'http://www.example.com/example-application' scm { connection 'scm:git:git://github.com/xyalan/marathon-cli.git' developerConnection 'scm:git:git@github.com:xyalan/marathon-cli.git' url 'https://github.com/xyalan/marathon-cli' } licenses { license { name 'The Apache License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id 'alan' name 'Alan Yang' email 'alanlhy@gmail.com' } } } } }
请问这个如何配置这个路径信息?
gradle-nexus-plugin
Credentials
In your ~/.gradle/gradle.properties you will need to set the mandatory Nexus credentials required for uploading your artifacts.
像上面的插件就是将用户密码默认放置在~
目录下中的。
还有Gradle
最大的一个优点就是灵活,因为是采用groovy
脚本直接编写的,所以你可以任何加载任何目录下的配置文件。比如在脚本中直接使用Properties
读取某个目录中的文件按照指定的格式加密或者其它都是OK的。
私密内容存文件,将文件加入到.ignore中,如这篇文章《Gradle实战:发布aar包到maven仓库》
看到你提到travi-ci,
travis 有两个加密命令 用来处理项目中的敏感信息
# 加密命令需要登录后在项目目录执行,travis 网站说加密得到 linux 或 mac 上执行
gem install travis # 需要ruby才能有gem
travis login
# 加密环境变量或者文件
travis encrypt env.global.PASSWORD=your_password --add # 添加环境变量 PASSWORD 到 .travis.yml,程序中直接使用环境变量
travis encrypt-file secret-file --add
参考:
https://docs.travis-ci.com/us...
https://docs.travis-ci.com/us...
https://uedsky.com/2016-06/tr...
直接使用占位符应该是最简单的方式吧:
gradle.properties 文件中填好私人信息,比如用户名、密码:
username= password=
然后在 build 脚本中直接使用 ${username}、${password} 代替,不过这个方法要求使用者配置自己的 gradle.properties,不配置的话会找不到占位符