英文:
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(":cassandra-autoconfigure"))
; 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't produced by this build.
This is true and I can see > 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("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
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论