MacOS V10.15.3 + Java V1.8 + Gradle v6.4 + Kotlin v1.3.41 + Android SDK
1. Build a Jar package and upload it to JCenter.
- TOC:
-
- The gradle command generates the project
-
- Add release plug-ins;
-
- Define Gradle Tasks that generate the related products.
-
- Configure related parameters.
-
- Perform upload
task bintrayUpload
- Perform upload
-
We use the gradle command from the command line to create a project for us.
mkdir 2020jcenterpublish cd 2020jcenterpublish mkdir javaLibraryJarKotlin cd javaLibraryJarKotlin gradle init #Select the library - >.. -> Kotlin project (Java also available) Copy the code
At this point in the javaLibraryJarKotlin folder, we have a project that Gradle generated for us. The directory tree is as follows:
. ├ ─ ─ build. Gradle ├ ─ ─ gradle │ └ ─ ─ wrapper │ ├ ─ ─ gradle - wrapper. Jar │ └ ─ ─ gradle - wrapper. The properties ├ ─ ─ gradlew ├ ─ ─ Gradlew. Bat ├ ─ ─ Settings. Gradle └ ─ ─ the SRC ├ ─ ─ the main │ ├ ─ ─ kotlin │ │ └ ─ ─ com │ │ └ ─ ─ halohoop │ │ └ ─ ─ librarykotlinpublish │ ├ ─ ├ ─ ├ ─ ├ ─test├── uninhibited-press, uninhibited-press, uninhibited-press, uninhibited-press, uninhibited-press, uninhibited-press, uninhibited-press 8 filesCopy the code
We’re going to iterate on this project, and now let’s assume that this is the project we’re going to code, and we’re going to package it and publish it to JCenter.
-
Add release plug-ins;
// rootProject/build.gradle plugins { // omit some code... id 'com.jfrog.bintray' version '1.8.5' id 'maven-publish' } Copy the code
-
Define Gradle Tasks that generate the related products.
// rootProject/build.gradle // Generate the source jar package task task sourcesJar(type: Jar) { from sourceSets.main.allJava archiveClassifier = 'sources' } Generate javadoc JAR package task task javadocJar(type: Jar) { from javadoc archiveClassifier = 'javadoc' } Copy the code
-
Configure related parameters.
// rootProject/build.gradle bintray { user = 'TODO set your bintray.com username ' key = "${project.hasProperty('apiKey') ? project.apiKey : ""}" override = true//TODO overwrites if there is the same version publish = true//TODO whether to publish directly after uploading pkg { repo = 'maven'//TODO set up the published Maven library. Go to "bintray.com/ your user name page" and you can see all the anticipating Maven library names name = project.name//TODO project name userOrg = user// The TODO user's organization licenses = ['the Apache 2.0']//TODO sets open source libraries under the open source protocol vcsUrl = "github.com/halohoop/2020jcenterpublish"//TODO sets the open source library address issueTrackerUrl = "github.com/halohoop/2020jcenterpublish/issue"//TODO sets open source library issue address version { name = '0.0.1'//TODO sets the version number desc = "javaLibraryJarKotlin 0.0.1 final" released = new Date()//TODO release time vcsTag = '0.0.1'//TODO sets version tag / / attributes = [' gradle - plugin ':'. Com. Use less: com. Use. Less. Gradle: gradle - useless - plugin '] / / negligible properties, there is demand can view the official documents related configuration}}TODO adds the name defined in the Publishing DSL publications = ['MyPublication'] } publishing { publications { MyPublication(MavenPublication) { // Standard open source library, the following three files are standard artifact(jar)//TODO configuration library JAR package artifact(sourcesJar)//TODO configuration library source JAR package artifact(javadocJar)//TODO configuration library Javadoc JAR package groupId "com.halohoop"/ / package name artifactId 'javalibraryjarkotlin'/ / library id version '0.0.1'//TODO sets the version number}}}Copy the code
-
BintrayUpload Task bintrayUpload This task is provided by com.jfrog.bintray and can be executed successfully as long as the relevant DSL is configured. In order not to expose the password information of our Bintray.com account in the source code of the project, the key attribute in bintray DSL above is dynamically acquired by the apiKey attribute for value. Therefore, we need to use -p to pass an apiKey attribute parameter when executing the task bintrayUpload. Format: -pkey =value Example:./gradlew bintrayUpload -papikey = your bintray.com password or apikey This apikey we at bintray.com/profile/edi… Can obtain, as shown in the following figure:
2. Build Aar package and upload it to JCenter;
- TOC:
-
- Generate an Android project using the IDE Android Studio
-
- Add release plug-ins;
-
- Define Gradle Tasks that generate the related products.
-
- Configure related parameters.
-
- Perform upload
task bintrayUpload
- Perform upload
- Since it is no longer officially supported to create an Android project using the “Android” command line, we will create a Library project using Android Studio. The App Module is named “AndroidLibrary”.
- See: developer.android.com/studio/tool…
├── AndroidLibrary │ ├─ Build.Gradle │ ├─ consumer-Rules. Pro │ ├─ ProGuard-rules SRC │ ├─ ├─ Java │ ├─ com │ ├─ Java │ ├─ ExampleInstrumentedTest. Java │ ├ ─ ─ the main │ │ ├ ─ ─ AndroidManifest. XML │ │ ├ ─ ─ Java │ │ │ └ ─ ─ com │ │ │ └ ─ ─ halohoop │ │ │ └ ─ ─ androidlibraryaarkotlin2 │ │ │ └ ─ ─ if kt │ │ └ ─ ─ res │ │ └ ─ ─ drawable │ └ ─ ─test│ ├── Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Gradle │ └ ─ ─ wrapper │ ├ ─ ─ gradle - wrapper. Jar │ └ ─ ─ gradle - wrapper. The properties ├ ─ ─ gradle. Properties ├ ─ ─ gradlew ├ ─ ─ ├─ local. Properties ├─ SettingsCopy the code
- Change App Module to Library Module
- Open the file
./androidlibrary/build.gradle
- Put the plug-in in it
apply plugin: 'com.android.application'
Modified toapply plugin: 'com.android.library'
- Delete applicationId:
android { defaultConfig { applicationId "xxx.xxxxx.xx" }}
- Open the file
- Add release plug-ins;
- Open the file
./build.gradle
Buildscript {// omit some code dependencies {// omit some code classpath'com. Jfrog. Bintray. Gradle: gradle bintray - plugin: 1.8.5' } } allprojects { apply plugin: 'com.jfrog.bintray' apply plugin: "maven-publish"// You can also apply the plugin to the build.gradle file of the corresponding module // omit some code}Copy the code
- Open the file
- Define Gradle Tasks that generate the related products.
// Generate the source jar package task task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } task androidJavadocs(type: Javadoc) { title = "${getStringProperty("mavProjectName")} ${project.version} API" description "Generates Javadoc" source = android.sourceSets.main.java.srcDirs classpath += files(android.bootClasspath) android.libraryVariants.all { variant -> if (variant.name == 'release') { owner.classpath += variant.javaCompileProvider.get().classpath } } exclude '**/R.html'.'**/R.*.html'.'**/index.html'.'**/*.kt' options { windowTitle("${getStringProperty("mavProjectName")} ${project.version} Reference") locale = 'en_US' encoding = 'UTF-8' charSet = 'UTF-8' links("http://docs.oracle.com/javase/7/docs/api/") linksOffline("http://d.android.com/reference"."${android.sdkDirectory}/docs/reference") setMemberLevel(JavadocMemberLevel.PUBLIC) } } Generate javadoc JAR package task task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { classifier = 'javadoc' from androidJavadocs.destinationDir } Copy the code
- Configure related parameters.
// Only the differences are listed here. For other parts, please refer to 1.4 // Omit some code publishing { publications { MyPublication(MavenPublication) { // Standard open source library, the following three files are standard artifact(bundleReleaseAar)//TODO library AAR package artifact(androidSourcesJar)//TODO configuration library source JAR package artifact(androidJavadocsJar)//TODO configuration library Javadoc JAR package // Omit some code}}}Copy the code
- Perform upload
task bintrayUpload
(see 1.5)
3. Skip 1 and 2, and use the packaged upload and publish plug-in directly, saving tedious configuration;
- Introduction of bintray plugin;
- Java/Kotlin engineering;
// rootProject/build.gradle plugins { // omit some code... id 'com.jfrog.bintray' version '1.8.5' } Copy the code
- The Android engineering;
// rootProject/build.gradle buildscript { // Omit some code dependencies { // Omit some code classpath 'com. Jfrog. Bintray. Gradle: gradle bintray - plugin: 1.8.5' } } allprojects { apply plugin: 'com.jfrog.bintray' // You can also apply the plugin to the build.gradle file of the corresponding Module // Omit some code } Copy the code
- Java/Kotlin engineering;
- Introducing a packaged upload and publish plug-in;
// Java project: rootProject/build.gradle / / the android project: rootProject/module/build gradle apply from: "https://cdn.jsdelivr.net/gh/halohoop/cdn@latest/utils/gradle/bintrayIntegrated.gradle" Copy the code
- Define attribute parameters The meaning of each attribute can be reflected by naming;
// Java project: rootProject/build.gradle / / the android project: rootProject/module/build gradle project.ext { bintrayUser = "halohoop" groupId = "com.halohoop" artifactId = project.name version = "0.0.3" javadocDir = "src/main/java" sourceDir = "src" bintrayOverride = true bintrayPublish = true bintrayPkgRepo = "maven" bintrayPkgName = project.name bintrayPkgUserOrg = project.ext.bintrayUser bintrayPkgLicenses = ['the Apache 2.0'] bintrayPkgVcsUrl = "github.com/${project.ext.bintrayUser}/${project.ext.bintrayPkgName}.git" bintrayPkgIssueTrackerUrl = "github.com/${project.ext.bintrayUser}/${project.ext.bintrayPkgName}/issue" bintrayPkgVersionDesc = "${project.ext.bintrayPkgName} ${project.ext.version} final" bintrayPkgVersionReleased = new Date() } Copy the code
Personal level is limited, readers are welcome to erratum.
- Wechat: halohoop
- E-mail: [email protected]
References
- Creating New Gradle Builds
- Github.com/sky-uk/grad…
- Developer.android.com/studio/buil…
The original link: http://halohoop.com/2020/03/05/meals-2020publish2jcenter/