简体   繁体   English

如何使用 Gradle 7.0 发布到 Maven 存储库

[英]How to publish to Maven Repo using Gradle 7.0

I am looking for working documentation on how to publish a library to the public Maven repository using Gradle 7.0.我正在寻找有关如何使用 Gradle 7.0 将库发布到公共 Maven 存储库的工作文档。

I have signed up for an account at Maven (Sonatype Actually), have jumped through the verification hoops, and so that seem to be ready to go.我已经在 Maven(实际上是 Sonatype)上注册了一个帐户,已经跳过了验证环节,因此似乎准备好了 go。 I have tried manual publishing, but ready to automate.我尝试过手动发布,但已准备好自动化。

The Maven Documentation is for Gradle 6 and lower. Maven 文档适用于 Gradle 6 及更低版本。 The specific module necessary has been removed from Gradle, and there is a clear message to this effect.所需的特定模块已从 Gradle 中删除,并且有明确的消息表明此效果。 "maven" has been removed, you have to use "maven-publish". “maven”已被删除,你必须使用“maven-publish”。 It seems that everything has changed, and the documentation on Maven is useless for Gradle 7似乎一切都变了,Maven 上的文档对 Gradle 7 没用

So then there is the documentation on Gradle which is just wrong as far as I can see.那么有关于 Gradle 的文档,据我所知,这是错误的。 It says to include the following to apply the plugin:它说包括以下内容以应用插件:

plugins {
    id 'maven-publish'
}

Good enough, but the documentation then says there are tasks, but those tasks are not there.足够好,但是文档说有任务,但那些任务不存在。 The documentation claims there is:该文件声称有:

  • generatePomFileForPubNamePublication - there is no generatePomFile task generatePomFileForPubNamePublication - 没有 generatePomFile 任务
  • publishPubNamePublicationToRepoNameRepository - not there either publishPubNamePublicationToRepoNameRepository - 也不存在
  • publishPubNamePublicationToMavenLocal - I have "publishToMavenLocal", is that the same thing? publishPubNamePublicationToMavenLocal - 我有“publishToMavenLocal”,是一样的吗?
  • publish - there is a task for this, but it does nothing.发布- 有一个任务,但它什么都不做。 It says it is "successful" but it does nothing.它说它是“成功的”,但它什么也没做。
  • publishToMavenLocal - it exists, but when I run it, it does nothing. publishToMavenLocal - 它存在,但是当我运行它时,它什么也不做。 It is documented that it creates files in the users/USER/.m2 folder, but in fact nothing is placed there.据记载,它在 users/USER/.m2 文件夹中创建文件,但实际上没有放置任何文件。

However, here are the tasks that I have available from running 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.

I know there is more configuration needed.我知道需要更多配置。 I have hacked some things, and gotten publishToMavenLocal to create files in the.m2 folder, but under no circumstances can I actually publish, so I am starting over, going by the book, however the book seems to be completely broken.我已经破解了一些东西,并让 publishToMavenLocal 在 .m2 文件夹中创建文件,但在任何情况下我都无法真正发布,所以我重新开始,按书行事,但是这本书似乎完全被打破了。

Does anyone have a working example of Gradle 7 build file that WORKS when publishing to Maven?有没有人有 Gradle 7 构建文件的工作示例,该文件在发布到 Maven 时有效?

For completeness, my current gradle.build file is:为了完整起见,我当前的 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()
}

Maybe I have to downgrade to Gradle 6?也许我必须降级到 Gradle 6? I have been trying to get things to work with 7, however it seems that the documentation is piss poor from the Gradle people.我一直试图让事情与 7 一起工作,但是 Gradle 人的文档似乎很糟糕。 Searching is so difficult because 9 of 10 hits are about retrieving from Maven, not publishing, and all the rest are random fragments which most of the time only applies to Gradle 6. But without in depth experience on the prior workings of Gradle it is often impossible to tell. Searching is so difficult because 9 of 10 hits are about retrieving from Maven, not publishing, and all the rest are random fragments which most of the time only applies to Gradle 6. But without in depth experience on the prior workings of Gradle it is often不可能告诉。 A working example, if you have one, would be very helpful.一个可行的例子,如果你有的话,会很有帮助。

Unfortunately this is my first attempt to automate the publication to Maven, so I have no old working example to work from, and no experience with how Gradle does this in the past.不幸的是,这是我第一次尝试将发布自动化到 Maven,所以我没有旧的工作示例可供使用,也没有 Gradle 过去如何做到这一点的经验。 I have read all the documentation from Gradle, but so much of it simply does not describe what I can get to run that it is useless, and I find myself doubting that it is correct.我已经阅读了 Gradle 的所有文档,但其中大部分内容并没有描述我可以运行的内容,以至于它是无用的,我发现自己怀疑它是否正确。

The new documentation has not been deployed yet, as you can see on sonatype jira ticket: https://issues.sonatype.org/browse/OSSRH-70091新文档尚未部署,您可以在 sonatype jira 票证上看到: https://issues.sonatype.org/browse/OSSRH-70091

Here my configuration, which work with gradle 7.3:这是我的配置,适用于 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")
            }
        }
}

The final settings needed:需要的最终设置:

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
}

The publish settings are needed to make certain tasks appear.需要publish设置才能显示某些任务。 The mavenJava is not a method call, but instead the identifier of an object that is used in tasks and also in the signing. mavenJava不是方法调用,而是用于任务和签名的 object 的标识符。 The username and password are placed in a projects file that is not checked into repository.用户名和密码放置在未检入存储库的项目文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Gradle将WAR文件发布到Maven Repo - Publish WAR file to Maven Repo using Gradle 如何使用 Gradle Publish Plugin 将 proguard JAR 发布为 Maven Artifact - How to publish proguard JAR as Maven Artifact using Gradle Publish Plugin 使用SBT将工件发布到本地Maven存储库并在Gradle项目中使用 - Publish artifact to local maven repo with SBT and use it in Gradle project 使用ivy:publish任务发布到公共Maven存储库-需要凭据吗? - using ivy:publish task to publish to the public Maven repo - need credentials? 如何使用 gradle maven-publish 插件和 Kotlin DSL 发布带有空 pom 的 shadow jar? - How to publish a shadow jar with an empty pom using gradle maven-publish plugin and Kotlin DSL? 如何使用shadowJar gradle插件而不是Maven发布将胖JAR发布到JitPack - How to publish a fat JAR to JitPack using shadowJar gradle plugin and not maven publish 如何使用 Gradle 将源代码发布到本地 maven 存储库? - How to publish source into local maven repository with Gradle? 如何使用现有的pom.xml将第三方文件发布到远程maven仓库? - How to publish a 3rd party files to remote maven repo using an existing pom.xml? 只发布JAR到Maven仓库 - Publish only JAR to maven repo 如何使用 maven-publish 和 shadow 插件重命名使用 Gradle 发布的 jar 文件? - How to rename jar file that's being published using Gradle with maven-publish and shadow plugins?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM