gradle-maven-publish-plugin

我要开发同款
匿名用户2021年11月26日
61阅读
开发技术Kotlin
所属分类手机/移动开发
授权协议Apache-2.0 License

作品详情

gradle-maven-publish-plugin

GradlepluginthatcreatesapublishtasktoautomaticallyuploadallofyourJava,KotlinorAndroidlibrariestoanyMaveninstance.ThispluginisbasedonChrisBanesinitialimplementationandhasbeenenhancedtoaddKotlinsupportandkeepupwiththelatestchanges.

Setupbuild.gradlebuildscript{repositories{mavenCentral()}dependencies{classpath'com.vanniktech:gradle-maven-publish-plugin:0.18.0'}}applyplugin:"com.vanniktech.maven.publish"

Snapshotscanbefoundhere.

Settingproperties

ToconfigurethecoordinatesofyourpublishedartifactsaswellasthePOMthispluginusesGradleproperties.It'sgenerallyrecommendedtosettheminyourgradle.propertiesfile.

GROUP=com.test.mylibraryPOM_ARTIFACT_ID=mylibrary-runtimeVERSION_NAME=3.0.5POM_NAME=MyLibraryPOM_DESCRIPTION=Adescriptionofwhatmylibrarydoes.POM_INCEPTION_YEAR=2020POM_URL=https://github.com/username/mylibrary/POM_LICENSE_NAME=TheApacheSoftwareLicense,Version2.0POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txtPOM_LICENSE_DIST=repoPOM_SCM_URL=https://github.com/username/mylibrary/POM_SCM_CONNECTION=scm:git:git://github.com/username/mylibrary.gitPOM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/username/mylibrary.gitPOM_DEVELOPER_ID=usernamePOM_DEVELOPER_NAME=UserNamePOM_DEVELOPER_URL=https://github.com/username/

ThisGradlepluginisusingitselftopublishanyoftheupdatesandsetstheGradlepropertiesinthisgradle.properties.

Inmultimoduleprojectsyoucansetmostpropertiesintherootgradle.propertiesfileandthenonlysetthemodulespecificonesinthesubmodules.ForexampleifyouhavetwomodulescalledruntimeanddriveryoucouldonlysetPOM_ARTIFACT_IDandPOM_NAMEin<project-dir>/runtime/gradle.propertiesand<project-dir>/driver/gradle.propertieswhilesharingtherestbyputtingtheminto<project-dir>/gradle.properties.

Wheretouploadto

Withoutanyfurtherconfigurationthepluginhastwotasks.publishwhichwilluploadtoMavenCentral(throughSonatypeOSSRH)bydefault.Topublishtothelocalmavenrepositoryonyourmachine(~/m2/repository)thereispublishToMavenLocal.

Incaseyouareusings01.oss.sonatype.orgyouneedtoconfigurethatlikethis:

allprojects{plugins.withId("com.vanniktech.maven.publish"){mavenPublish{sonatypeHost="S01"}}}

TheusernameandpasswordforSonatypeOSScanbeprovidedasGradlepropertiescalledmavenCentralUsernameandmavenCentralPasswordtoavoidhavingtocommitthem.YoucanalsosupplythemasenvironmentvariablescalledORG_GRADLE_PROJECT_mavenCentralUsernameandORG_GRADLE_PROJECT_mavenCentralPassword.

ToremovethedefaultrepositorysetsonatypeHosttonull.

YoucanaddadditionalrepositoriestopublishtousingthestandardGradleAPIs:

publishing{repositories{maven{defreleasesRepoUrl="$buildDir/repos/releases"defsnapshotsRepoUrl="$buildDir/repos/snapshots"url=version.endsWith('SNAPSHOT')?snapshotsRepoUrl:releasesRepoUrl}}}

MoreinformationcanbefoundinGradle'sdocumentation

Note:Topreventloopingbehavior,especiallyinKotlinprojects/modules,youneedtorunthepublishtaskwith--no-daemonand--no-parallelflags

Signing

ThepluginsupportssigningallofyourreleaseartifactswithGPG.ThisisarequirementwhenpublishingtoMavenCentral-ourdefaultbehavior.Anyversionendingin-SNAPSHOTwillneverbesigned.Signingparameterscanbeconfiguredvia:

signing.keyId=12345678signing.password=some_passwordsigning.secretKeyRingFile=/Users/yourusername/.gnupg/secring.gpg

It'sbesttoplacetheminsideyourhomedirectory,$HOME/.gradle/gradle.properties.YoucanfindmoreinformationaboutthesepropertiesinGradle'sdocumentaion.

Incaseyouwanttouseinmemorysigningkeys,whichworksgreatforCI,youcanspecifythemlikethisinstead:

signingInMemoryKey=exported_ascii_armored_key#Optional.signingInMemoryKeyId=24875D73#Ifkeywascreatedwithapassword.signingInMemoryKeyPassword=secret

ThesepropertiescanalsobeprovidedasenvironmentvariablesbyprefixingthemwithORG_GRADLE_PROJECT_

Itispossibletodisablesigningofreleaseartifactsdirectlyinyourbuildscripts(takesprecedence):

mavenPublish{releaseSigningEnabled=false}

Alternatively,youcanuseaGradlepropertywhichisrecommendedifyouonlywanttosigncertainbuildsoronlybuildoncertainmachines.

RELEASE_SIGNING_ENABLED=falseAndroidVariants

Bydefault,the"release"variantwillbeusedforpublishing.Optionally,aspecificvariantcanbedefinedbythepluginextension:

mavenPublish{androidVariantToPublish="demoDebug"//oruseproject.property('PUBLISH_VARIANT'),etc.}Releasing

Oncepublishiscalled,andifyou'reusingaNexusrepository,you'llhavetomakearelease.Thiscanbedonemanuallybyfollowingthereleasestepsatsonatype.

Additionally,thepluginwillcreateacloseAndReleaseRepositorytaskthatyoucancallafterpublish:

#prepareyourreleasebyassigningaversion(removethe-SNAPSHOTsuffix)./gradlewpublish--no-daemon--no-parallel./gradlewcloseAndReleaseRepository

Itassumesthere'sonlyonestagingrepositoryactivewhencloseAndReleaseRepositoryiscalled.Ifyouhavestalestagingrepositories,you'llhavetodeletethembyloggingathttps://oss.sonatype.org(oryouNexusinstance).

Baseplugin

Startingwithversion0.15.0thereisabaseplugin.Thisnewpluginhasthesamecapabilitiesasthemainpluginbutdoesnotconfigureanythingautomatically.InthecurrentstagetheAPIsarestillmarkedwith@Incubatingsotheymightchange.

Inyourrootbuild.gradlefileyoucandothegeneralconfigurationforallmodulesinyourproject.

importcom.vanniktech.maven.publish.SonatypeHostallprojects{plugins.withId("com.vanniktech.maven.publish.base"){group="com.example.project"version="1.0.3-SNAPSHOT"mavenPublishing{publishToMavenCentral("DEFAULT")//Willonlyapplytononsnapshotbuilds.//Usescredentialsasdescribedabove,supportsbothregularandinmemorysigning.signAllPublications()pom{name="MyLibrary"description="Adescriptionofwhatmylibrarydoes."inceptionYear="2020"url="https://github.com/username/mylibrary/"licenses{license{name="TheApacheLicense,Version2.0"url="https://www.apache.org/licenses/LICENSE-2.0.txt"distribution="https://www.apache.org/licenses/LICENSE-2.0.txt"}}developers{developer{id="username"name="UserName"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"}}//AlternativelytotheDSLbasedPOMconfigurationaboveyoucandefinethem//inGradlepropertiespomFromGradleProperties()}}}

TheabovealsoworkstoconfigurethePOMoftheregularpluginwithoutproperties.It'salsopossibletouseitinaprojectwheresomemodulesusetheregularandsomeusethebaseplugin.

Intheindividualprojectsyoucanthenconfigurepublishinglikethis:

importcom.vanniktech.maven.publish.JavaLibraryimportcom.vanniktech.maven.publish.JavadocJarapplyplugin:"com.vanniktech.maven.publish.base"mavenPublishing{//availableoptions://-JavaLibrary//-GradlePlugin//-AndroidLibrary//-KotlinJvm//-KotlinJs//-KotlinMultiplatform//thefirstparameterconfiguresthejavadocjar,availableoptions://-None//-Empty//-Javadoc//-Dokka("dokkaHtml")-theparameteristhenameoftheDokkatask//secondoneisforwhethertopublishsources,optional,defaultstotrue(notsupportedforKotlinMultiplatform(//AndroidLibraryhasathirdparameterforwhichvarianttopublish,defaultsto"release"configure(newJavaLibrary(newJavadocJar.Javadoc(),true))}License

Copyright(C)2018Vanniktech-NiklasBaudy

LicensedundertheApacheLicense,Version2.0

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论