如何使用 maven-publish 和 artifactory-gradle-plugin 生成 maven-metata.xml?

How do I generate maven-metata.xml with maven-publish and the artifactory-gradle-plugin?

我在 groovy 中编写了一个 Gradle 插件,并使用 Gradle 构建了它。我有一个本地网络 Artifactory 服务器,我使用 Gradle Artifactory 插件和 Gradle 中的 maven-publish 插件发布结果。我有另一个 Gradle 依赖此插件作为依赖项的构建脚本。如果我列出我对特定版本的依赖项,我就能够让这一切正常工作。我曾尝试使用 Maven 版本范围(例如“[1.0,2.0]”),但这并没有说明找不到 maven-metadata.xml。我检查了 Artifactory,果然,它不存在。我需要做什么才能生成它,最好是在插件构建期间?

这是我的自定义 gradle 插件的 build.gradle 文件:

buildscript {
    repositories {
        maven {
            url "${artifactory_contextUrl}/plugins-release"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }
    }
    dependencies {
        classpath group: 'org.apache.directory.studio', name: 'org.apache.commons.io', version: '2.4'
        classpath group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.9'
    }
}

plugins {
    id 'com.jfrog.artifactory' version '3.0.1'
}

apply plugin: 'groovy'
apply plugin: 'maven-publish'

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'plugins-snapshot-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications ('mavenJava')
        }
    }
    resolve {
        repository {
            repoKey = 'libs-release'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

dependencies {
    compile gradleApi()
    compile localGroovy()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

我搜索了 Gradle、Artifactory 和 Maven 文档以了解 maven-metadata.xml 以及如何生成和部署。这是有道理的,我可能可以手动构建一个,但我找不到任何具体解释如何使用 maven-publish 插件或 artifactory-[=27 在 Gradle 中自动生成它的内容=]-插件。我不想手动更新文件,因为这会破坏自动化工作,而且我不想切换到 mvn,因为我已经在 Gradle.

上投入了很多

maven-metadata.xml 应该由 Artifactory 处理。 您在 Artifactory 中的本地存储库布局是什么?

必须将 groupId 添加到 publications 部分。实施后,maven-metadata.xml 文件已发布到工件存储库。

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'com.group'
        }
    }
}

我遇到了同样的问题,结果证明 Artifactory 存储库不是 Maven 存储库,而是一个通用存储库。我花了很长时间才注意到,因为我没有创建回购,我假设它是一个 Maven 回购,并且 deploying/resolving 否则按预期工作。
切换到 Maven 存储库后,maven-metadata.xml 是在发布时生成的。

接受的答案是正确的。我投了赞成票。但是,也有这个警告。

我有一个多模块项目,所以我将使用“allprojects”。如果您有 monolith/single-jar ( :( ).. 您可以使用与“allprojects”不同的范围。

他们这里的关键是你设置的“组”。 (以及版本)

所有项目{

apply plugin: 'java-library'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'


repositories {
    jcenter()
}

group = 'com.group'

version = '1.0-SNAPSHOT'

}

好的,现在 build.gradle(在我的 multi-module 项目中不是 root-build.gradle)(但根 build.gradle 中的值会相似)

下面是我的non-rootbuild.gradle文件的全部内容

// the "name" variable inside the publications/myPublicationName block is getting overwritten.  so create a variable here to capture the name (as the artifactid)
def artifactIdForPublicationBlockHolder = "${name}"


dependencies {
    testImplementation group: 'junit', name: 'junit', version: junitVersion
}

println("hey.here.read.me")
println("group=${group}")
println("version=${version}")
println("artifactId=${name}")


publishing {
    publications {
        myCustomPublicationName(MavenPublication) {
            // groupId, artifactId and version have defaults, so do not arbitrarily override : https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:publications

//your value below could be slightly different, look for *.jar after you do ./gradlew. build (note, this path value (of "./") is relative to the non-root-build.gradle path, not the overall root-build.gradle
"./build/libs/${artifactIdForPublicationBlockHolder}-${version}.jar"
        }
    }
}

正如 link 所说,您将获得

的默认值

// groupId, artifactId, version 都有默认值,请勿随意覆盖:https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:publications

您只需设置这些值,就像我在上面显示的

group = 'com.group' 
version = '1.0-SNAPSHOT'

代码

磨碎几次后
myCustomPublicationName(MavenPublication)

我发现我 custom-set 的东西越少越好。并且更喜欢搭载默认值...这意味着设置驱动默认值的值...在 build.gradle .. 而不是设置 myCustomPublicationName(MavenPublication)

改变里面的值

myCustomPublicationName(MavenPublication)

应该保留(恕我直言)到默认值对您不起作用时。这通常是极少数的时间。

注:

我的 non-root-gradle.build 顶部的“${name}” 被我的 multi-module 项目的目录结构填充。

我不知道它在非 multi-module 中是如何工作的,因为我从不写单体。

settings.gradle 我代码中的示例:

rootProject.name = 'com.me.myproject-rootProjectName'

include ':source:java:mydatalayer'
include ':source:java:mybizlogic'
include ':source:java:mydomain'

奖金报价如下:

Moreover, modular decomposition represents a key component of software quality. If you have a tightly coupled system, when you tweak one component, the whole system breaks. If you thought in terms of APIs, the intermodular boundaries are clear, so you can maintain and improve one module without affecting the others.

Massive refactorings prove difficult. If you built something as a monolithic system and then find you had repeated code all over the place, and you want to refactor it properly, you'll have a massive job. In contrast, if you wrote it as components, but you got some of the component boundaries a little wrong, you can tweak them easily.

-- Joshua Bloch, former Chief Java Architect at Google. Modular Decomposition Link