https://kotlinlang.org logo
Title
a

Ayfri

04/05/2022, 5:25 PM
Hi, is there any tutorial on how to work with multiple gradle modules ?
t

Tim Oltjenbruns

04/05/2022, 8:35 PM
Yes, I would look at Gradle’s documentation itself
there are not many specifics for Kotlin gradle modules.
what are you trying to accomplish?
a

Ayfri

04/05/2022, 8:38 PM
I have a Kotlin/JS library that gives typings to a JavaScript library, but this JavaScript library has many optional dependencies and plugins, so I want to set typings also into other modules, but I've never used multiple modules for a project before
t

Tim Oltjenbruns

04/05/2022, 8:40 PM
Basically you make a new folder for the second module, set it up pretty much like the first, and when one needs to depend on the other you use
implementation(project(":projectName"))
instead of a maven dependency
If your current module is the root module, I suggest moving that into a subfolder first so they are siblings rather than parent-child
a

Ayfri

04/05/2022, 8:46 PM
Thanks ! Also, the JavaScript library has like 20 optional plugins (I've just done typings for 6 of them), do I really need to have all these folders in my project or won't it be cleaner to have a directory for all these ?
t

Tim Oltjenbruns

04/05/2022, 8:52 PM
Depends how you want to bundle them. If you want them all bundled separately, you need a separate folder for each
a

Ayfri

04/05/2022, 8:52 PM
No I mean like a
plugins
folder with all modules inside
t

Tim Oltjenbruns

04/05/2022, 8:54 PM
Sorry I’m not following, what is in this folder? The JS modules?
If you have a 1:1 relationship with the JS plugin to the KT typing/gradle module then I would expect each gradle module to have the JS plugin it depends on in it’s folder
a

Ayfri

04/05/2022, 8:58 PM
I mean like this file tree:
/buildSrc
  build.gradle.kts
/gradle
/main-module
  build.gradle.kts
/plugins
  /plugin1
    build.gradle.kts
  /plugin2
    build.gradle.kts
  /plugin3
    build.gradle.kts
build.gradle.kts
gradle.properties
...
t

Tim Oltjenbruns

04/05/2022, 9:00 PM
Yes you can do that.
if plugin 1 needs to depend on plugin 2, then plugin 1 does
implementation(project(":plugins:plugin2")
if plugin 1 depends on main-module then you can do
implementation(project(":main-module"))
a

Ayfri

04/05/2022, 9:01 PM
okay I see, thanks I'll try !
t

Tim Oltjenbruns

04/05/2022, 9:02 PM
If you don’t want the
:plugins
prefix you can get around it with the settings.gradle.kts file. But I’ll save my breath unless you really want to do that
a

Ayfri

04/05/2022, 9:04 PM
Yeah I don't need that
1
Also, publishing should be kept at root
build.gradle.kts
? But then how can I target a jar to be published from a specific module ?
t

Tim Oltjenbruns

04/05/2022, 9:08 PM
you would have a publishing plugin and task on each submodule
but if you like the root can call all the publish tasks
a

Ayfri

04/05/2022, 9:10 PM
I guess that signing and nexusPublishing should be kept at root as it will be the exact same thing for each modules ?
t

Tim Oltjenbruns

04/05/2022, 9:12 PM
sharing config across gradle modules is a huge topic worthy of it’s own discussion
The naive way to do it is use allprojects in the root gradle file. It’s not performant, but it works.
It breaks configuration avoidance which means gradle will configure all your modules, even if you only want to build one.
a

Ayfri

04/05/2022, 10:24 PM
I'm having
Projects must be configuring
error in each submodule no matter what I try :/
t

Tim Oltjenbruns

04/05/2022, 10:31 PM
Try settling with the duplication for now. Apply the publishing Gradle plugin and configure it in each and every submodule.
It's not pretty but it will rule out some other issues
a

Ayfri

04/05/2022, 10:45 PM
Oh, I was just having a dependency not found, fixed it and everything working now x)