如何使用 Gradle 7.0 发布到 Maven Repo

How to publish to Maven Repo using Gradle 7.0

我正在寻找有关如何使用 Gradle 7.0.

将库发布到 public Maven 存储库的工作文档

我已经在 Maven(实际上是 Sonatype)注册了一个帐户,已经跳过了验证环节,所以似乎已经准备就绪。我尝试过手动发布,但准备自动化。

Maven 文档适用于 Gradle 6 及更低版本。所需的特定模块已从 Gradle 中删除,并且对此有明确的消息。 “maven”已被删除,你必须使用“maven-publish”。好像一切都变了,Maven上的文档对Gradle7

没用

然后就是 documentation on Gradle,据我所知这是错误的。它说要包括以下内容以应用插件:

plugins {
    id 'maven-publish'
}

足够好,但是文档然后说有任务,但那些任务不存在。文档声称有:

但是,这里是我可以从 运行ning tasks --all:

获得的任务
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
javadocJar - Assembles a jar archive containing the main javadoc.
sourcesJar - Assembles a jar archive containing the main sources.
testClasses - Assembles test classes.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':purpleLib'.
dependencies - Displays all dependencies declared in project ':purpleLib'.
dependencyInsight - Displays the insight into a specific dependency in project ':purpleLib'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of project ':purpleLib'.
projects - Displays the sub-projects of project ':purpleLib'.
properties - Displays the properties of project ':purpleLib'.
tasks - Displays the tasks runnable from project ':purpleLib'.

Publishing tasks
----------------
publish - Publishes all publications produced by this project.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
components - Displays the components produced by project ':purpleLib'. [deprecated]
dependentComponents - Displays the dependent components of components in project ':purpleLib'. [deprecated]
model - Displays the configuration model of project ':purpleLib'. [deprecated]
processResources - Processes main resources.
processTestResources - Processes test resources.

我知道需要更多配置。我破解了一些东西,并让 publishToMavenLocal 在 .m2 文件夹中创建文件,但在任何情况下我都不能真正发布,所以我重新开始,按照书来,但是这本书似乎完全坏了。

有没有人有 Gradle 7 发布到 Maven 时工作的构建文件的工作示例?

为了完整起见,我当前的 gradle.build 文件是:

/*
 build for the purple utilities library
 */
 
plugins {
    // the "id" commands below find an existing pluging code, 
    // create an instance of it, and put into the project.
    id 'java-library';
    id 'maven-publish';
}
java {
    withJavadocJar()
    withSourcesJar()
}
project.repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral();
}; 

project.version '3.0';
project.description = "Purple utilities for web programming";
project.group = 'com.purplehillsbooks.purple';
project.archivesBaseName = 'purple';

project.dependencies({

    // Use JUnit test framework.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    
    compileOnly 'org.apache.tomcat:tomcat-catalina:8.5.46'
})


project.test {
    useJUnitPlatform()
}

也许我必须降级到 Gradle 6?我一直在努力让事情与 7 一起工作,但似乎 Gradle 人的文档很糟糕。搜索非常困难,因为 10 次点击中有 9 次是关于从 Maven 检索,而不是发布,其余所有都是随机片段,大部分时间仅适用于 Gradle6。但没有对之前工作的深入经验Gradle 往往无法分辨。如果您有一个工作示例,将会非常有帮助。

不幸的是,这是我第一次尝试将 publication 自动化到 Maven,所以我没有旧的工作示例,也没有关于 Gradle 过去如何执行此操作的经验。我已经阅读了 Gradle 的所有文档,但其中太多内容根本没有描述我可以得到什么 运行 它是无用的,我发现自己怀疑它是否正确。

新的文档还没有部署,你可以在 sonatype jira ticket 上看到:https://issues.sonatype.org/browse/OSSRH-70091

这是我的配置,适用于 gradle 7.3:

repositories {
        maven {
            name = "ossrh"
            url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"

            credentials {
                username = System.getenv("MAVEN_USERNAME")
                password = System.getenv("MAVEN_PASSWORD")
            }
        }
}

最终需要的设置:

publishing {
    publications{

        mavenJava(MavenPublication){

            groupId = 'com.xxxxx.yyyy'
            artifactId = 'purple'
            version = '3.0'
            from components.java

            pom {
                name = 'purple'
                description = 'A set of useful utility classes for web applications.'
                url = 'https://github.com/xxxxx/yyyy'
                inceptionYear = '2017'

                licenses {
                    license {
                        name = 'MIT License'
                        url = 'http://www.opensource.org/licenses/mit-license.php'
                    }
                }
                developers {
                    developer {
                        id = 'xxxxx'
                        name = 'xxxxx'
                        email = 'gradle@xxxx.com'
                    }
                }
                scm {
                    connection='scm:git:git:github.com/agilepro/purple.git'
                    developerConnection='scm:git:https://github.com/agilepro/purple.git'
                    url='https://github.com/agilepro/purple'
                }
            }
        }
    }

    repositories {
        maven {
            name = "OSSRH"
            url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username = project.ossrhUsername
                password = project.ossrhPassword
            }
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}

需要 publish 设置才能显示某些任务。 mavenJava 不是方法调用,而是任务和签名中使用的对象的标识符。用户名和密码放置在未签入存储库的项目文件中。