前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gradle打包发布到maven的nexus仓库

gradle打包发布到maven的nexus仓库

作者头像
微笑的小小刀
发布2019-07-30 11:35:29
1.7K0
发布2019-07-30 11:35:29
举报
文章被收录于专栏:java技术大本营java技术大本营

前提背景

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

以前的解决方案及问题

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

代码语言:javascript
复制
apply from: '../maven_push.gradle'

maven_push.gradle的内容如下:

代码语言:javascript
复制
// 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依赖配置如下:

代码语言:javascript
复制
dependencies {
  compile project(":tools-mybatis")
  compile project(":tools-ops")
}

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

新的打包方案

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

代码语言:javascript
复制
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工具栏的插件菜单来发布了

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

更多文章请点击查看原文

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java技术大本营 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提背景
  • 以前的解决方案及问题
  • 新的打包方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档