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

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

How to publish a JAR with no source?

问题

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

  1. data-cassandra
  2. -- cassandra-autoconfigure
  3. -- cassandra-starter

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

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

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

这是正确的,我可以看到 > Task :cassandra-starter:jar SKIPPED。我如何强制 Gradle 只构建包含 META-INF 的 JAR 文件?

data-cassandra/build.gradle.kts

  1. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
  2. plugins {
  3. java
  4. kotlin("jvm") apply false
  5. maven-publish
  6. }
  7. allprojects {
  8. group = "mycompany.data"
  9. version = "1.0.0-SNAPSHOT"
  10. repositories {
  11. mavenCentral()
  12. }
  13. }
  14. subprojects {
  15. apply(plugin = "kotlin")
  16. apply(plugin = "maven-publish")
  17. tasks.withType<Test> {
  18. useJUnitPlatform()
  19. }
  20. tasks.withType<KotlinCompile> {
  21. kotlinOptions {
  22. freeCompilerArgs = listOf(
  23. "-Xjsr305=strict"
  24. )
  25. jvmTarget = "11"
  26. }
  27. }
  28. plugins.withType<JavaPlugin> {
  29. extensions.configure<JavaPluginExtension> {
  30. sourceCompatibility = JavaVersion.VERSION_11
  31. targetCompatibility = JavaVersion.VERSION_11
  32. }
  33. val springBootVersion: String by project
  34. dependencies {
  35. implementation(platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
  36. implementation("org.jetbrains.kotlin:kotlin-reflect")
  37. implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
  38. testImplementation("org.springframework.boot:spring-boot-starter-test") {
  39. exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
  40. exclude(group = "com.vaadin.external.google", module = "android-json")
  41. }
  42. }
  43. }
  44. val isSnapshot = project.version.toString().toLowerCase().endsWith("snapshot")
  45. val artifactoryUrl = property("artifactoryUrl") as String
  46. extensions.configure<PublishingExtension> {
  47. publications {
  48. create<MavenPublication>("maven") {
  49. from(components["java"])
  50. }
  51. repositories {
  52. maven {
  53. url = uri(artifactoryUrl + if (isSnapshot) "/libs-snapshot-local" else "/libs-release-local")
  54. credentials {
  55. username = property("artifactoryUsername") as String
  56. password = property("artifactoryPassword") as String
  57. }
  58. if (!artifactoryUrl.startsWith("https")) {
  59. isAllowInsecureProtocol = true
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. tasks.withType<PublishToMavenRepository> {
  67. doFirst {
  68. println("Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}")
  69. }
  70. }

cassandra-autoconfigure/build.gradle.kts

  1. plugins {
  2. kotlin("jvm")
  3. kotlin("plugin.spring")
  4. }
  5. val springDataVersion: String by project
  6. dependencies {
  7. implementation(platform("org.springframework.data:spring-data-releasetrain:$springDataVersion"))
  8. api("org.springframework.boot:spring-boot-starter-data-cassandra-reactive")
  9. }

cassandra-starter/build.gradle.kts

  1. plugins {
  2. id("org.springframework.boot")
  3. kotlin("jvm")
  4. kotlin("plugin.spring")
  5. }
  6. val cassandraUnitVersion: String by project
  7. dependencies {
  8. api(project(":cassandra-autoconfigure"))
  9. testImplementation("org.cassandraunit:cassandra-unit:$cassandraUnitVersion") {
  10. exclude(group = "org.hibernate", module = "hibernate-validator")
  11. }
  12. }
  13. tasks.bootJar {
  14. enabled = false
  15. }
英文:

I've a multimodule Kotlin Gradle project:

  1. data-cassandra
  2. -- cassandra-autoconfigure
  3. -- 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.

  1. 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:

  1. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
  2. plugins {
  3. java
  4. kotlin(&quot;jvm&quot;) apply false
  5. `maven-publish`
  6. }
  7. allprojects {
  8. group = &quot;mycompany.data&quot;
  9. version = &quot;1.0.0-SNAPSHOT&quot;
  10. repositories {
  11. mavenCentral()
  12. }
  13. }
  14. subprojects {
  15. apply(plugin = &quot;kotlin&quot;)
  16. apply(plugin = &quot;maven-publish&quot;)
  17. tasks.withType&lt;Test&gt; {
  18. useJUnitPlatform()
  19. }
  20. tasks.withType&lt;KotlinCompile&gt; {
  21. kotlinOptions {
  22. freeCompilerArgs = listOf(
  23. &quot;-Xjsr305=strict&quot;
  24. )
  25. jvmTarget = &quot;11&quot;
  26. }
  27. }
  28. plugins.withType&lt;JavaPlugin&gt; {
  29. extensions.configure&lt;JavaPluginExtension&gt; {
  30. sourceCompatibility = JavaVersion.VERSION_11
  31. targetCompatibility = JavaVersion.VERSION_11
  32. }
  33. val springBootVersion: String by project
  34. dependencies {
  35. implementation(platform(&quot;org.springframework.boot:spring-boot-dependencies:$springBootVersion&quot;))
  36. implementation(&quot;org.jetbrains.kotlin:kotlin-reflect&quot;)
  37. implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8&quot;)
  38. testImplementation(&quot;org.springframework.boot:spring-boot-starter-test&quot;) {
  39. exclude(group = &quot;org.junit.vintage&quot;, module = &quot;junit-vintage-engine&quot;)
  40. exclude(group = &quot;com.vaadin.external.google&quot;, module = &quot;android-json&quot;)
  41. }
  42. }
  43. }
  44. val isSnapshot = project.version.toString().toLowerCase().endsWith(&quot;snapshot&quot;)
  45. val artifactoryUrl = property(&quot;artifactoryUrl&quot;) as String
  46. extensions.configure&lt;PublishingExtension&gt; {
  47. publications {
  48. create&lt;MavenPublication&gt;(&quot;maven&quot;) {
  49. from(components[&quot;java&quot;])
  50. }
  51. repositories {
  52. maven {
  53. url = uri(artifactoryUrl + if (isSnapshot) &quot;/libs-snapshot-local&quot; else &quot;/libs-release-local&quot;)
  54. credentials {
  55. username = property(&quot;artifactoryUsername&quot;) as String
  56. password = property(&quot;artifactoryPassword&quot;) as String
  57. }
  58. if (!artifactoryUrl.startsWith(&quot;https&quot;)) {
  59. isAllowInsecureProtocol = true
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. tasks.withType&lt;PublishToMavenRepository&gt; {
  67. doFirst {
  68. println(&quot;Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}&quot;)
  69. }
  70. }

cassandra-autoconfigure/build.gradle.kts

  1. plugins {
  2. kotlin(&quot;jvm&quot;)
  3. kotlin(&quot;plugin.spring&quot;)
  4. }
  5. val springDataVersion: String by project
  6. dependencies {
  7. implementation(platform(&quot;org.springframework.data:spring-data-releasetrain:$springDataVersion&quot;))
  8. api(&quot;org.springframework.boot:spring-boot-starter-data-cassandra-reactive&quot;)
  9. }

cassandra-starter/build.gradle.kts

  1. plugins {
  2. id(&quot;org.springframework.boot&quot;)
  3. kotlin(&quot;jvm&quot;)
  4. kotlin(&quot;plugin.spring&quot;)
  5. }
  6. val cassandraUnitVersion: String by project
  7. dependencies {
  8. api(project(&quot;:cassandra-autoconfigure&quot;))
  9. testImplementation(&quot;org.cassandraunit:cassandra-unit:$cassandraUnitVersion&quot;) {
  10. exclude(group = &quot;org.hibernate&quot;, module = &quot;hibernate-validator&quot;)
  11. }
  12. }
  13. tasks.bootJar {
  14. enabled = false
  15. }

答案1

得分: 0

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

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

cassandra-starter/build.gradle.kts

  1. tasks.jar {
  2. enabled = true
  3. }

修复了这个问题。

英文:

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

  1. tasks.jar {
  2. enabled = true
  3. }

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:

确定