Dinesh Kannan Rajaram
07/12/2024, 9:10 AMlibs.versions.toml under rootProject/gradle/. Can I use libs.* in my build.gradle.kts under rootProject/buildSrc ?Vampire
07/12/2024, 9:15 AMbuildSrc/settings.gradle.kts, yes, otherwise notVampire
07/12/2024, 9:15 AMbuildSrc is a standalone build that just happens to be built before your main buildDinesh Kannan Rajaram
07/12/2024, 9:17 AMVampire
07/12/2024, 9:18 AMbuildSrc is not a subproject, it is a standalone build.
And as I said, you can easily share it by just defining to use it in its settings script.Vampire
07/12/2024, 9:18 AMDinesh Kannan Rajaram
07/12/2024, 9:35 AMDemoTask.Kt I want to register a task of this type in my build.gradle.kts .
Should I use buildSrc or Precompiled script plugins. Which one is better/ recommended ?
Currently I am having to define the task classes in a separate build and include this build to register tasks in different one and set the buildscript/dependencies/classpath.Sergej Koščejev
07/12/2024, 9:39 AMDinesh Kannan Rajaram
07/12/2024, 9:42 AM2) explicitly included build is what I am currently using.
I was thinking If I can create a pre-compiled plugin that provides the task (demo-task) , I can apply this to the build file. Can I not do that ?Sergej Koščejev
07/12/2024, 9:43 AMSergej Koščejev
07/12/2024, 9:48 AMVampire
07/12/2024, 9:48 AMbuildSrc whenever possible and only use buildSrc if I really need to, like for example to monkey-patch 3rd-party plugin classes.Vampire
07/12/2024, 9:49 AMVampire
07/12/2024, 9:50 AMI start with buildSrc because it’s simpler and less boilerplatetechnically, yes, 1 line less boilerplate 🙂
Sergej Koščejev
07/12/2024, 9:51 AMVampire
07/12/2024, 9:52 AMSergej Koščejev
07/12/2024, 9:52 AMSergej Koščejev
07/12/2024, 9:52 AMVampire
07/12/2024, 9:53 AMbuildSrc 🙂Dinesh Kannan Rajaram
07/12/2024, 10:03 AMbuildSrc yet but just exploring my options. I am starting with a simple use case of creating a module and registering a custom task.
I can define the CustomTask class in the build.gradle.kts but it will become difficult to maintain in the future.
If the CustomTask is defined out of the build file then I can either use an included build or buildSrc.
And I understand now that included builds are the recommended way.
And then I came across the pre compiled script plugins which might also help me what I trying to do.
Thanks for the inputs and suggestions. 🙏Vampire
07/12/2024, 10:21 AMbuildSrc and an included build is, depends on whom you ask.
Even Gradle is not really consistent imho, as the docs in most places still show buildSrc, while the init task generates an included build for build logic.
But by tendency, I would trust code more than documentation. 🙂
If you ask me, I recommend using an included build. But for small projects the subtle differences between the two are most often neglectible.
precompiled script plugins are really just syntactic sugar to creating a binary plugin, the end result should be the same, and you could as well publish plugins written as precompiled script plugins.Dinesh Kannan Rajaram
07/15/2024, 9:52 AMbuildSrc but don't know how to do it with a subproject.
root-project
├── build.gradle.kts
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── sub-project
├── build.gradle.kts
├── settings.gradle.kts
└── src
├── main
│ ├── kotlin
│ │ └── com.example
│ │ └── MyCustomTask.kt
│ └── resources
└── test
├── kotlin
│ └── com.example
│ └── MyCustomTaskTest.kt
└── resourcesVampire
07/15/2024, 9:57 AMbuildSrc is built and put via a parent class loader to the classpath of all build scripts. That's also the reason why for example all tasks in all projects are out-of-date when you change anything in buildSrc.
With an included build, it is only built and added to the classpath - including version conflict resolution of dependencies - when it is actually used.
If you use a plugin that is built there, this happens automatically.
If you just want to use that custom task, you need to add the buildscript classpath dependency in some way.
On is to depend on it in buildscript { dependencies { classpath(...) } }, another would be to add a plugin with no actions that you apply to your project, just to get the code onto the classpath that way.Sergej Koščejev
07/15/2024, 10:05 AMpluginManagement {
includeBuild("sub-project")
}
It has to be added to the beginning of the file, before the plugins block.Vampire
07/15/2024, 10:08 AMbuildSrc and only build what is used and necessary and only includes it where it is used.Dinesh Kannan Rajaram
07/15/2024, 11:24 AMbuildscript { dependencies { classpath(...) } }
I tried classpath(":sub-project") and classpath(files("sub-project/src/main/com/example"))
If I build the compile the subproject independently and point to its build/libs/sub-project.jar it works fine. So was wondering how to build the subproject and make it available to the buildScript {}Sergej Koščejev
07/15/2024, 11:27 AMplugins block instead? It would make your life easier. If you have to use buildscript then you can give your project a group and name and use that group and name in classpath, e.g. classpath("mygroup:myplugin") and Gradle should automatically replace it with a dependency on the project from the included build.Dinesh Kannan Rajaram
07/15/2024, 11:54 AMA problem occurred configuring root project 'root-project'.
> Could not resolve all files for configuration ':classpath'.
> Could not find org.example:sub-project:.
Required by:
project :
I have include(":sub-project") in my root-project/settings.gradle.kts
And for the plugins{} is it same as what mentioned by Bjorn ? to register a plugin with no tasks and apply to the root project's build.gradle.kts ?Sergej Koščejev
07/15/2024, 11:56 AMgroup = "org.example" in your subproject’s build.gradle.kts?
yes, that’s the same as Björn suggested.Dinesh Kannan Rajaram
07/15/2024, 12:09 PMdo you setYes I did.in your subproject’s build.gradle.kts?group = "org.example"
Dinesh Kannan Rajaram
07/15/2024, 2:29 PMsub-project/build.gradle.kts where GreetingsPlugin Class is defined in src/main/com/example/MyCustomTask.kt
plugins {
kotlin("jvm") version "2.0.0"
`java-gradle-plugin`
}
dependencies {
implementation(libs.bundles.jackson)
implementation(libs.bundles.http4k)
testImplementation(libs.bundles.test)
}
gradlePlugin{
plugins{
create("greetingsPlugin"){
id = "com.example.greetingsPlugin"
implementationClass = "com.example.GreetingsPlugin"
}
}
}
I apply the plugin in my root-project/build.gradle.kts like this
import com.example.MyCustomTask
plugins {
id("com.example.greetingsPlugin")
}
tasks.register("customTask", MyCustomTask::class.java) {
println("do something")
}
Subproject is included in th rootproject's settings
But Still I get this error.
Plugin [id: 'com.example.greetingsPlugin'] was not found in any of the following sources:
I am not sure If I am missing something 😐Sergej Koščejev
07/15/2024, 2:31 PMDinesh Kannan Rajaram
07/15/2024, 2:35 PMrootProject.name and include("sub-project)Sergej Koščejev
07/15/2024, 2:35 PMDinesh Kannan Rajaram
07/15/2024, 2:36 PMrootProject.name = "root-project"
include(":sub-project")Sergej Koščejev
07/15/2024, 2:36 PMSergej Koščejev
07/15/2024, 2:36 PMSergej Koščejev
07/15/2024, 2:36 PMDinesh Kannan Rajaram
07/15/2024, 2:45 PMSergej Koščejev
07/15/2024, 2:46 PMDinesh Kannan Rajaram
07/15/2024, 2:48 PMSergej Koščejev
07/15/2024, 2:49 PMSergej Koščejev
07/15/2024, 2:50 PMDinesh Kannan Rajaram
07/15/2024, 2:50 PM