maven Gradle 7和jitpack.io在发布期间出错

x6492ojm  于 2023-06-21  发布在  Maven
关注(0)|答案(6)|浏览(216)

当我将Android项目升级到Gradle 7.0并希望在www.example.com中发布aar库jitpack.io,我遇到了

Script '/script/maven-plugin.gradle' line: 2

* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin 'com.github.dcendents.android-maven'.
   > Could not create plugin of type 'AndroidMavenPlugin'.
      > Could not generate a decorated class for type AndroidMavenPlugin.
         > org/gradle/api/publication/maven/internal/MavenPomMetaInfoProvider

查看完整日志https://jitpack.io/com/github/appdevnext/moka/0.7.1/build.log

93ze6v8z

93ze6v8z1#

Maven插件在Gradle 7.0中已被删除,请改用maven-publish插件。
我让它工作与

plugins {
    id 'maven-publish'
    // ...
}

task androidSourcesJar(type: Jar) {
    classifier 'sources'
    from android.sourceSets.main.java.srcDirs 
}

project.afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                artifact androidSourcesJar // optional sources
            }
        }
    }
}

你需要自己的jitpack.yml

jdk:
  - openjdk11
install:
  - ./gradlew build :lib:publishToMavenLocal

在这里,您可以看到完整的pull request https://github.com/AppDevNext/Moka/pull/77,现在它可以工作https://jitpack.io/#AppDevNext/moka/1.0

m4pnthwp

m4pnthwp2#

DSL版本

publishing {
      publications {
        val mavenJava by creating(MavenPublication::class) {
            from(components["java"])
        }
    }
}
v8wbuo2f

v8wbuo2f3#

hannes ach的回答对我很有效。
在旧的gradle中,...-sources.jar文件也被上传到jitpack。为了恢复这种行为,gradle代码段必须稍微增强:

project.afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                artifact androidSourcesJar
            }
        }
    }
}

task androidSourcesJar(type: Jar) {
    classifier 'sources'
    from android.sourceSets.main.java.srcDirs 
}
kulphzqa

kulphzqa4#

我做了这3个步骤(称为jitpack,定义Java ver。,发布Maven)请关注他们!

1-在build.gradle(Project:...)中添加:

repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }

然后在buildscript部分下添加:

plugins {
    id 'maven-publish'
}

2-在build.gradle(Module:app)中,在android部分添加:

compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

3-在build.gradle(Module:module-name)中,在dependencies部分后添加:

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId = 'com.github.yourgitid' //your git id
                artifactId = 'Myket-Intent' //your-repository
                version = '0.1.15' // As same as the Tag
            }
        }
    }
}
7rtdyuoh

7rtdyuoh5#

下面是我在Groovy中使用的方法

group = [groupId]
version = [version]

task javadoc(type: Javadoc) {
    configurations.implementation.canBeResolved(true)
    configurations.api.canBeResolved(true)

    failOnError false

    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    //destinationDir = file("../javadoc/")
    classpath += configurations.api
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier = "sources"
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release
                artifact(sourcesJar)

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.github.[name].[repo]'
                artifactId = '[submodule name]'
                version = '[version]'

                pom.withXml {
                    def dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
                    def configurationNames = ["implementation", "api"]

                    configurationNames.forEach { configurationName ->
                        configurations[configurationName].allDependencies.forEach {
                            if (it.group != null && it.version != "unspecified") {
                                def dependencyNode = dependenciesNode.appendNode("dependency")
                                dependencyNode.appendNode("groupId", it.group)
                                dependencyNode.appendNode("artifactId", it.name)
                                dependencyNode.appendNode("version", it.version)
                                // dependencyNode.appendNode("scope", configurationName)
                            }
                        }
                    }
                }
            }
        }
    }
}

以下是我在KTS的工作经验

group = [groupId]
version = [version]

val sourcesJar by tasks.registering(Jar::class) {
    from(android.sourceSets["main"].java.srcDirs)
    archiveClassifier.set("sources")
}

val javadoc by tasks.registering(Javadoc::class) {
    configurations.implementation.get().isCanBeResolved = true
    configurations.api.get().isCanBeResolved = true

    isFailOnError = false
    source = android.sourceSets["main"].java.getSourceFiles()
    classpath += project.files(android.bootClasspath.joinToString(separator = File.pathSeparator))
    classpath += configurations.api
}

// build a jar with javadoc
val javadocJar by tasks.registering(Jar::class) {
    dependsOn(javadoc)
    archiveClassifier.set("javadoc")
    from(javadoc.get().destinationDir)
}

artifacts {
    archives(sourcesJar)
    archives(javadocJar)
}

afterEvaluate {
    publishing {
        publications {
            register("mavenJava", MavenPublication::class) {
                groupId = "com.github.[name].[repo]"
                artifactId = "[submodule name]"
                version = "[version]"

                from(components["release"])
                artifact(sourcesJar.get())

                pom.withXml {
                    val dependenciesNode: groovy.util.Node =
                        (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
                    val configurationNames = arrayOf("implementation", "api")

                    configurationNames.forEach { configurationName ->
                        configurations[configurationName].allDependencies.forEach {
                            if (it.group != null && it.version != "unspecified") {
                                val dependencyNode = dependenciesNode.appendNode("dependency")
                                dependencyNode.appendNode("groupId", it.group)
                                dependencyNode.appendNode("artifactId", it.name)
                                dependencyNode.appendNode("version", it.version)
                                // dependencyNode.appendNode("scope", configurationName)
                            }
                        }
                    }
                }
            }
        }
    }
}

请注意,即使使用withXML块,api依赖项的传递依赖项也不会自动添加。我不得不手动添加它们。

zdwk9cvp

zdwk9cvp6#

这是一个已知且记录在案的问题:

  • 无法创建类型为“AndroidMavenPlugin”的插件
  • 无法为类型AndroidMavenPlugin生成修饰类。
  • org/gradle/API/publication/maven/internal/MavenPomMetaInfoProvider

对于我的情况,我必须声明:
app/module/build.gradle

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId = 'com.github.alkathirikhalid'
                artifactId = 'connection'
                version = 'v1.0.9'
            }
        }
    }
}

项目/build.gradle

plugins {
    id 'maven-publish'
}

参考文献:

相关问题