Gradle上传Maven私服

  1. 在module中添加maven插件,apply plugin:'maven'
  2. 添加上传任务uploadArchives
uploadArchives {
   repositories.mavenDeployer {
     repository(url:REPO_URL) {// 仓库地址
        // 这里配置上传账户的用户名和密码
       authentication(userName:REPO_USERNAME,password:REPO_PASSWORD)
     }
     pom.version = VERSION_NAME
     pom.groupId = GROUP_ID
     pom.artifactId = MODULE_ID
   }
}
  1. 一并上传源码和注释
task sourcesJar(type:Jar) {
   from android.sourceSets.main.java.srcDirs
   classifier = 'sources'
}
task javadoc(type:Javadoc) {
   failOnError false
   source = android.sourceSets.main.java.sourceFiles
   classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
   classpath += configurations.compile
}
task javadocJar(type:Jar,dependsOn:javadoc) {
   classifier = 'javadoc'
   from javadoc.destinationDir
}
artifacts {
   archives sourcesJar
   archives javadocJar
}
  1. build之后再右侧Gradle面板找到对应module下的uploadArchives任务双击执行

注意,如果maven仓库禁止匿名访问则需要在引用的时候配置如下

repositories {
// other repo...
   maven {
     url REPO_URL
     credentials {
       username REPO_USERNAME
       password REPO_PASSWORD
     }
   }
}

完整实例如下:

apply plugin: 'maven'
apply plugin: 'signing'Properties properties = new Properties();
properties.load(project.rootProject.file('local.properties').newDataInputStream())                                                                                                                         afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: 'http://nexus.hbc.tech/nexus/content/repositories/android-releases/') {
                    authentication(userName: properties.getProperty('maven.user'), password: properties.getProperty('maven.password'))
                }
                pom.project {
                    version '1.0.0'
                    artifactId 'app-list'
                    groupId 'com.hugboga.list'
                    packaging 'aar'
                    description 'This is app-list library'
                }
            }
        }
    }
    // 进行数字签名
    signing {
        required { gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    } task androidJavadocs(type: Javadoc) {
        // 设置源码所在的位置
        source = android.sourceSets.main.java.sourceFiles
    } task androidJavadocsJar(type: Jar) {
        // 指定文档名称
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    } // 生成sources.jar
    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    } // 产生相关配置文件的任务
    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

你可能感兴趣的:(Gradle上传Maven私服)