【问题标题】:How to publish a JAR with no source?如何发布没有源的 JAR?
【发布时间】:2020-09-02 23:17:05
【问题描述】:

我有一个多模块 Kotlin Gradle 项目:

data-cassandra
-- cassandra-autoconfigure
-- cassandra-starter

cassandra-autoconfigure 有源代码,其他两个没有。 cassandra-starter的人生目的是把cassandra-autoconfigure作为api(project(":cassandra-autoconfigure"))引入;这是standard convention in Spring Bootcassandra-starter 有一些测试代码,但 src/main/kotlin 没有。

问题是,当我尝试发布这些项目时,它失败了。

Artifact cassandra-starter-1.0.0-SNAPSHOT.jar wasn't produced by this build.

这是真的,我可以看到> Task :cassandra-starter:jar SKIPPED。如何强制 Gradle 构建仅包含 META-INF 的 jar?

data-cassandra/build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    java
    kotlin("jvm") apply false
    `maven-publish`
}

allprojects {
    group = "mycompany.data"
    version = "1.0.0-SNAPSHOT"

    repositories {
        mavenCentral()
    }
}

subprojects {
    apply(plugin = "kotlin")
    apply(plugin = "maven-publish")

    tasks.withType<Test> {
        useJUnitPlatform()
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf(
                "-Xjsr305=strict"
            )
            jvmTarget = "11"
        }
    }

    plugins.withType<JavaPlugin> {
        extensions.configure<JavaPluginExtension> {
            sourceCompatibility = JavaVersion.VERSION_11
            targetCompatibility = JavaVersion.VERSION_11
        }
        val springBootVersion: String by project
        dependencies {
            implementation(platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
            implementation("org.jetbrains.kotlin:kotlin-reflect")
            implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
            testImplementation("org.springframework.boot:spring-boot-starter-test") {
                exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
                exclude(group = "com.vaadin.external.google", module = "android-json")
            }
        }
    }

    val isSnapshot = project.version.toString().toLowerCase().endsWith("snapshot")
    val artifactoryUrl = property("artifactoryUrl") as String

    extensions.configure<PublishingExtension> {
        publications {
            create<MavenPublication>("maven") {
                from(components["java"])
            }
            repositories {
                maven {
                    url = uri(artifactoryUrl + if (isSnapshot) "/libs-snapshot-local" else "/libs-release-local")
                    credentials {
                        username = property("artifactoryUsername") as String
                        password = property("artifactoryPassword") as String
                    }
                    if (!artifactoryUrl.startsWith("https")) {
                        isAllowInsecureProtocol = true
                    }
                }
            }
        }
    }
}

tasks.withType<PublishToMavenRepository> {
    doFirst {
        println("Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}")
    }
}

cassandra-autoconfigure/build.gradle.kts

plugins {
    kotlin("jvm")
    kotlin("plugin.spring")
}

val springDataVersion: String by project
dependencies {
    implementation(platform("org.springframework.data:spring-data-releasetrain:$springDataVersion"))
    api("org.springframework.boot:spring-boot-starter-data-cassandra-reactive")
}

cassandra-starter/build.gradle.kts

plugins {
    id("org.springframework.boot")
    kotlin("jvm")
    kotlin("plugin.spring")
}

val cassandraUnitVersion: String by project
dependencies {
    api(project(":cassandra-autoconfigure"))
    testImplementation("org.cassandraunit:cassandra-unit:$cassandraUnitVersion") {
        exclude(group = "org.hibernate", module = "hibernate-validator")
    }
}

tasks.bootJar {
    enabled = false
}

【问题讨论】:

  • 可以分享模块的 Gradle 文件吗?
  • @FranciscoMateo 查看更新。

标签: java spring-boot kotlin gradle


【解决方案1】:

回答我自己的问题,来自Spring Boot docs

默认情况下,当配置了 bootJar 或 bootWar 任务时,jar 或战争任务被禁用。一个项目可以配置为同时构建 一个可执行存档和一个普通存档同时通过 启用 jar 或 war 任务:

cassandra-starter/build.gradle.kts

tasks.jar {
    enabled = true
}

解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多