Spring Boot使用1.5.6发布重新打包的子模块jar

时间:2019-02-11 19:37:04

标签: java spring spring-boot gradle

我们已经将maven多模块项目转换为gradle,并且在spring boot jar的发布方面遇到了问题。

设置就像这样

root
|- module1
|- common
|- common-test
|- module2-common
|- module2-write
|- module3-common
|- module3-write
|- module4
|- module5
|- deploy

deploy包含重新包装的jar。

发布(使用maven-publish)时,工件只有几千字节大。显然,实际上并没有发布deploy jar。

部署build.gradle.kts

import org.flywaydb.gradle.task.FlywayMigrateTask
import org.springframework.boot.gradle.repackage.RepackageTask
import org.springframework.boot.gradle.run.BootRunTask

plugins {
    kotlin("jvm")
    id("org.springframework.boot") version ("1.5.6.RELEASE")
    id("io.spring.dependency-management") version("1.0.6.RELEASE")
    id("org.flywaydb.flyway")
    java
}

tasks {
    withType<Jar> {
        baseName = project.name
        version = project.version as String
    }

    val implementation = configurations.getByName("implementation")
    configurations.create("includeAllJars") {
        this.extendsFrom(implementation)
    }
    withType<RepackageTask> {
        this.setCustomConfiguration("includeAllJars")
//        this.classifier = "exec"
    }
}

build.gradle.kts

plugins {
    base
    java
    idea
    kotlin("jvm") version "1.2.51"
    kotlin("plugin.jpa") version ("1.2.51")
    kotlin("plugin.spring") version ("1.2.51")
    id("nebula.maven-publish") version "8.1.0"
    ...
}

allprojects {
    group = "com.company.group"

    ext {
        set("springBootVersion", "1.5.6.RELEASE")
    }
    apply {
        plugin("org.jetbrains.kotlin.jvm")
        plugin("org.jetbrains.kotlin.plugin.jpa")
        plugin("org.jetbrains.kotlin.plugin.spring")
    }
    dependencies {
        implementation(kotlin("stdlib-jdk8"))
        implementation(kotlin("reflect"))
        implementation(platform("org.springframework.boot:spring-boot-dependencies:${extra["springBootVersion"]}"))
        ...
    }
}

val sourcesJar by tasks.registering(Jar::class) {
    classifier = "sources"
    from(sourceSets["main"].allSource)
}

publishing {
    publications {
        register("maven", MavenPublication::class) {
//            from(project(":server-deploy").components["java"])
//            artifact(sourcesJar.get())
//            artifact(file("deploy/build/docker/runnable.jar"))
        }
    }
    repositories {
        ...
    }
}

对于出版物部分,我尝试指定 from(components["java"])artifact(file("deploy/build/libs/deploy-${project.version}.jar"))和以上选项。所有这些导致发布的jar仅1-3KB大。

我尝试将发布块移至部署build.gradle.kts,但这会导致此错误

* What went wrong:
Execution failed for task ':deploy:publishMavenPublicationToMavenLocal'.
> Failed to publish publication 'maven' to repository 'mavenLocal'
   > Invalid publication 'maven': POM file is invalid. Check any modifications you have made to the POM file.

The Spring Boot docs for the gradle plugin似乎没有列出任何相关内容。

1 个答案:

答案 0 :(得分:0)

关于威尔金森先生的评论,他完全正确地说,Spring Boot似乎不支持Gradle5。解决方案是在包含Spring Boot插件的一个模块中不使用platform(),而使用{ {1}}其他地方。这不是一个长期的解决方案!在我们的情况下这是完美的,因为我们直接从Maven过渡到Gradle,然后立即从Spring Boot 1过渡到Spring Boot2。

platform()

deploy/build.gradle.kts

plugins { id("org.springframework.boot") version ("1.5.6.RELEASE") id("io.spring.dependency-management") version("1.0.6.RELEASE") id("nebula.maven-publish") java … } dependencyManagement { imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${extra["springBootVersion"]}") } } build.gradle.kts之外的所有其他deploy

root