Maven在Gradle插件中发布逻辑

时间:2019-08-29 13:00:07

标签: android maven gradle kotlin

我有一些要发布到本地Maven的库。我想为此使用插件,以最大程度地重复使用。

基本代码设置如下:

apply plugin: 'com.android.library'
apply plugin: 'LibraryPlugin'

android {
    defaultConfig {
        versionCode 1
        versionName "1.0q"
    }
}

def groupId = "x.y.z"
def artifactId = "LibZ"

project.afterEvaluate {
    publishing {
        publications {
            parseDebugLibraryResources(MavenPublication) {
                setGroupId groupId
                setArtifactId artifactId
                version = android.defaultConfig.versionName
            }
        }
    }
}

./gradlew publishToMavenLocal之后,将其正确放置在此处。 我想将该项目移动。afterEvaluate>发布>出版物内容到插件中,以便可以在多个项目中重复使用。

这是插件代码:

class LibraryPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        project.plugins.apply("kotlin-android")
        project.plugins.apply("maven-publish")
        project.dependencies.add("implementation", BuildPlugins.kotlinStandardLibrary)
        val extension = project.extensions.getByType<LibraryExtension>()
        extension.configureLibrary()

        project.afterEvaluate {
         //   publishing {

         //   }
        }
    }

    private fun LibraryExtension.configureLibrary() {
        setCompileSdkVersion(AndroidSdk.compile)
        defaultConfig.apply {
            setMinSdkVersion(AndroidSdk.min)
            setTargetSdkVersion(AndroidSdk.target)
            testInstrumentationRunner = TestLibraries.UI.instrumentationRunner
        }
    }
}

现在,它不知道Project.afterEvaluate中的“发布”节点。如何将代码移入插件?

1 个答案:

答案 0 :(得分:0)

我需要像这样获得发布节点:

val publishing = project.properties["publishing"] as PublishingExtension

完整示例:

class LibraryPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        project.plugins.apply("kotlin-android")
        project.plugins.apply("maven-publish")
        project.dependencies.add("implementation", BuildPlugins.kotlinStandardLibrary)
        val extension = project.extensions.getByType<LibraryExtension>()
        extension.configureLibrary()


        project.afterEvaluate {
            val publishing = project.properties["publishing"] as PublishingExtension
            publishing {

            }
        }
    }

    private fun LibraryExtension.configureLibrary() {
        setCompileSdkVersion(AndroidSdk.compile)
        defaultConfig.apply {
            setMinSdkVersion(AndroidSdk.min)
            setTargetSdkVersion(AndroidSdk.target)
            testInstrumentationRunner = TestLibraries.UI.instrumentationRunner
        }
    }
}