Gradle、SpringBoot、MavenPublish -发布仅包含依赖项和/或约束,而不包含版本

rvpgvaaj  于 2023-09-29  发布在  Spring
关注(0)|答案(3)|浏览(170)

我在Gradle中使用maven-publish插件来发布我的Sping Boot 应用程序jar。我运行通常的任务:./gradlew artifactorypublish。但是,出现了以下错误,我无法理解其含义:

> Task :assembleArtifact
> Task :application-jar:compileJava UP-TO-DATE
> Task :application-jar:processResources UP-TO-DATE
> Task :application-jar:classes UP-TO-DATE
> Task :application-jar:jar SKIPPED
> Task :generateMetadataFileForMavenJavaPublication FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':generateMetadataFileForMavenJavaPublication'.
> Invalid publication 'mavenJava':
    - Publication only contains dependencies and/or constraints without a version. You need to add minimal version information, publish resolved versions (https://docs.gradle.org/6.1/userguide/publishing_maven.html#publishing_maven:resolved_dependencies) or reference a platform (https://docs.gradle.org/6.1/userguide/platforms.html)

我的构建。gradle:

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'maven-publish'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

publishing {
    publications {
        mavenJava(MavenPublication){
            components.java
        }
    }
}

...

版本号:
Gradle 6.1
Sping Boot 2.2.6(主要由Spring Boot Initializr生成)

wydwbb8l

wydwbb8l1#

您面临的问题实际上是由于Gradle 6.0.1附带的更改https://github.com/gradle/gradle/pull/11388而导致的所需行为
Gradle要求您提供适当版本的依赖项,以便禁止用户发布无效的Gradle模块元数据。下面关于这些问题的讨论将帮助您对此有更深入的了解
https://github.com/gradle/gradle/issues/11862
https://github.com/spring-gradle-plugins/dependency-management-plugin/issues/262
现在,在你的情况下,有两种解决方案你可以选择

publishing {
    publications {
        mavenJava(MavenPublication){
            // bootJar is the default build task configured by Spring Boot
            artifact bootJar
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }
    }
}

关于第二个选项的更多信息可以在此链接上找到-https://docs.gradle.org/6.6.1/userguide/publishing_maven.html

lrl1mhuk

lrl1mhuk2#

通过@中村扩展答案,如果您将一个项目设置为Springboot Library并需要设置:

bootJar {
    enabled=false
}

jar {
    enabled=true
}

您可以设置:

publishing {
    publications {
        mavenJava(MavenPublication){
            // jar is the default build task configured by Spring Boot if bootJar=false
            artifact jar
        }
    }
}
pobjuy32

pobjuy323#

折腾了一会儿,我找到了解决办法:

publishing {
    publications {
        mavenJava(MavenPublication){
            // bootJar is the default build task configured by Spring Boot
            artifact bootJar
        }
    }
}

详解

这是因为components.java被配置为默认的java插件任务:jarwar。但是对于Sping Boot ,在应用插件org.springframework.boot之后,默认任务变为bootJarbootWar
(For您的参考)从Sping Boot 文档:
可以使用bootJar任务构建可执行jar。该任务是在应用java插件时自动创建的,并且是BootJar的示例。assemble任务自动配置为依赖于bootJar任务,因此运行assemble(或build)也将运行bootJar任务。
因此,components.java无法正确识别伪影。我们应该指向bootJarbootWar
参考:https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#publishing-your-application-maven-publish

相关问题