一、添加插件
apply plugin: 'maven-publish'
二、添加如下配置
//打包源码 task sourcejar(type: jar) { from sourcesets.main.alljava } publishing { publications { maven(mavenpublication) { //指定group/artifact/version信息,可以不填。默认使用项目group/name/version作为groupid/artifactid/version groupid project.group artifactid project.name version project.version //如果是war包填写components.web,如果是jar包填写components.java from components.java //配置上传源码 artifact sourcejar { classifier "sources" } } } repositories { maven { //指定要上传的maven私服仓库 url = "http://jenkins.maxrocky.com:8080/maven/content/repositories/thirdparty/" //认证用户和密码 credentials { username 'admin' password 'maxrocky5721' } } } }
三、执行以下命令进行上传
gradle publishmavenpublicationtomavenrepository
正常的输出结果
➜ gradle publishmavenpublicationtomavenrepository :common:generatepomfileformavenpublication :common:compilejava up-to-date :common:processresources up-to-date :common:classes up-to-date :common:jar up-to-dat :common:sourcejar up-to-date :common:publishmavenpublicationtomavenrepository upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.jar upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.jar.sha1 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.jar.md5 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.pom upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.pom.sha1 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.pom.md5 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0-sources.jar upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0-sources.jar.sha1 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0-sources.jar.md5 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/maven-metadata.xml upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/maven-metadata.xml.sha1 upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/maven-metadata.xml.md5 build successful total time: 1.906 secs
四、遇见的问题
有时候会出错,如下输出
:common:generatepomfileformavenpublication :common:compilejava up-to-date :common:processresources up-to-date :common:classes up-to-date :common:jar up-to-date :common:sourcejar up-to-date :common:publishmavenpublicationtomavenrepository upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.jar could not transfer artifact com.kingboy:common:jar:1.0 from/to remote (http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/): could not write to resource 'com/kingboy/common/1.0/common-1.0.jar' upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0.pom could not transfer artifact com.kingboy:common:pom:1.0 from/to remote (http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/): could not write to resource 'com/kingboy/common/1.0/common-1.0.pom' upload http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/com/kingboy/common/1.0/common-1.0-sources.jar could not transfer artifact com.kingboy:common:jar:sources:1.0 from/to remote (http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/): could not write to resource 'com/kingboy/common/1.0/common-1.0-sources.jar' :common:publishmavenpublicationtomavenrepository failed failure: build failed with an exception. * what went wrong: execution failed for task ':common:publishmavenpublicationtomavenrepository'. > failed to publish publication 'maven' to repository 'maven' > failed to deploy artifacts: could not transfer artifact com.kingboy:common:jar:1.0 from/to remote (http://jenkins.maven.com:8080/maven/content/repositories/thirdparty/): could not write to resource 'com/kingboy/common/1.0/common-1.0.jar' * try: run with --stacktrace option to get the stack trace. run with --info or --debug option to get more log output. build failed
我的是因为网络原因,换个网络,多试几次就成功了,去stackoverflow上看了以下,没有太明确的原因。
以上就是gradle使用maven-publish发布jar包上传到私有maven配置的详细内容,更多关于gradle发布jar包到私有maven的资料请关注其它相关文章!