Gradle plugin that creates a publish
task to automatically upload all of your Java, Kotlin or Androidlibraries to any Maven instance. This plugin is based on Chris Banes initial implementationand has been enhanced to add Kotlin support and keep up with the latest changes.
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
}
}
apply plugin: "com.vanniktech.maven.publish"
Snapshots can be found here.
To configure the coordinates of your published artifacts as well as the POM this pluginuses Gradle properties. It's generally recommended to set them in your gradle.properties
file.
GROUP=com.test.mylibrary
POM_ARTIFACT_ID=mylibrary-runtime
VERSION_NAME=3.0.5
POM_NAME=My Library
POM_DESCRIPTION=A description of what my library does.
POM_INCEPTION_YEAR=2020
POM_URL=https://github.com/username/mylibrary/
POM_LICENSE_NAME=The Apache Software License, Version 2.0
POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENSE_DIST=repo
POM_SCM_URL=https://github.com/username/mylibrary/
POM_SCM_CONNECTION=scm:git:git://github.com/username/mylibrary.git
POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/username/mylibrary.git
POM_DEVELOPER_ID=username
POM_DEVELOPER_NAME=User Name
POM_DEVELOPER_URL=https://github.com/username/
This Gradle plugin is using itself to publish any of the updates and sets the Gradle properties inthis gradle.properties.
In multi module projects you can set most properties in the root gradle.properties
file andthen only set the module specific ones in the submodules. For example if you have two modulescalled runtime
and driver
you could only set POM_ARTIFACT_ID
and POM_NAME
in<project-dir>/runtime/gradle.properties
and <project-dir>/driver/gradle.properties
while sharingthe rest by putting them into <project-dir>/gradle.properties
.
Without any further configuration the plugin has two tasks. publish
which will uploadto Maven Central (through Sonatype OSSRH) by default. To publish to the local maven repository on yourmachine (~/m2/repository
) there is publishToMavenLocal
.
In case you are using s01.oss.sonatype.org
you need to configure that like this:
allprojects {
plugins.withId("com.vanniktech.maven.publish") {
mavenPublish {
sonatypeHost = "S01"
}
}
}
The username and password for Sonatype OSS can be provided as Gradle properties called mavenCentralUsername
and mavenCentralPassword
to avoid having to commit them. You can also supply them as environment variablescalled ORG_GRADLE_PROJECT_mavenCentralUsername
and ORG_GRADLE_PROJECT_mavenCentralPassword
.
To remove the default repository set sonatypeHost
to null
.
You can add additional repositories to publish to using the standard Gradle APIs:
publishing {
repositories {
maven {
def releasesRepoUrl = "$buildDir/repos/releases"
def snapshotsRepoUrl = "$buildDir/repos/snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
}
More information can be found in Gradle's documentation
Note: To prevent looping behavior, especially in Kotlin projects / modules, you need to run the publish
task with --no-daemon
and --no-parallel
flags
The plugin supports signing all of your release artifacts with GPG. This is a requirement when publishing toMaven Central - our default behavior. Any version ending in -SNAPSHOT
will never be signed. Signing parameterscan be configured via:
signing.keyId=12345678
signing.password=some_password
signing.secretKeyRingFile=/Users/yourusername/.gnupg/secring.gpg
It's best to place them inside your home directory, $HOME/.gradle/gradle.properties
. You can find more informationabout these properties in Gradle's documentaion.
In case you want to use in memory signing keys, which works great for CI, you can specify them like this instead:
signingInMemoryKey=exported_ascii_armored_key
# Optional.
signingInMemoryKeyId=24875D73
# If key was created with a password.
signingInMemoryKeyPassword=secret
These properties can also be provided as environment variables by prefixing them with ORG_GRADLE_PROJECT_
It is possible to disable signing of release artifacts directly in your build scripts (takes precedence):
mavenPublish {
releaseSigningEnabled = false
}
Alternatively, you can use a Gradle property which is recommended if you only want to sign certain buildsor only build on certain machines.
RELEASE_SIGNING_ENABLED=false
By default, the "release" variant will be used for publishing. Optionally, a specific variant can be defined by the plugin extension:
mavenPublish {
androidVariantToPublish = "demoDebug" // or use project.property('PUBLISH_VARIANT'), etc.
}
Once publish
is called, and if you're using a Nexus repository, you'll have to make a release. This canbe done manually by following the release steps at sonatype.
Additionally, the plugin will create a closeAndReleaseRepository
task that you can call after publish
:
# prepare your release by assigning a version (remove the -SNAPSHOT suffix)
./gradlew publish --no-daemon --no-parallel
./gradlew closeAndReleaseRepository
It assumes there's only one staging repository active when closeAndReleaseRepository is called. If you have stale staging repositories, you'll have to delete them by logging at https://oss.sonatype.org (or you Nexus instance).
Starting with version 0.15.0 there is a base plugin. This new plugin has the same capabilities as the mainplugin but does not configure anything automatically. In the current stage the APIs are still marked with @Incubating
so they might change.
In your root build.gradle
file you can do the general configuration for all modules in your project.
import com.vanniktech.maven.publish.SonatypeHost
allprojects {
plugins.withId("com.vanniktech.maven.publish.base") {
group = "com.example.project"
version = "1.0.3-SNAPSHOT"
mavenPublishing {
publishToMavenCentral("DEFAULT")
// Will only apply to non snapshot builds.
// Uses credentials as described above, supports both regular and in memory signing.
signAllPublications()
pom {
name = "My Library"
description = "A description of what my library does."
inceptionYear = "2020"
url = "https ://github.com/username/mylibrary/"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "username"
name = "User Name"
url = "https://github.com/username/"
}
}
scm {
url = "https://github.com/username/mylibrary/"
connection = "scm:git:git://github.com/username/mylibrary.git"
developerConnection = "scm:git:ssh://git@github.com/username/mylibrary.git"
}
}
// Alternatively to the DSL based POM configuration above you can define them
// in Gradle properties
pomFromGradleProperties()
}
}
}
The above also works to configure the POM of the regular plugin without properties. It's also possible to use it ina project where some modules use the regular and some use the base plugin.
In the individual projects you can then configure publishing like this:
import com.vanniktech.maven.publish.JavaLibrary
import com.vanniktech.maven.publish.JavadocJar
apply plugin: "com.vanniktech.maven.publish.base"
mavenPublishing {
// available options:
// - JavaLibrary
// - GradlePlugin
// - AndroidLibrary
// - KotlinJvm
// - KotlinJs
// - KotlinMultiplatform
// the first parameter configures the javadoc jar, available options:
// - None
// - Empty
// - Javadoc
// - Dokka("dokkaHtml") - the parameter is the name of the Dokka task
// second one is for whether to publish sources, optional, defaults to true (not supported for KotlinMultiplatform(
// AndroidLibrary has a third parameter for which variant to publish, defaults to "release"
configure(new JavaLibrary(new JavadocJar.Javadoc(), true))
}
Copyright (C) 2018 Vanniktech - Niklas Baudy
Licensed under the Apache License, Version 2.0
maven-publish插件 maven-publish是一个Gradle插件,可以我们的编译的输出物(artifacts)发布到Apache Maven仓库当中,例如aar,jar等library发布到仓库当中,我们可以通过gradle或者maven进行远程依赖使用。 maven-publish的使用 引入插件 在需要用到的模块的build.gradle文件加入这句 apply plugin:
gradle–groovy-dsl和kotlin-dsl对比 官方文档:Migrating build logic from Groovy to Kotlin 官方demo:kotlin-dsl-samples/samples at master · gradle/kotlin-dsl-samples · GitHub android相关的迁移可以查阅:将构建配置从 Groovy 迁移到 KTS
异常信息 Execution failed for task ':gradle-demo:publishMavenJavaPublicationToMavenRepository'. > Failed to publish publication 'mavenJava' to repository 'maven' > Artifact gradle-demo-1.0.jar wasn't p
在参考郭霖大神的文章 再见JCenter,将你的开源库发布到MavenCentral上吧 发布项目到Maven时候出现一点问题,在此记录下: 发布步骤按照上文进行就可以了,在点击pulish发布时时候出现几点问题: 1.No compatible plugin found in project for publishing 提示发布时没有找到兼容的插件 出现这个问题时,解决方法可能有以下几种: 需
Gradle 插件上传 Maven 库配置详解 Gradle 插件上传 Maven 库,有两种方式,本文分别进行介绍。 Old Maven Plugin Gradle 1.0 中提供的原始的,过时的发布机制将工件部署到 Maven 存储库。 如果我们的项目仅生成默认的 jar 文件。现在,想将此 jar 文件部署到本地 Maven 存储库,怎么做呢? build.gradle 声明 使用 Mave
Gradle 8.0 发布 Gradle团队很兴奋地宣布了一个新的主要版本Gradle,8.0。这个主要版本有突破性的变化;请参考Gradle 7.x升级指南寻求指导。 此版本包括对的几项改进科特林DSL。这包括一个解释器来跳过声明性插件的Kotlin编译器,并升级到Kotlin API级。 内置版本的许多好处现在都可以从buildSrc,比如跑步buildSrc直接执行任务,跳过测试,并使用bu
一、添加插件 apply plugin: 'maven-publish' 二、添加如下配置 //打包源码 task sourceJar(type: Jar) { from sourceSets.main.allJava } publishing { publications { maven(MavenPublication) { //指定g
我有一个发布到maven的多平台kotlin库项目,并且一直在更新到kotlin 1.3多平台模型和kotlin-dsl 前面的groovy gradle脚本有一个modifyPom块,我在这里找到了一个示例。然而,只要我补充 无论pom数据中有什么,我都会得到相同的结果,即modifyPom groovy闭包的调用用一个非常模糊的错误中断了构建: 换句话说,修改Pom groovy闭包调用的行号
android-maven-publish Deprecated: This plugin is no longer needed since maven-publish is officially supported by AGP. Use AGP 3.6.0 or newer. Description Modification of the standard Maven Publish plu
我有gradle多模块项目配置与kotlin脚本。我想添加发布到maven存储库,我找到了maven发布插件。但它似乎跳过了为每个项目配置的版本: : : 输出: 文件可以。如何使gradle获得模块的正确版本?
我在groovy中编写了一个Gradle插件,并使用Gradle来构建它。我有一个本地网络艺术工厂服务器,我使用Gradle中的Gradle艺术工厂插件和maven-发布插件将结果发布到该服务器。我有另一个Gradle构建脚本,它依赖于此插件作为依赖项。如果我用特定的版本列出我的依赖项,我就能够让这一切正常工作。我尝试过使用maven版本范围(例如,“[1.0,2.0)”),但这失败了,因为它找不
我有一个项目,它有一个(Java)模块,其次是一个(Android库)模块,它依赖于模块。我以前一直在我的文件中使用插件,并且我一直在使用该插件的任务来发布我的两个模块中的工件。这已经工作并产生了pom文件,这些文件反映了我的文件中的依赖项。 我想我会用新的插件替换旧的插件。但是,我看到插件生成的pom文件不包含任何依赖项。这是设计使然,这是插件中的错误还是我错误地使用了插件? 我的模块中的文件如
我试图发布一个由Gradle 6.0创建的模糊JAR。1个ProGuard插件到Maven存储库。在过去的几天里,我学到了很多关于Gradle配置、变体和工件的知识,但我似乎无法做到这一点。我相信相关文件就是这里的文件。 我创建了一个简单的示例,展示了我在Github上的预期设置。 如果我运行在这个项目中,我得到了以下错误。 我还尝试了不推荐的插件。不幸的是,这个插件忽略了定制的ProGuard