1

EDIT: Things have been dramatically changed. Here are the latest question.


I want to configure my Gradle project with multiple subproject to use shadowed jar as main jar, and to create javadoc with it.

Here are my current goals:

  1. When I run gradle publishToMavenLocal or gradle publish, Gradle must generate these files:
    1. Shadow Jar as Main:
      Since only reason I use shadow plugin in root project is combining all subprojects as one, the shadow jar must be main jar, which doesn't have any postfix like -all, -dev-all etc.
    2. Create javadoc of all subprojects:
      Each subprojects have javadoc comment on their source code. I want to create combined javadoc-like thing when publishing.

So, after publishing, there should be mol-1.0.0-SNAPSHOT.jar(shadow jar) and mol-1.0.0-SNAPSHOT-javadoc.jar(javadoc) present in Maven repository. (mol is name of my project)


Here are my Gradle build scripts. You can inspect full codes in GitHub repo because there are too many files related to.

NOTE: I think I've over-engineered my build script. So I minified my build scripts so no buildSrc is needed.

// <root>/build.gradle.kts
import org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE
import org.gradle.api.attributes.Bundling.EXTERNAL
import org.gradle.api.attributes.Category.CATEGORY_ATTRIBUTE
import org.gradle.api.attributes.Category.VERIFICATION
import org.gradle.api.attributes.VerificationType.MAIN_SOURCES
import org.gradle.api.attributes.VerificationType.VERIFICATION_TYPE_ATTRIBUTE
import org.gradle.kotlin.dsl.named

plugins {
  `java-library`
  `maven-publish`
  id("com.github.johnrengelman.shadow") version("7.1.2")
  //id("io.freefair.aggregate-javadoc") version("6.5.1")
  id("io.freefair.aggregate-javadoc-jar") version("6.5.1")
}

@Suppress("UnstableApiUsage")
val mainSources by configurations.creating<Configuration> {
  isVisible = false
  isCanBeResolved = true
  isCanBeConsumed = false

  attributes {
    attribute(CATEGORY_ATTRIBUTE, objects.named(VERIFICATION))
    attribute(BUNDLING_ATTRIBUTE, objects.named(EXTERNAL))
    attribute(VERIFICATION_TYPE_ATTRIBUTE, objects.named(MAIN_SOURCES))
  }
}

dependencies {
  implementation(project(":bukkit_1_19_R1", "reobf")) {
    isTransitive = false
  }
  implementation(project(":core"))

  mainSources(project(":bukkit_1_19_R1"))
  mainSources(project(":core"))
}

tasks {
  java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(17))
  }
  build {
    dependsOn(shadowJar)
  }
  shadowJar {
    archiveClassifier.set("")
  }
}

allprojects {
  group = "com.github.exmsrv"
  version = "1.0.0-SNAPSHOT"
}

@file:Suppress("UnstableApiUsage")

dependencyResolutionManagement {
  repositories {
    mavenCentral()
    maven("https://repo.papermc.io/repository/maven-public/")
  }

  pluginManagement {
    repositories {
      gradlePluginPortal()
      mavenCentral()
      maven("https://repo.papermc.io/repository/maven-public/")
    }
  }
}

rootProject.name = "mol"

include("core")
include("bukkit_1_19_R1")
// <root>/bukkit_1_19_R1/build.gradle.kts
plugins {
  `java-library`
  id("io.papermc.paperweight.userdev") version("1.3.8")
}

dependencies {
  compileOnly(project(":core"))
  paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.19.2-R0.1-SNAPSHOT")
}
// <root>/core/build.gradle.kts
plugins {
  `java-library`
}

dependencies {
  compileOnly("io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT")
}
// Do not put publishing block because it will be shadowed by shadowJar

As you can see, these are not 'complete'. I need help for completing these scripts.


NOTE: I've managed to publish shadowed jar as main, but for some reason, aggregateJavadocJar task takes too long to process. After inspecting javadoc.options file, it turned out it tried to create uber-javadoc which contains javadocs of every dependencies.

This is not what I meant. I want javadoc link to other javadoc link if it is not my code.

Here is what I found (javadoc.options)

tetratheta
  • 177
  • 1
  • 13
  • 1
    Sharing outputs between Gradle subprojects can be really difficult. This post might help: https://stackoverflow.com/a/73600442/4161471. Can you update your question to include any specific error messages or stacktrace that Gradle outputs? – aSemy Oct 09 '22 at 11:27
  • I added Gradle build scan result. Speaking of the [answer](https://stackoverflow.com/a/73600442/8587037), I'm kind of worried that my subprojects uses different dependencies, so I end up including every dependencies of my subprojects to rootproject to build 'combined' result. – tetratheta Oct 09 '22 at 13:06
  • I'm not sure if I did right, but after applying 'mergedJar' solution, build also fails. [Here](https://paste.gg/p/anonymous/d740a1a5622641f1aa503903e1177c39) are the current code of `build.gradle.kts` and its log. – tetratheta Oct 09 '22 at 13:17
  • "Cannot choose between the following variants of project" - it looks like project `:bukkit_1_19_R1` is providing two different variants (`reobf` and `runtimeElements`), and `mergedJar` can't distinguish between them. You can try and [add attributes](https://docs.gradle.org/current/userguide/cross_project_publications.html#sec:variant-aware-sharing) to the `mergedJar` configuration, or define your [own attributes and configurations](https://docs.gradle.org/7.3/samples/sample_jvm_multi_project_with_code_coverage.html) in each providing and consuming subproject. – aSemy Oct 09 '22 at 13:26
  • I write a draft guide for sharing outputs between subprojects in this issue: https://github.com/liutikas/gradle-best-practices/issues/6 – aSemy Oct 09 '22 at 13:28
  • In that 'draft guide', I couldn't find out what package/class to import for build scripts (both buildSrc and subprojects). IDE screams about `2 type arguments expected for fun > C.registering(action: T.() -> Unit): RegisteringDomainObjectDelegateProviderWithAction`. Also, to me, it is difficult to understand. All I could understand was very vague image of what are those code for, but that's all I could understand. – tetratheta Oct 09 '22 at 14:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248674/discussion-between-tetratheta-and-asemy). – tetratheta Oct 09 '22 at 14:05
  • NOTE: The problem hasn't been resolve. So if you have any other suggestion, feel free to tell me. – tetratheta Oct 11 '22 at 05:37

0 Answers0