欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gradle上传Nexus

程序员文章站 2022-03-12 07:57:57
文章目录Nexus 搭建下载解压启动发布代码注解1注解2注解3注解4注解5远程仓库的创建Nexus 搭建下载略解压略启动cd /Users/你的用户名/Downloads/nexus-3.27.0-03-mac/nexus-3.27.0-03/bin./nexus start在浏览器输入http://localhost:8081/#admin第一次启动会慢一些,会发现浏览器找不到页面,没关系,等一下,或者刷新一下发布代码先贴完整版代码apply plugin: 'com.andr...

Nexus 搭建

下载

nexus

解压

启动

cd /Users/你的用户名/Downloads/nexus-3.27.0-03-mac/nexus-3.27.0-03/bin

./nexus start

在浏览器输入http://localhost:8081/#admin

第一次启动会慢一些,会发现浏览器找不到页面,没关系,等一下,或者刷新一下

Android发布代码

先贴完整版代码

apply plugin: 'com.android.library'
// 1
apply plugin: "maven-publish"

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

task sourceJar(type: Jar) {
    if (hasAndroidPlugin()) {
        from android.sourceSets.main.java.srcDirs
        archiveClassifier = 'sources'
    } else if (hasJavaPlugin()){
        from sourceSets.main.allSource
        archiveClassifier = 'sources'
    }
}

def hasJavaPlugin() {
    if (plugins.hasPlugin("java-library")) {
        return true
    }
    return false
}

def hasAndroidPlugin() {
    if (plugins.hasPlugin("com.android.library")) {
        return true
    }
    return false
}


// 3
afterEvaluate { project ->
    tasks.all { Task task ->
        if (task.name.equalsIgnoreCase('publishAarPomPublishPublicationToMavenRepository')) {
            task.dependsOn tasks.getByName('assemble')
        }
    }
}

project.afterEvaluate {
    publishing {

        publications {
            aarPomPublish(MavenPublication) {
                groupId "com.test.demo"
                version "1.0.0"
                artifactId "yaoyan"
                // 2
                artifact bundleReleaseAar
                artifact("app.iml")
                artifact(sourceJar)
                // 4
                pom.withXml {
                    def root = asNode()
                    def dependenciesNode = root.appendNode('dependencies')
                    def repositoriesNode = root.appendNode('repositories')
                    ext.addDependency = { Dependency dep, String scope ->
                        if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                            return

                        final dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dep.group)
                        dependencyNode.appendNode('artifactId', dep.name)
                        dependencyNode.appendNode('version', dep.version)
                        dependencyNode.appendNode('scope', scope)

                        if (!dep.transitive) {
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            exclusionNode.appendNode('groupId', '*')
                            exclusionNode.appendNode('artifactId', '*')
                        } else if (!dep.properties.excludeRules.empty) {
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            dep.properties.excludeRules.each { ExcludeRule rule ->
                                exclusionNode.appendNode('groupId', rule.group ?: '*')
                                exclusionNode.appendNode('artifactId', rule.module ?: '*')
                            }
                        }
                    }

                    configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
                    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                }
            }
        }
        // 5
        repositories {
            maven {
                url "http://127.0.0.1:8081/repository/Releases/"
                credentials {
                    username = "admin" // The resolver user name
                    password = "xie123"
                }
            }
        }
    }
}




dependencies {
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation('com.android.support:recyclerview-v7:26.1.0') {
        exclude group: 'com.android.support', module: 'support-annotations'  // 根据组织名 + 构建名剔除
        // 你也可以分别通过  group 和 module 剔除 对应的模块。
    
}

注解1

apply plugin: "maven-publish"
添加maven-publish插件,是官方提供的插件

注解2

artifact是发布一个工件
这里可以指明一个task或者一个文件

例如

plugins {
    id 'maven-publish'
}

task sourceJar(type: Jar) {
  archiveClassifier = "sources"
}

publishing {
  publications {
    maven(MavenPublication) {
      artifact sourceJar // Publish the output of the sourceJar task
      artifact 'my-file-name.jar' // Publish a file created outside of the build
      artifact source: sourceJar, classifier: 'src', extension: 'zip'
    }
  }
}

也可以在将artifact写成闭包的形式,在闭包中配置classifier和extension
例如

plugins {
    id 'maven-publish'
}

task sourceJar(type: Jar) {
  archiveClassifier = "sources"
}

publishing {
  publications {
    maven(MavenPublication) {
      artifact(sourceJar) {
        // These values will be used instead of the values from the task. The task values will not be updated.
        classifier "src"
        extension "zip"
      }
      artifact("my-docs-file.htm") {
        classifier "documentation"
        extension "html"
      }
    }
  }
}

artifact bundleReleaseAar
这是一个task的任务名,可以通过 ./gradlew tasks --all 找到这个任务,该任务会生成release版本的aar包,同样还有bundleDebugAar

gradle上传Nexus

artifact("app.iml")
发送project下的app.iml文件

gradle上传Nexus
artifact(sourceJar)
传入task,生成资源文件

注解3

afterEvaluate { project ->
    tasks.all { Task task ->
        if (task.name.equalsIgnoreCase('publishAarPomPublishPublicationToMavenRepository')) {
            task.dependsOn tasks.getByName('assemble')
        }
    }
}

将publishAarPomPublishPublicationToMavenRepository任务放在aar包所在的任务后面去执行

这么长的名字如何得来?
gradle上传Nexus
gradle上传Nexus
由 publish + aarPomPublish(第一张图片的方法,生成的方法名首字母大写) + PublicationTo + maven(第二张图片的方法,生成的方法名首字母大写)+ Repository组成的

gradle上传Nexus

注解4

该方法主要是生成pom文件

gradle上传Nexus

注解5

设置远程仓库

远程仓库的创建

gradle上传Nexus
gradle上传Nexus
gradle上传Nexus
gradle上传Nexus
可以为repositories设置名称,默认为maven

repositories {
            maven {
                name = "xixix"
                url "http://127.0.0.1:8081/repository/Releases/"
                credentials {
                    username = "admin" // The resolver user name
                    password = "xie123"
                }
            }
        }

设置了name
gradle上传Nexus

java发布代码

与Android发布代码类似,只不过不是aar包是jar包
改写为from components.java

publications {
            aarPomPublish(MavenPublication) {
                groupId "com.test.demo"
                version "1.0.1"
                artifactId "hahaha"
                from components.java
                artifact(sourceJar)
		}
}       

本文地址:https://blog.csdn.net/qq_29951983/article/details/108568321

相关标签: gradle nexus