GradlepluginthatcreatesapublishtasktoautomaticallyuploadallofyourJava,KotlinorAndroidlibrariestoanyMaveninstance.ThispluginisbasedonChrisBanesinitialimplementationandhasbeenenhancedtoaddKotlinsupportandkeepupwiththelatestchanges.
Setupbuild.gradlebuildscript{repositories{mavenCentral()}dependencies{classpath'com.vanniktech:gradle-maven-publish-plugin:0.18.0'}}applyplugin:"com.vanniktech.maven.publish"Snapshotscanbefoundhere.
SettingpropertiesToconfigurethecoordinatesofyourpublishedartifactsaswellasthePOMthispluginusesGradleproperties.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.
WheretouploadtoWithoutanyfurtherconfigurationthepluginhastwotasks.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
SigningThepluginsupportssigningallofyourreleaseartifactswithGPG.ThisisarequirementwhenpublishingtoMavenCentral-ourdefaultbehavior.Anyversionendingin-SNAPSHOTwillneverbesigned.Signingparameterscanbeconfiguredvia:
signing.keyId=12345678signing.password=some_passwordsigning.secretKeyRingFile=/Users/yourusername/.gnupg/secring.gpgIt'sbesttoplacetheminsideyourhomedirectory,$HOME/.gradle/gradle.properties.YoucanfindmoreinformationaboutthesepropertiesinGradle'sdocumentaion.
Incaseyouwanttouseinmemorysigningkeys,whichworksgreatforCI,youcanspecifythemlikethisinstead:
signingInMemoryKey=exported_ascii_armored_key#Optional.signingInMemoryKeyId=24875D73#Ifkeywascreatedwithapassword.signingInMemoryKeyPassword=secretThesepropertiescanalsobeprovidedasenvironmentvariablesbyprefixingthemwithORG_GRADLE_PROJECT_
Itispossibletodisablesigningofreleaseartifactsdirectlyinyourbuildscripts(takesprecedence):
mavenPublish{releaseSigningEnabled=false}Alternatively,youcanuseaGradlepropertywhichisrecommendedifyouonlywanttosigncertainbuildsoronlybuildoncertainmachines.
RELEASE_SIGNING_ENABLED=falseAndroidVariantsBydefault,the"release"variantwillbeusedforpublishing.Optionally,aspecificvariantcanbedefinedbythepluginextension:
mavenPublish{androidVariantToPublish="demoDebug"//oruseproject.property('PUBLISH_VARIANT'),etc.}ReleasingOncepublishiscalled,andifyou'reusingaNexusrepository,you'llhavetomakearelease.Thiscanbedonemanuallybyfollowingthereleasestepsatsonatype.
Additionally,thepluginwillcreateacloseAndReleaseRepositorytaskthatyoucancallafterpublish:
#prepareyourreleasebyassigningaversion(removethe-SNAPSHOTsuffix)./gradlewpublish--no-daemon--no-parallel./gradlewcloseAndReleaseRepositoryItassumesthere'sonlyonestagingrepositoryactivewhencloseAndReleaseRepositoryiscalled.Ifyouhavestalestagingrepositories,you'llhavetodeletethembyloggingathttps://oss.sonatype.org(oryouNexusinstance).
BasepluginStartingwithversion0.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))}LicenseCopyright(C)2018Vanniktech-NiklasBaudy
LicensedundertheApacheLicense,Version2.0
评论