├── .gitignore ├── LICENSE ├── README.md ├── assets └── gradle-publish-plugin.png ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── kotlin └── com └── whl ├── ComponentLibrary.kt ├── GradlePublishExtension.kt ├── GradlePublishPlugin.kt └── component ├── AndroidComponentLibrary.kt ├── JavaComponentLibrary.kt └── KotlinComponentLibrary.kt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | infer-out 3 | .gradle 4 | /local.properties 5 | /.idea/ 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | .idea 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 汪海游龙 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## PLEASE NOTE, THIS PROJECT IS NO LONGER BEING MAINTAINED 2 | 3 | ## Recommended use https://github.com/vanniktech/gradle-maven-publish-plugin 4 | 5 | 6 | ### Gradle Publish Plugin 7 | 8 | A flex gradle plugin make publish your library to maven repository become easy. 9 | 10 | ![gradle-publish-plugin.png](assets/gradle-publish-plugin.png) 11 | 12 | ### Feature 13 | - support publish multi-library, such as Java、Android、Kotlin 14 | - support for api / implementation dependencies in new Gradle 15 | - supports also @aar and transitive: false. 16 | - generate Kotlin doc with [dokka](https://github.com/Kotlin/dokka) 17 | - support upload sources Jar (configurable, default true) 18 | - sign a library including sources, Javadoc, and a customized POM (configurable, default false, and require Gradle Version >= 4.8) 19 | 20 | ### Usage 21 | 22 | for Gradle version >= 2.1: 23 | 24 | plugins { 25 | id "com.whl.gradle-publish-plugin" version "0.1.16-SNAPSHOT" 26 | } 27 | 28 | 29 | for Gradle version < 2.1 or where dynamic configuration is required: 30 | 31 | buildscript { 32 | repositories { 33 | maven { 34 | url "https://plugins.gradle.org/m2/" 35 | } 36 | } 37 | dependencies { 38 | classpath "com.whl:gradle-publish-plugin:0.1.16-SNAPSHOT" 39 | } 40 | } 41 | 42 | apply plugin: "com.whl.gradle-publish-plugin" 43 | 44 | Also see it in [Gradle plugins](https://plugins.gradle.org/plugin/com.whl.gradle-publish-plugin) 45 | 46 | then, configuration in your build.gradle,such as: 47 | 48 | simple example: 49 | 50 | group 'com.example' 51 | version '1.0-SNAPSHOT' 52 | 53 | gradlePublish { 54 | 55 | releaseRepository { 56 | url = "http://your repository.com/nexus/content/repositories/releases" 57 | userName = "your release account" 58 | password = "your release account" 59 | } 60 | 61 | } 62 | 63 | complete example: 64 | 65 | group 'com.example' 66 | version '1.0-SNAPSHOT' 67 | 68 | gradlePublish { 69 | 70 | sourceJarEnabled = true 71 | javaDocEnabled = true 72 | signEnabled = false 73 | 74 | releaseRepository { 75 | url = "http://your repository.com/nexus/content/repositories/releases" 76 | userName = "your release account" 77 | password = "your release account" 78 | } 79 | 80 | snapshotRepository { 81 | url = "http://your repository.com/nexus/content/repositories/snapshots" 82 | userName = "your snapshot account" 83 | password = "your snapshot account" 84 | } 85 | 86 | } 87 | 88 | 89 | last, execute `./gradlew publish` or `./gradlew :library:publish` task to publish your library to specified maven repository 90 | -------------------------------------------------------------------------------- /assets/gradle-publish-plugin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HarlonWang/gradle-publish-plugin/2a8a946bc5525ad987d9f8d2272bdcaf0b1a295f/assets/gradle-publish-plugin.png -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.jetbrains.kotlin.jvm' version '1.3.11' 3 | id 'maven-publish' 4 | id 'signing' 5 | id 'java-gradle-plugin' 6 | id "com.gradle.plugin-publish" version "0.10.1" 7 | } 8 | 9 | group 'com.whl' 10 | version '0.1.16-SNAPSHOT' 11 | 12 | repositories { 13 | mavenCentral() 14 | jcenter() 15 | google() 16 | } 17 | 18 | gradlePlugin { 19 | plugins { 20 | gradlePublishPlugin { 21 | id = 'com.whl.gradle-publish-plugin' 22 | implementationClass = 'com.whl.GradlePublishPlugin' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | api "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 29 | api gradleApi() 30 | api localGroovy() 31 | api "org.jetbrains.dokka:dokka-gradle-plugin:0.9.17" 32 | compileOnly 'com.android.tools.build:gradle:3.2.1' 33 | } 34 | 35 | compileKotlin { 36 | kotlinOptions.jvmTarget = "1.8" 37 | } 38 | compileTestKotlin { 39 | kotlinOptions.jvmTarget = "1.8" 40 | } 41 | 42 | pluginBundle { 43 | website = 'https://github.com/81813780/gradle-publish-plugin' 44 | vcsUrl = 'https://github.com/81813780/gradle-publish-plugin' 45 | description = 'Gradle plugin that supported publish all of your Java, Kotlin or Android libraries to any Maven instance.' 46 | tags = ['publish', 'maven', "gradle", "plugin", "library"] 47 | 48 | plugins { 49 | gradlePublishPlugin { 50 | displayName = 'Gradle maven publish plugin' 51 | } 52 | } 53 | 54 | mavenCoordinates { 55 | groupId = "com.whl" 56 | artifactId = "gradle-publish-plugin" 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HarlonWang/gradle-publish-plugin/2a8a946bc5525ad987d9f8d2272bdcaf0b1a295f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Feb 01 22:29:53 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'gradle-publish-plugin' 2 | 3 | -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/ComponentLibrary.kt: -------------------------------------------------------------------------------- 1 | package com.whl 2 | 3 | import org.gradle.api.publish.maven.MavenPublication 4 | 5 | /** 6 | * Only support publish kotlin or android or java library or mix library with kotlin project, otherwise see 7 | */ 8 | abstract class ComponentLibrary { 9 | 10 | fun buildComponentLibrary(mavenPublication: MavenPublication, extension: GradlePublishExtension) { 11 | fromComponent(mavenPublication) 12 | if (extension.sourceJarEnabled) { 13 | mavenPublication.artifact(sourcesJar()) 14 | } 15 | if (extension.javaDocEnabled) { 16 | mavenPublication.artifact(docJar()) 17 | } 18 | withPom(mavenPublication) 19 | } 20 | 21 | abstract fun fromComponent(mavenPublication: MavenPublication) 22 | 23 | abstract fun docJar() : Any 24 | 25 | abstract fun sourcesJar() : Any 26 | 27 | open fun withPom(mavenPublication: MavenPublication) {} 28 | 29 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/GradlePublishExtension.kt: -------------------------------------------------------------------------------- 1 | package com.whl 2 | 3 | import org.gradle.api.Action 4 | import org.gradle.api.Project 5 | 6 | open class GradlePublishExtension(project: Project) { 7 | 8 | /** 9 | * The release repository url this should be published to. 10 | */ 11 | private val defaultReleaseUrl: String = "${project.buildDir}/repository/releases" 12 | /** 13 | * The snapshot repository url this should be published to. 14 | */ 15 | private val defaultSnapshotUrl: String = "${project.buildDir}/repository/snapshots" 16 | 17 | /** 18 | * The source code should be published default, otherwise not 19 | */ 20 | var sourceJarEnabled = true 21 | 22 | /** 23 | * The Signing Plugin is used to generate a signature file for each artifact. 24 | * Since Gradle Version 4.8 25 | */ 26 | var signEnabled = false 27 | 28 | /** 29 | * The java doc should be published default, otherwise not 30 | */ 31 | var javaDocEnabled = false 32 | 33 | /** 34 | * The release repository this should be set to. 35 | */ 36 | var releaseRepository = MavenRepository(url = defaultReleaseUrl) 37 | /** 38 | * The snapshot repository this should be set to. 39 | */ 40 | var snapshotRepository = MavenRepository(url = defaultSnapshotUrl) 41 | 42 | fun releaseRepository(action: Action) { 43 | action.execute(releaseRepository) 44 | } 45 | 46 | fun snapshotRepository(action: Action) { 47 | action.execute(snapshotRepository) 48 | } 49 | 50 | data class MavenRepository( 51 | /** 52 | * The repository url this should be published to. 53 | */ 54 | var url: String, 55 | /** 56 | * The userName that should be used for publishing. 57 | */ 58 | var userName: String = "", 59 | /** 60 | * The password that should be used for publishing. 61 | */ 62 | var password: String = "" 63 | ) 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/GradlePublishPlugin.kt: -------------------------------------------------------------------------------- 1 | package com.whl 2 | 3 | import com.whl.component.AndroidComponentLibrary 4 | import com.whl.component.JavaComponentLibrary 5 | import com.whl.component.KotlinComponentLibrary 6 | import org.gradle.api.Plugin 7 | import org.gradle.api.Project 8 | import org.gradle.api.publish.PublishingExtension 9 | import org.gradle.api.publish.maven.MavenPublication 10 | import org.gradle.plugins.signing.SigningExtension 11 | import org.gradle.util.GradleVersion 12 | import java.lang.RuntimeException 13 | 14 | open class GradlePublishPlugin : Plugin{ 15 | 16 | override fun apply(project: Project) { 17 | val extension: GradlePublishExtension = createExtension(project) 18 | project.afterEvaluate { 19 | configurePublishing(project, extension) 20 | if (GradleVersion.current() >= GradleVersion.version("4.8") && extension.signEnabled) { 21 | configureSigning(project) 22 | } 23 | } 24 | } 25 | 26 | private fun createExtension(project: Project) = project.extensions.create("gradlePublish", GradlePublishExtension::class.java, project) 27 | 28 | private fun configurePublishing(project: Project, extension: GradlePublishExtension) { 29 | project.plugins.apply("maven-publish") 30 | 31 | project.plugins.withId("maven-publish") { 32 | val version = project.version as String 33 | 34 | project.extensions.configure(PublishingExtension::class.java) { publishing -> 35 | publishing.publications { publication -> 36 | publication.create("maven", MavenPublication::class.java) { maven -> 37 | maven.version = version 38 | project.componentLibrary().buildComponentLibrary(maven, extension) 39 | } 40 | } 41 | publishing.repositories {repository -> 42 | val (url, userName, password) = if (version.endsWith("-SNAPSHOT")) extension.snapshotRepository else extension.releaseRepository 43 | repository.maven {mavenRepository -> 44 | mavenRepository.setUrl(url) 45 | mavenRepository.credentials { credential -> 46 | credential.username = userName 47 | credential.password = password 48 | } 49 | } 50 | } 51 | } 52 | } 53 | } 54 | 55 | private fun configureSigning(project: Project) { 56 | project.plugins.apply("signing") 57 | project.plugins.withId("signing") { 58 | project.extensions.configure(SigningExtension::class.java) {signing -> 59 | val publishing = project.extensions.getByName("publishing") as PublishingExtension 60 | signing.sign(publishing.publications.getByName("maven")) 61 | } 62 | } 63 | } 64 | 65 | } 66 | 67 | fun Project.componentLibrary() : ComponentLibrary = when { 68 | plugins.hasPlugin("java-library") && !plugins.hasPlugin("org.jetbrains.kotlin.jvm") -> JavaComponentLibrary(project) 69 | plugins.hasPlugin("com.android.library") -> AndroidComponentLibrary(project) 70 | plugins.hasPlugin("org.jetbrains.kotlin.jvm") -> KotlinComponentLibrary(project) 71 | else -> throw RuntimeException("This project was unsupported, please make sure one of apply java-library or com.android.library or org.jetbrains.kotlin.jvm !") 72 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/component/AndroidComponentLibrary.kt: -------------------------------------------------------------------------------- 1 | package com.whl.component 2 | 3 | import com.android.build.gradle.LibraryExtension 4 | import com.whl.ComponentLibrary 5 | import org.gradle.api.Project 6 | import org.gradle.api.Task 7 | import org.gradle.api.artifacts.Dependency 8 | import org.gradle.api.artifacts.ModuleDependency 9 | import org.gradle.api.publish.maven.MavenPublication 10 | import org.gradle.api.tasks.bundling.Jar 11 | import org.gradle.api.tasks.javadoc.Javadoc 12 | import java.io.File 13 | 14 | class AndroidComponentLibrary(private val project: Project) : ComponentLibrary() { 15 | 16 | private val android: LibraryExtension = project.extensions.getByName("android") as LibraryExtension 17 | 18 | override fun fromComponent(mavenPublication: MavenPublication) { 19 | fromAndroidComponent(mavenPublication) 20 | } 21 | 22 | private fun fromAndroidComponent(mavenPublication: MavenPublication) { 23 | var bundleReleaseAar: Task? = null 24 | if (project.tasks.findByName("bundleReleaseAar") != null) { 25 | bundleReleaseAar = project.tasks.getByName("bundleReleaseAar") 26 | } 27 | //we only use bundleRelease in lower android gradle plugin version such as 2.3.3 28 | //more information look this https://stackoverflow.com/questions/51433769/why-android-gradle-maven-publish-artifact-bundlerelease-not-found/51869825#51869825 29 | if (project.tasks.findByName("bundleRelease") != null) { 30 | bundleReleaseAar = project.tasks.getByName("bundleRelease") 31 | } 32 | mavenPublication.artifact(bundleReleaseAar) 33 | } 34 | 35 | /** 36 | * Reference on https://github.com/JakeWharton/dagger-reflect/blob/master/gradle/gradle-mvn-push.gradle 37 | */ 38 | override fun docJar(): Any { 39 | val androidJavaDocs = project.tasks.create("androidJavadocs", Javadoc::class.java) 40 | androidJavaDocs.setSource(android.sourceSets.getByName("main").java.srcDirs) 41 | androidJavaDocs.classpath += project.files("${android.bootClasspath}${File.pathSeparator}") 42 | 43 | val androidJavaDocsJar = project.tasks.create("androidJavaDocsJar", Jar::class.java) 44 | androidJavaDocsJar.classifier = "javadoc" 45 | androidJavaDocsJar.from(androidJavaDocs.destinationDir) 46 | androidJavaDocsJar.dependsOn(androidJavaDocs) 47 | return androidJavaDocsJar 48 | } 49 | 50 | override fun sourcesJar(): Any { 51 | val androidSourcesJar = project.tasks.create("androidSourcesJar", Jar::class.java) 52 | androidSourcesJar.from(android.sourceSets.getByName("main").java.srcDirs) 53 | androidSourcesJar.classifier = "sources" 54 | return androidSourcesJar 55 | } 56 | 57 | override fun withPom(mavenPublication: MavenPublication) { 58 | mavenPublication.pom.withXml { xmlProvider -> 59 | val dependenciesNode = xmlProvider.asNode().appendNode("dependencies") 60 | fun addDependency(dep: Dependency, scope: String) { 61 | if (dep.group == null || dep.version == null || dep.name == "unspecified") { 62 | return // ignore invalid dependencies 63 | } 64 | //currently we only handle the dependency implements ModuleDependency interface 65 | // for support more feature, such as excludeRules 66 | if (dep is ModuleDependency) { 67 | val dependencyNode = dependenciesNode.appendNode("dependency") 68 | dependencyNode.apply { 69 | appendNode("groupId", dep.group) 70 | appendNode("artifactId", dep.name) 71 | appendNode("version", dep.version) 72 | appendNode("scope", scope) 73 | dep.artifacts.forEach {depArtifact -> 74 | appendNode("type", depArtifact.type) 75 | } 76 | } 77 | 78 | val exclusionsNode = dependencyNode.appendNode("exclusions") 79 | when { 80 | !dep.isTransitive -> { // If this dependency is not transitive, we should force exclude all its dependencies them from the POM 81 | val exclusionNode = exclusionsNode.appendNode("exclusion") 82 | exclusionNode.apply { 83 | appendNode("groupId", "*") 84 | appendNode("artifactId", "*") 85 | } 86 | } 87 | !dep.excludeRules.isEmpty() -> // Otherwise add specified exclude rules 88 | dep.excludeRules.forEach {rule -> 89 | val exclusionNode = exclusionsNode.appendNode("exclusion") 90 | exclusionNode.apply { 91 | appendNode("groupId", rule.group ?: "*") 92 | appendNode("artifactId", rule.module ?: "*") 93 | } 94 | } 95 | else -> { 96 | //do nothing 97 | } 98 | } 99 | } 100 | } 101 | 102 | // List all "compile" dependencies (for old Gradle) 103 | project.configurations.getByName("compile").dependencies.forEach { dep -> 104 | addDependency( 105 | dep, 106 | "compile" 107 | ) 108 | } 109 | 110 | //support api & implementation configuration until gradle version 3.4 111 | if (project.configurations.findByName("api") != null) { 112 | // List all "api" dependencies (for new Gradle) as "compile" dependencies 113 | project.configurations.getByName("api").dependencies.forEach {dep -> addDependency(dep, "api") } 114 | } 115 | 116 | if (project.configurations.findByName("implementation") != null) { 117 | // List all "implementation" dependencies (for new Gradle) as "runtime" dependencies 118 | project.configurations.getByName("implementation").dependencies.forEach {dep -> addDependency(dep, "runtime") } 119 | } 120 | } 121 | } 122 | 123 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/component/JavaComponentLibrary.kt: -------------------------------------------------------------------------------- 1 | package com.whl.component 2 | 3 | import com.whl.ComponentLibrary 4 | import org.gradle.api.Project 5 | import org.gradle.api.plugins.JavaPluginConvention 6 | import org.gradle.api.publish.maven.MavenPublication 7 | import org.gradle.api.tasks.bundling.Jar 8 | 9 | open class JavaComponentLibrary(private val project: Project) : ComponentLibrary() { 10 | 11 | override fun fromComponent(mavenPublication: MavenPublication) { 12 | mavenPublication.from(project.components.getByName("java")) 13 | } 14 | 15 | override fun docJar() : Any { 16 | val javadocJar = project.tasks.maybeCreate("javadocJar", Jar::class.java) 17 | javadocJar.from(project.tasks.getByName("javadoc")) 18 | javadocJar.classifier = "javadoc" 19 | return javadocJar 20 | } 21 | 22 | override fun sourcesJar() : Any{ 23 | val sourcesJar = project.tasks.maybeCreate("sourcesJar", Jar::class.java) 24 | val javaPluginConvention = project.convention.getPlugin(JavaPluginConvention::class.java) 25 | sourcesJar.from(javaPluginConvention.sourceSets.getByName("main").allJava) 26 | sourcesJar.classifier = "sources" 27 | return sourcesJar 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/whl/component/KotlinComponentLibrary.kt: -------------------------------------------------------------------------------- 1 | package com.whl.component 2 | 3 | import org.gradle.api.Project 4 | import org.gradle.api.plugins.JavaBasePlugin 5 | import org.gradle.api.tasks.bundling.Jar 6 | import org.jetbrains.dokka.gradle.DokkaPlugin 7 | 8 | class KotlinComponentLibrary(private val project: Project) : JavaComponentLibrary(project){ 9 | 10 | init { 11 | applyDokkaPlugin() 12 | } 13 | 14 | private fun applyDokkaPlugin() { 15 | //No more duplicate apply plugin if already had 16 | if (!project.plugins.hasPlugin("org.jetbrains.dokka")) { 17 | project.plugins.apply(DokkaPlugin::class.java) 18 | } 19 | } 20 | 21 | override fun docJar(): Any { 22 | val dokka = project.tasks.getByName("dokka") 23 | dokka.setProperty("outputFormat", "html") 24 | dokka.setProperty("outputDirectory", "${project.buildDir}/javadoc") 25 | 26 | val dokkaJar = project.tasks.maybeCreate("dokkaJar", Jar::class.java) 27 | dokkaJar.group = JavaBasePlugin.DOCUMENTATION_GROUP 28 | dokkaJar.description = "Assembles Kotlin docs with Dokka" 29 | dokkaJar.classifier = "javadoc" 30 | dokkaJar.from(dokka) 31 | return dokkaJar 32 | } 33 | 34 | } --------------------------------------------------------------------------------