Gradle上传sources.jar和aar到jfrog artifactory。 artifactoryPublish 将生成两个 pom-default.xml,它会随机推送其中一个
Gradle upload sources.jar and aar to jfrog artifactory. artifactoryPublish will generate two pom-default.xml, it will pushlish one of them randomly
我有一个使用 kotlin 的 android 项目,它包含一个库模块,我想将这个库上传到 jfrog artifactory。我已成功将 aar 文件上传到 artifactory。现在我想上传 library-sources.jar 到 artifactory。但是当我执行gradle任务artifactoryPublish时,它会生成两个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"
}
......
pom-default.xml of aar_pub
<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>
pom-default.xml of mavenJava
<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
包含单个版本的所有工件。
您定义了两个相同的 Publications
(好吧,两者都有 aar 和 pom,只有其中一个有源 JAR)并试图将它们同时上传到相同的坐标。
- 不要为
aar
、jar
、pom
中的每一个都定义一个 Publication
。
- 将所有神器合并为一个
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 的任何任务之间没有正式关系。您必须在 publish
之前手动执行 assemble
。 (记得每次都这样做。)
考虑迁移到 Android Gradle 插件 3.6.0 或更高版本,它本身支持 maven-publish
插件。参见 documentation。这有以下好处:
- 您不必手动将依赖项写入 POM。
- 每当 AAR 更改时,出版物都会自动选取。 (您无需在干净构建后发布 nothing。您不必在
publish
之前手动执行 assemble
。)
- 在将
withSourcesJar()
支持添加到 AGP 之前,您仍然需要处理源 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 {
// ...
}
}
我有一个使用 kotlin 的 android 项目,它包含一个库模块,我想将这个库上传到 jfrog artifactory。我已成功将 aar 文件上传到 artifactory。现在我想上传 library-sources.jar 到 artifactory。但是当我执行gradle任务artifactoryPublish时,它会生成两个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"
}
......
pom-default.xml of aar_pub
<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>
pom-default.xml of mavenJava
<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
包含单个版本的所有工件。
您定义了两个相同的 Publications
(好吧,两者都有 aar 和 pom,只有其中一个有源 JAR)并试图将它们同时上传到相同的坐标。
- 不要为
aar
、jar
、pom
中的每一个都定义一个Publication
。 - 将所有神器合并为一个
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 的任何任务之间没有正式关系。您必须在 publish
之前手动执行 assemble
。 (记得每次都这样做。)
考虑迁移到 Android Gradle 插件 3.6.0 或更高版本,它本身支持 maven-publish
插件。参见 documentation。这有以下好处:
- 您不必手动将依赖项写入 POM。
- 每当 AAR 更改时,出版物都会自动选取。 (您无需在干净构建后发布 nothing。您不必在
publish
之前手动执行assemble
。) - 在将
withSourcesJar()
支持添加到 AGP 之前,您仍然需要处理源 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 {
// ...
}
}