如何发布没有源代码的 JAR?

huangapple go评论76阅读模式
英文:

How to publish a JAR with no source?

问题

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

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

cassandra-autoconfigure 包含源代码,而另外两个模块没有。cassandra-starter 的作用是将 cassandra-autoconfigure 作为 api(project(":cassandra-autoconfigure")) 引入;这是 Spring Boot 的标准约定cassandra-starter 包含一些测试代码,但 src/main/kotlin 中没有任何内容。

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

Artifact cassandra-starter-1.0.0-SNAPSHOT.jar 未在此构建中生成。

这是正确的,我可以看到 > 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
}
英文:

I've a multimodule Kotlin Gradle project:

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

cassandra-autoconfigure has source code, the other two don't. cassandra-starter's purpose in life is to bring in cassandra-autoconfigure as api(project(&quot;:cassandra-autoconfigure&quot;)); this is a standard convention in Spring Boot. cassandra-starter has some test code, but nothing in src/main/kotlin.

Problem is, when I try to publish these projects, it's failing.

Artifact cassandra-starter-1.0.0-SNAPSHOT.jar wasn&#39;t produced by this build.

This is true and I can see &gt; Task :cassandra-starter:jar SKIPPED. How do I force Gradle to build the jar with only META-INF in it?

data-cassandra/build.gradle.kts:

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

plugins {
    java
    kotlin(&quot;jvm&quot;) apply false
    `maven-publish`
}

allprojects {
    group = &quot;mycompany.data&quot;
    version = &quot;1.0.0-SNAPSHOT&quot;

    repositories {
        mavenCentral()
    }
}

subprojects {
    apply(plugin = &quot;kotlin&quot;)
    apply(plugin = &quot;maven-publish&quot;)

    tasks.withType&lt;Test&gt; {
        useJUnitPlatform()
    }

    tasks.withType&lt;KotlinCompile&gt; {
        kotlinOptions {
            freeCompilerArgs = listOf(
				&quot;-Xjsr305=strict&quot;
			)
            jvmTarget = &quot;11&quot;
        }
    }

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

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

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

tasks.withType&lt;PublishToMavenRepository&gt; {
    doFirst {
        println(&quot;Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}&quot;)
    }
}

cassandra-autoconfigure/build.gradle.kts

plugins {
	kotlin(&quot;jvm&quot;)
	kotlin(&quot;plugin.spring&quot;)
}

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

cassandra-starter/build.gradle.kts

plugins {
	id(&quot;org.springframework.boot&quot;)
	kotlin(&quot;jvm&quot;)
	kotlin(&quot;plugin.spring&quot;)
}

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

tasks.bootJar {
	enabled = false
}

答案1

得分: 0

回答我的问题,来自Spring Boot文档

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

cassandra-starter/build.gradle.kts

tasks.jar {
	enabled = true
}

修复了这个问题。

英文:

Answering my own question, from Spring Boot docs:

> By default, when the bootJar or bootWar tasks are configured, the jar
> or war tasks are disabled. A project can be configured to build both
> an executable archive and a normal archive at the same time by
> enabling the jar or war task:

cassandra-starter/build.gradle.kts

tasks.jar {
	enabled = true
}

fixes the issue.

huangapple
  • 本文由 发表于 2020年9月3日 07:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63714701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定