gpt4 book ai didi

android - Gradle 将sources.jar 和aar 上传到jfrog Artifact 。 artifactoryPublish 会生成两个 pom-default.xml,它会随机推送其中一个

转载 作者:行者123 更新时间:2023-12-05 00:18:45 36 4
gpt4 key购买 nike

我有一个带有 kotlin 的 android 项目,它包含一个库模块,我想将此库上传到 jfrog artifactory。我已成功将 aar 文件上传到 artifactory。现在我想将 library-sources.jar 上传到 artifactory。但是当我执行 gradle 任务 神器发布 ,它会生成两个 pom-default.xml 文件,并随机发布其中一个。
项目 build.gradle

    buildscript {
ext.kotlin_version = "1.4.30"
repositories {
maven {
url "http://localhost:8081/artifactory/my_virtual_repo/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"
}
}
allprojects {
repositories {
maven {
url "http://localhost:8081/artifactory/my_virtual_repo/"
}
}
}
库 build.gradle
......

apply plugin: "com.jfrog.artifactory"
apply plugin: "maven-publish"

def MAVEN_LOCAL_PATH = "http://localhost:8081/artifactory"
def GROUP_ID = "com.xxx.artifactlib"
def ARTIFACT_ID = "artifactlib"
def VERSION_NAME = "1.3"

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

task sourcesJar(type: Jar) {
group = 'jar'
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

artifacts {
archives sourcesJar
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId = GROUP_ID
artifactId = ARTIFACT_ID
version = VERSION_NAME
artifact tasks.sourcesJar
pom.withXml {
def dependencies = asNode().appendNode("dependencies")
configurations.api.allDependencies.each {
def dependency = dependencies.appendNode("dependency")
print(it.group)
dependency.appendNode("groupId", it.group)
dependency.appendNode("artifactId", it.name)
dependency.appendNode("version", it.version)
}
}
}
}

publications {
aar_pub(MavenPublication) {
groupId = GROUP_ID
artifactId = ARTIFACT_ID
version = VERSION_NAME

artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")

pom.withXml {
def dependencies = asNode().appendNode("dependencies")
configurations.api.allDependencies.each {
def dependency = dependencies.appendNode("dependency")
dependency.appendNode("groupId", it.group)
dependency.appendNode("artifactId", it.name)
dependency.appendNode("version", it.version)
}
}
}
}
}

artifactoryPublish {
contextUrl = MAVEN_LOCAL_PATH
publications("mavenJava", "aar_pub")

clientConfig.publisher.repoKey = "my_local_repo"
clientConfig.publisher.username = "xxx"
clientConfig.publisher.password = "xxx"
}
......
aar_pub 的 pom-default.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.artifactlib</groupId>
<artifactId>artifactlib</artifactId>
<version>1.3</version>
<packaging>aar</packaging>
<dependencies/>
</project>
mavenJava 的 pom-default.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.artifactlib</groupId>
<artifactId>artifactlib</artifactId>
<version>1.3</version>
<packaging>pom</packaging>
<dependencies/>
</project>
如何上传给定的 pom 文件(如 aar_pub 的 pom-default)?谢谢你。

最佳答案

单个 Publication 包含单个版本的所有 Artifactory 。
您定义了两个相同的 Publications(好吧,两者都有 aar 和 pom,其中只有一个有源 JAR)并试图同时将它们上传到相同的坐标。

  • 不要为每个 Publicationaarjar 等定义一个 pom
  • 将所有 Artifactory 合并到一个 Publication 中。

  • 在你的情况下:
    publishing {
    publications {
    mavenJava(MavenPublication) {
    groupId = GROUP_ID
    artifactId = ARTIFACT_ID
    version = VERSION_NAME

    artifact tasks.sourcesJar
    artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")

    pom.withXml {
    def dependencies = asNode().appendNode("dependencies")
    configurations.api.allDependencies.each {
    def dependency = dependencies.appendNode("dependency")
    print(it.group)
    dependency.appendNode("groupId", it.group)
    dependency.appendNode("artifactId", it.name)
    dependency.appendNode("version", it.version)
    }
    }
    }
    }
    }

    artifactoryPublish {
    publications("mavenJava")
    }
    潜在问题
    出版物与产生 AAR 的任何任务之间没有正式的关系。您必须在 assemble 之前手动执行 publish 。 (记住每次都这样做。)
    考虑迁移到原生支持 maven-publish 插件的 Android Gradle 插件 3.6.0 或更高版本。见 documentation 。这有以下好处:
  • 您不必手动将依赖项写入 POM。
  • 每当 AAR 更改出版物时,它都会自动拾取它。 (在干净的构建之后您不会发布任何内容。您不必在 assemble 之前手动执行 publish 。)
  • 在向 AGP 添加对 withSourcesJar() 的支持之前,您仍然必须处理源 JAR。见 issue

  • 在您的情况下,它看起来像这样:
    // Because the components are created only during the afterEvaluate phase, you must
    // configure your publications using the afterEvaluate() lifecycle method.
    afterEvaluate {
    publishing {
    publications {
    mavenJava(MavenPublication) {
    // Applies the component for the release build variant.
    from components.release
    artifact tasks.sourcesJar

    groupId = GROUP_ID
    artifactId = ARTIFACT_ID
    version = VERSION_NAME
    }
    }
    }

    artifactoryPublish {
    // ...
    }
    }

    关于android - Gradle 将sources.jar 和aar 上传到jfrog Artifact 。 artifactoryPublish 会生成两个 pom-default.xml,它会随机推送其中一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66622975/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com