Gradle maven publish plugin config引用了动态创建的gradle任务

时间:2017-09-24 18:00:20

标签: android gradle maven-publish

我的build.gradle文件中有一个发布配置块,如下所示

docker run --name myredis --sysctl net.core.somaxconn=511 -d redis

}

注意publishing { publications { maven(MavenPublication) { groupId "groupId" artifactId 'artifactId' version "4.0" artifact "$buildDir/outputs/aar/artifact-release.aar" artifact releaseJavadocsJar // Generate pom file so that the artifact dependencies can be fetched automatically. pom.withXml { def dependenciesNode = asNode().appendNode('dependencies') configurations.compile.allDependencies.each { dependency -> def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', dependency.group) dependencyNode.appendNode('artifactId', dependency.name) dependencyNode.appendNode('version', dependency.version) } } } } ,这是一个像这样定义的gradle任务:

releaseJavadocsJar

当我同步gradle这一行时

  android.libraryVariants.all { variant ->
    if (variant.name == 'release') {
        def taskJavadoc = task("releaseJavadocs", type: Javadoc) {
            source = variant.javaCompile.source
            classpath = files(((Object) android.bootClasspath.join(File.pathSeparator)))
            classpath += files(variant.javaCompile.classpath.files)
        }
        task("releaseJavadocsJar", type: Jar) {
            classifier = 'javadoc'
            from taskJavadoc.destinationDir
        }
    }
}

来自出版物抱怨

   artifact releaseJavadocsJar

这意味着当gradle同步发生时,我的动态创建的任务还没有。

有什么方法吗?

1 个答案:

答案 0 :(得分:1)

将其封装在project.afterEvalute区块中并不起作用,但这样做了:

project.tasks.whenTaskAdded { addedTask ->
if (addedTask.name == 'releaseJavadocsJar') {
    // Publishing to local maven repo via 'gradlew publishToMavenLocal'.
    publishing.publications {
         maven(MavenPublication) {
    groupId "groupId"
    artifactId 'artifactId'
    version "4.0"
    artifact "$buildDir/outputs/aar/artifact-release.aar"
    artifact releaseJavadocsJar

    // Generate pom file so that the artifact dependencies can be fetched automatically.
    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')
        configurations.compile.allDependencies.each { dependency ->
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', dependency.group)
            dependencyNode.appendNode('artifactId', dependency.name)
            dependencyNode.appendNode('version', dependency.version)
        }
    }
}