温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

gradle怎么打包发布到maven的nexus仓库

发布时间:2021-07-15 10:34:28 来源:亿速云 阅读:386 作者:chen 栏目:大数据

这篇文章主要介绍“gradle怎么打包发布到maven的nexus仓库”,在日常操作中,相信很多人在gradle怎么打包发布到maven的nexus仓库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”gradle怎么打包发布到maven的nexus仓库”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

前提背景

公司要封装一个工具类,把常用的mybatis,apollo,redis,初始化运行检查等等都封装在一起,项目建好了,但是打包发布nexus之后,别的项目死活拉不到依赖包,经查,是gradle打包时生成的pom文件中没有加入模块依赖.

以前的解决方案及问题

以前公司用gradle打包的时候,先新建一个maven_push.gradle ,然后在要打包的模块build.gradle中加上一句

apply from: '../maven_push.gradle'

maven_push.gradle的内容如下:

// The Maven plugin adds support for deploying artifacts to Maven repositories.// 一个可以让你把库上传到maven仓库的插件apply plugin: 'maven'// The signing plugin adds the ability to digitally sign built files and artifacts. These digital signatures can then be used to prove who built the artifact the signature is attached to as well as other information such as when the signature was generated.// 对库文件进行数字签名的插件,可以通过签名知道谁创建了这个库文件,签名的时间等等信息apply plugin: 'signing'
// 声明变量记录maven库地址def mavenRepositoryUrl// 判断是发布到正式库,还是snapshots库if (isReleaseBuild()) {    println 'RELEASE BUILD'    // 下面的库地址指向的是我们私有仓库的Releases 仓库    mavenRepositoryUrl = "http://xxx.com/repository/maven-public/"} else {    println 'SNAPSHOTS BUILD'    // 下面的库地址指向的是我们私有仓库的snapshots 仓库    mavenRepositoryUrl = "http://xxxx.com/repository/maven-snapshots/"}
// 根据我们在likelib下gradle.properties中声明的版本名称,来分辨是Release版本还是 snapshots版本def isReleaseBuild() {    return !VERSION_NAME.contains("SNAPSHOT");}
afterEvaluate { project ->    // 我们声明我们要执行的上传到maven的task    uploadArchives {        repositories {            mavenDeployer {                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }                // 我们类比下compile com.squareup.okhttp:okhttp:2.7.0                // artifactId 对应com.squareup.okhttp; groupId 对应okhttp;version对应2.7.0                // 这样就类似坐标的方式定位到了制定的库文件                pom.artifactId = POM_ARTIFACT_ID                pom.groupId = POM_GROUP_ID                pom.version = VERSION_NAME
               // 授权验证,这里也就是你登陆搭建的私服服务器时候的用户名\密码                repository(url: mavenRepositoryUrl) {                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())                }                // 这里是配置我们maven库需要的pom.xml文件的各个内容,具体意思我们在主目录gradle.properties中解释                pom.project {                    name POM_NAME                    packaging POM_PACKAGING                    description POM_DESCRIPTION                    url POM_URL
               }            }        }    }
   // 进行数字签名    signing {        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }        sign configurations.archives    }
}

这样是可以打包,但是对模块依赖就没办法打包,如gradle依赖配置如下:

dependencies {  compile project(":tools-mybatis")  compile project(":tools-ops")}

但打包出来的pom.xml文件里面并没有这两个依赖.

新的打包方案

上述通过 maven 插件可能还有别的姿试可以打包,但是没有找到,通过查找官方文档,找到了以下解决方案
引入 maven-publish,这个plugin , 然后在模块build.gradle中加入配置:

publishing {  publications {    maven(MavenPublication) {      groupId = group      artifactId = 'tools-starter'      version = version      from components.java    }  }  repositories {
   maven { url "http://xxxx.com/repository/maven-snapshots/"      credentials {        username 'xxx'        password 'xxxx'      }    }
 }}

这样就可以通过Idea右边gradle工具栏的插件菜单来发布了

gradle怎么打包发布到maven的nexus仓库

这样生成的pom.xml中是包含这两个依赖的.

到此,关于“gradle怎么打包发布到maven的nexus仓库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI